我希望在收到订单后,将数量大于1的行项目拆分为单独的行项目。我读了很多关于这方面的书,看到了很多在签出过程发生之前做这件事的好脚本,比如https://businessbloomer.com/woocommerce-display-separate-cart-items-product-quantity-1/ 和https://stackoverflow.com/questions/32485152/woocommerce-treat-cart-items-separate-if-quantity-is-more-than-1?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa.
订单完成后,有人有没有想过该怎么做?还是在WooCommerce中创建订单?我想把任何数量大于1的东西分成单独的行项目。
所以我认为我需要访问所有行项目,然后找到数量大于1的行项目,然后添加新的行项目并减少数量,直到所有项目都平衡。我需要一些帮助的地方是如何创建行项目?我知道我可以检查它们,如下所示:
function inspect_line_items()
{
$order = wc_get_order( 390 );
foreach ($order->get_items() as $item_id => $item_data) {
// Get an instance of corresponding the WC_Product object
$product = $item_data->get_product();
$product_name = $product->get_name(); // Get the product name
$item_quantity = $item_data->get_quantity(); // Get the item quantity
$item_total = $item_data->get_total(); // Get the item line total
// Displaying this data (to check)
if ($item_quantity >1 ){
echo \'HALP!\';
}
}
}
好的,我正在继续尝试,我已经能够添加行项目(这不是prod,只是测试它,显然这有一些差距:))。这就是说,我可以在结帐后添加一个新项目,然后使用另一个挂钩将订单状态更改回处理状态。现在的问题是显示的价格。即使我减少了特定项目的数量,它仍将显示全部/原始成本。我尝试更新sub\\u total和total字段,然后我得到了这个时髦的折扣:行项目。有什么想法吗?我是这里的正确人选吗?add_action( \'woocommerce_checkout_create_order\', \'change_total_on_checking\', 20, 1 );
function change_total_on_checking( $order ) {
// Get order total
$total = $order->get_total();
$items = $order->get_items();
foreach ($order->get_items() as $item_id => $item_data) {
// Get an instance of corresponding the WC_Product object
$product = $item_data->get_product();
$product_id = $item_data->get_id();
$product_name = $product->get_name();
$price = $product->get_price();
$item_quantity = $item_data->get_quantity();
$item_data[\'quantity\'] = $item_data[\'quantity\'] - 1 ; // Get the item quantity
//$item_data[\'total\'] = $item_data[\'total\'] - $price;
//$item_data[\'sub_total\'] = $item_data[\'sub_total\'] - $price;
$item_total = $item_data->get_total(); // Get the item line total
//do_action(\'woocommerce_add_to_order\', $item_data[\'id\'], $item_data[\'product_id\'], $item_quantity, $item_data[\'variation_id\']);
WC()->cart->add_to_cart( $product_id, $item_quantity );
$order->add_product( $product, 1);
$order->calculate_totals(); // updating totals
$order->save(); // Save the order data
}
}
add_action( \'woocommerce_thankyou\', \'woocommerce_thankyou_change_order_status\', 10, 1 );
function woocommerce_thankyou_change_order_status( $order_id ){
if( ! $order_id ) return;
$order = wc_get_order( $order_id );
$order->update_status( \'processing\' );
}