Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress 以编程方式更新订单不';t更新总数_Wordpress_Woocommerce - Fatal编程技术网

Wordpress 以编程方式更新订单不';t更新总数

Wordpress 以编程方式更新订单不';t更新总数,wordpress,woocommerce,Wordpress,Woocommerce,我正在尝试更新订购产品的数量,在这样做时,我希望订单反映实际成本。我发现所发生的是,产品成本降低到与总成本相匹配,而订单总成本从未实际更新。我在下面提供了一个简单的示例: function prefix_update_woo_order() { $order_id = 123; // This needs to be a real order or there will be errors $order_item_id = 5; // This needs to

我正在尝试更新订购产品的数量,在这样做时,我希望订单反映实际成本。我发现所发生的是,产品成本降低到与总成本相匹配,而订单总成本从未实际更新。我在下面提供了一个简单的示例:

function prefix_update_woo_order() {

    $order_id       = 123; // This needs to be a real order or there will be errors
    $order_item_id  = 5; // This needs to be a real order item ID or there will be errors.

    $order       = new WC_Order( $order_id );
    $order_items = $order->get_items();
    $order_items[ $order_item_id ]->set_quantity( 2 );

    $order->calculate_taxes();
    $order->calculate_totals();
    $order->save();

}
add_action( 'admin_init', 'prefix_update_woo_order' );
例如,一款“带徽标的豆豆”产品售价18.00美元,而我最初购买的是1。我想在下单后通过编程将订单项的数量更新为2,而不是1。我希望总数是36美元,但我发现产品成本的变化与总价相匹配。“带标志的豆豆”的价格不再是18美元,数量更新为2个,成本降低到9美元一个


简而言之,我要做的是更新现有订单项目数量,并更新总数以反映新的数量成本、折扣和税费。我需要使用什么方法来实现这一目标?

我以前没有尝试过您想要实现的目标。我在代码中看到的是,
$order\u items
是从
WC\u order::get\u items()
生成的items对象数组,但我没有看到通知WC\u order实例订单项已更改。我希望有一种方法,比如
$order->update\u cart($order\u items)我相信我找到了一些有用的链接,可以进行更多的研究


对不起,我帮不了什么忙

使用以下代码片段执行上述任务-

function prefix_update_woo_order() {

    $order_id       = 123; // This needs to be a real order or there will be errors
    $order_item_id  = 5; // This needs to be a real order item ID or there will be errors.

    $order       = wc_get_order( $order_id );
    if( $order && !wc_get_order_item_meta( $order_item_id, '_order_item_data_updated', true ) ) {
        $item_price = wc_get_order_item_meta( $order_item_id, '_line_total', true );
        $updated_item_quantity = 2;
        wc_update_order_item_meta( $order_item_id, '_qty', $updated_item_quantity );
        wc_update_order_item_meta( $order_item_id, '_line_total', $item_price * $updated_item_quantity );

        $order->calculate_totals();
        $order->save();
        // set flag
        wc_add_order_item_meta( $order_item_id, '_order_item_data_updated', true, true );
    }

}
add_action( 'admin_init', 'prefix_update_woo_order' );

代码将转到活动主题的函数。php

您好,我认为此代码将改变您的问题

add_action( 'admin_init', 'test_order_update_order' );
function test_order_update_order() {
    $order_id       = 80; // This needs to be a real order or there will be errors
    $order_item_id  = 11; // This needs to be a real order item ID or there will be errors.
    $quantity = 2;  //quantity which you want to set.
    $order       = new WC_Order( $order_id );
    $order_items = $order->get_items();
    foreach ( $order_items as $key => $value ) {
        if ( $order_item_id == $key ) {
           $product_value = $value->get_data();
           $product_id    = $product_value['product_id']; 
        }
    }
    $product = wc_get_product( $product_id );
    $price = $product->get_price();
    $price = ( int ) $quantity * $price;
    $order_items[ $order_item_id ]->set_quantity( 2 );
    $order_items[ $order_item_id ]->set_subtotal( $price );
    $order->calculate_taxes();
    $order->calculate_totals();
    $order->save();
}

这就是我想到的。我已尝试在适用的地方使用。简单地更新订单项目数量似乎需要很多工作,但事实就是如此

底部的注释是不必要的,但是如果您使用的是,那么这将解决这个问题

/**
 * Update the order item quantity and totals
 * @param Integer $order_id
 * @param Integer $order_item_id
 * @param Integer $quantity         - Quantity to set
 * 
 * @return void
 */
function prefix_update_woo_order( $order_id, $order_item_id, $quantity ) {

    // Get Order, Item, and Product Data
    $order          = new WC_Order( $order_id );
    $order_items    = $order->get_items();
    $line_item      = $order_items[ $order_item_id ];
    $variation_id   = $line_item->get_variation_id();
    $product_id     = $line_item->get_product_id();
    $product        = wc_get_product( $variation_id ? $variation_id : $product_id );
    $quantity_old   = $line_item->get_quantity();

    // Calculate Old and New Discounts
    $discount = wc_format_decimal( $line_item->get_subtotal() - $line_item->get_total(), '' );
    if( ! empty( $discount ) ) {
        $discount_per_qty = wc_format_decimal( ( $discount / $quantity_old ), '' );
        $discount = wc_format_decimal( ( $discount_per_qty * $quantity ), '' );
    }

    // Set Quantity and Order Totals
    $line_item->set_quantity( $quantity );
    $total = wc_get_price_excluding_tax( $product, array( 'qty' => $line_item->get_quantity() ) );  // Also see `wc_get_price_excluding_tax()`
    $line_item->set_subtotal( $total );             // Without Discount
    $line_item->set_total( $total - $discount );    // With Discount

    // Save Everything
    $line_item->save();
    wc_save_order_items( $order_id, $order_items );

    /**
     * If using the 'Cost of Goods' Plugin
     * - - - - - -
     * $cog         = $line_item->get_meta( '_wc_cog_item_cost', true );
     * $new_cog     = wc_format_decimal( ( $quantity * $cog ), '' );
     * $line_item->update_meta_data( '_wc_cog_item_total_cost', $new_cog );
     * wc_cog()->set_order_cost_meta( $order_id, true );
     */

}

使用类似于
$total=wc\u get\u price\u include\u tax($product,array('qty'=>$line\u item->get\u quantity())
可能是一个更好的解决方案,因为
$price
可能并不总是以整数结尾-它可能是一个浮点。