【问题标题】:WooCommerce Add item to Order with item order metaWooCommerce 使用项目订单元将项目添加到订单
【发布时间】:2021-02-12 08:50:08
【问题描述】:

我想删除一个现有的 WooComerce 项目并从中添加 x 个新的 WooComerce 项目。这取决于$ item->数量有多高。

我的代码如下所示:


add_action( 'woocommerce_thankyou', 'my_change_status_function', 10, 1 );

function my_change_status_function( $order_id ) {

    $order = new WC_Order( $order_id );
    foreach ( $order->get_items() as $item_id => $item ) {
        if ( $item->get_quantity() > 1 ) {
            wc_delete_order_item( $item_id );
            for ( $i = 1; $i <= $item->get_quantity(); $i++ ) {
                $new_item_ids = woocommerce_add_order_item(
                    $order_id,
                    array(
                        'order_item_name' => $item->get_name() . '_' . $i,
                        'order_item_type' => 'line_item',
                    )
                );

                if ( $new_item_ids ) {

                    foreach ( $metavalues as $key => $value ) {
                        wc_add_order_item_meta( $new_item_ids, $key, $value );
                    }
                }
            }
        }
    }
}

我有点绝望的地方在这里:


if ( $new_item_ids ) {

    foreach ( $metavalues as $key => $value ) { <-- I dont know where i get the $metavalues
    wc_add_order_item_meta( $new_item_ids, $key, $value );
    }
}

我已经尝试过 Stackoverflow 的这篇文章:

WooCommerce Add item to Order

有人可以帮我吗?

提前谢谢你

【问题讨论】:

    标签: php wordpress methods woocommerce orders


    【解决方案1】:

    自从 WooCommerce 3 以来,您的代码已经过时,可以使用一些 WC_Abstract_Order 方法进行简化。此外,您最好使用woocommerce_checkout_order_created 挂钩,它会在订单创建后触发:

    add_action( 'woocommerce_checkout_order_created', 'separate_order_items_by_quantity' );
    function separate_order_items_by_quantity( $order ) {
        $items_processed = false; // Initializing
    
        // Loop through order items
        foreach ( $order->get_items() as $item_id => $item ) {
            $quantity = $item->get_quantity(); // Get quantity
            $product  = $item->get_product(); // Get the WC_Product Object
    
            // When item has quatity more than 1, separete items by quantity
            if ( $item->get_quantity() > 1 ) {
                $order->remove_item( $item_id ); // Remove item
                $items_processed = true; // Flag it
    
                // Loop through quantity
                for ( $i = 1; $i <= $quantity; $i++ ) {
                    // Add the same productb (separated) n times the quantity
                    $new_item_id = $order->add_product( $product, 1, array(
                        '_custom_index' => $i, // (required) to have separated products
                        'name'          => $product->get_name() . ' ' . $i, // (optional) change product name appending the index
                        'Some meta key' => 'some meta value', // (optional) Here you can set a custom meta key and its value (custom order item meta data) … To hide it from customer the metakey should start with an underscore.
                    ) );
                }
            }
        }
    
        if( $items_processed ) {
            $order->apply_changes(); // Merge changes with data and clear
            $order->save(); // Save data
        }
    }
    

    代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。

    【讨论】:

    • 另外,你能告诉我我正在尝试的逻辑是否有用吗?当我的代码有点不堪重负时,请告诉我。我总是在这里了解更多信息。
    猜你喜欢
    • 2021-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-12
    • 2023-03-27
    • 1970-01-01
    • 2021-06-09
    • 1970-01-01
    相关资源
    最近更新 更多