我正在为Woocommerce编写一个代码,它将自动合并组合来自同一客户的所有订单,直到我们翻转状态。我遇到的唯一问题是,当客户下第二个订单时,行项目会重复,其中包含与上一个订单相同的项目之一。这使得我们的工作人员更加困惑。
我已经包含了一个部分,检查该项目是否存在于以前的订单中,然后它更新该项目的数量,而不是添加一个新行。这部分代码在结帐时导致错误:“错误处理结帐。请稍后再试。”没有这部分代码,一切都按预期工作。
// Add a custom function to check and combine orders
function combine_orders_on_status_change($order_id) {
// Get the current order
$current_order = wc_get_order($order_id);
// Check if the order status is 'on-hold'
if ($current_order->get_status() === 'on-hold') {
// Get user ID of the current order
$user_id = $current_order->get_user_id();
// Get all orders of the same user with status 'on-hold'
$args = array(
'post_type' => 'shop_order',
'post_status' => 'wc-on-hold',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => '_customer_user',
'value' => $user_id,
),
),
);
$orders = get_posts($args);
// Find the older order with added items to combine with
$older_order = null;
foreach ($orders as $order_post) {
$order_to_combine = wc_get_order($order_post->ID);
// Check if it's not the same order and it's not already combined
if ($order_to_combine && $order_to_combine->get_id() !== $order_id && $order_to_combine->get_status() !== 'combined') {
// Check if shipping addresses match (if present in both orders)
$current_shipping_address = $current_order->get_address('shipping');
$combine_shipping_address = $order_to_combine->get_address('shipping');
if (!empty($current_shipping_address) && !empty($combine_shipping_address) && $current_shipping_address !== $combine_shipping_address) {
continue; // Skip combining if shipping addresses don't match
}
// Check if delivery dates match (if present in both orders)
$current_delivery_date = $current_order->get_meta('_orddd_timestamp', true);
$combine_delivery_date = $order_to_combine->get_meta('_orddd_timestamp', true);
if (!empty($current_delivery_date) && !empty($combine_delivery_date) && $current_delivery_date !== $combine_delivery_date) {
continue; // Skip combining if delivery dates don't match
}
// Set the older order with added items to combine with
$older_order = $order_to_combine;
break; // Stop after finding the first eligible order
}
}
// If an older order with added items was found, combine orders
if ($older_order) {
// Check if the older order is transitioning from 'received' to 'processing'
if ($older_order->get_status() === 'on-hold' && $current_order->get_status() === 'processing') {
return;
}
// Combine orders by copying items
$combined_items_list = array();
foreach ($current_order->get_items() as $item) {
$product_id = $item->get_product_id();
$quantity = $item->get_quantity();
// Check if the product already exists in the combined order
$existing_item = $older_order->get_item($product_id);
if ($existing_item) {
// If the product exists, update the quantity
$existing_quantity = $existing_item->get_quantity();
$older_order->set_quantity($existing_item, $existing_quantity + $quantity);
} else {
// If the product does not exist, add it to the combined order
$older_order->add_product(wc_get_product($product_id), $quantity);
}
$combined_items_list[] = $item->get_name() . ' x' . $quantity;
// Restore stock for each item
wc_update_product_stock($product_id, $quantity, 'increase');
}
// Copy customer notes with clear delineation
$custom_order_number = $current_order->get_order_number();
$customer_note = $current_order->get_customer_note();
if (!empty($customer_note)) {
$existing_notes = $older_order->get_customer_note();
$combined_notes = $existing_notes . "\n--- Note from Order #" . $custom_order_number . " ---\n" . $customer_note;
$older_order->set_customer_note($combined_notes);
}
// Move the current order with added items to 'combined' status
$current_order->update_status('combined', __('Orders combined', 'astra'));
// Add a note to the order log
$combined_items_log = implode(', ', $combined_items_list);
$note_combined = sprintf(__('Order combined with Order #%s. Items added: %s', 'astra'), $custom_order_number, $combined_items_log);
$older_order->add_order_note($note_combined);
// Log entry for the newer order
$note_newer = sprintf(__('Items added to Order #%s during combination.', 'astra'), $older_order->get_order_number());
$current_order->add_order_note($note_newer);
$older_order->calculate_totals();
// Save changes to the orders
$older_order->save();
$current_order->save();
// Fire the update order action hook after the old_order is saved
wc_reduce_stock_levels($older_order->get_id());
}
}
}
// Hook into order status change
add_action('woocommerce_order_status_changed', 'combine_orders_on_status_change', 10, 3);
字符串
2条答案
按热度按时间iovurdzv1#
你提供的信息是不够的,但尝试这些,我希望它的工作:
查找项目:
字符串
更新数量:
型
**错误处理和调试:**通过将此代码添加到wp-tag.php来启用WP_DEBUG
型
还添加错误处理以捕获订单组合过程中发生的任何问题。这将有助于识别导致错误的过程的特定部分。
but5z9lq2#
这里是更新的代码,你应该尝试
字符串