php 从Woocommerce 3中的订单项目中获取正确的变化价格

ux6nzvsh  于 2023-04-28  发布在  PHP
关注(0)|答案(3)|浏览(103)

我试图得到正确的价格为每个项目的变化,但它似乎只是得到第一个价格的产品变化。不知道如何解决这个问题。
验证码:

$query = new WC_Order_Query( array(
        'status' => 'on-hold',
        'orderby' => 'date',
        'order' => 'DESC',
        'return' => 'ids',
    ) );
    $order_ids = $query->get_orders();

    foreach( $order_ids as $order_id ) {

        $order = new WC_Order($order_id);

        foreach ($order->get_items() as $item_id => $item_obj) {
            $_product = wc_get_product($item_obj['product_id']);
            $product = new WC_Product_Variable($item_obj['product_id']);
            $product_variations = $product->get_available_variations();

            $variation_product_id = $product_variations [0]['variation_id'];

            $variation_product = new WC_Product_Variation( $variation_product_id );
            $t_dy =  $variation_product->get_price();
            $item_qty = $item_obj['qty'];
            $it_total = $item_qty * $t_dy;
            $td = wc_update_order_item_meta($item_id, '_line_total', $it_total);
            $order->calculate_totals();
            $order->save();
        }

    }
j2cgzkjk

j2cgzkjk1#

    • 已更新3**

代码:

// Loop through order items
foreach ($order->get_items() as $item_id => $item ) {

    // Targeting only product variation items
    if( $item->get_variation_id() > 0 ){ 

        // Get an instance of the WC_Product_Variation object
        $product = $item->get_product(); 

        $price   = (float) $product->get_price(); // <=== HERE the variation price

        $qty     = (int) $item->get_quantity(); // <=== HERE the quantity

        // set line totals
        $item->set_total( $price * $qty );
        $item->set_subtotal( $price * $qty );

        $item->save(); // save order item data
    }
}

// The following need to be outside the order item loop
$order->calculate_totals(); // Save is included into the method

最好是这样。
相关:

  • 在Woocommerce 3中获取订单项目和WC_Order_Item_Product
  • 如何获取WooCommerce订单详情
093gszye

093gszye2#

找到问题了!- 它给出了错误的ID替换:

$variation_product_id = $product_variations [0]['variation_id'];

用这个:

$product_variation_id = $item_obj->get_variation_id();
$variation_product_id = $product_variation_id;
$variation_product = new WC_Product_Variation( $variation_product_id );
bakd9h0s

bakd9h0s3#

这是我现在运行的代码,我还发现这是互联网上唯一一个讨论这方面代码的地方,考虑到woocommerce是多么受欢迎,这让我有些惊讶。要么人们不分享,要么它对每个人来说都很明显。(接下来:获得货币,计算汇率和正确的行项目税率,这将是有趣的)

foreach ($order_data['line_items'] as $order_line_item) {
        $product_id = $order_line_item['product_id'];
        $product = wc_get_product($product_id);
        $variation_id = $order_line_item['variation_id'];
        if($variation_id !== 0){
            $variation = new WC_Product_Variation( $variation_id );
            $regular_price = $variation->get_regular_price();
            $is_virtual = $variation->is_virtual();
            $is_downloadable = $variation->is_downloadable();
        } else {
            $regular_price = $product->get_regular_price();
            $is_virtual = $product->is_virtual();
            $is_downloadable = $product->is_downloadable();
        }
    }

正如您所看到的,当您执行print_r($order_data)或var_dump($order_data)时,您可以清楚地看到每一行都有一个变体ID,该ID针对客户添加到其购物车并 checkout 的每一个订单行存储。这是您的客户在订购时选择的变体。

[line_items] => Array
    (
        [0] => Array
            (
                [id] => 32500
                [name] => Test Product Name
                [product_id] => 66
                [variation_id] => 26077
                [quantity] => 1
                [tax_class] => 
                [subtotal] => 279.45
                [subtotal_tax] => 0.00
                [total] => 279.45
                [total_tax] => 0.00
                [taxes] => Array
                    (
                        [0] => Array
                            (
                                [id] => 1
                                [total] => 0
                                [subtotal] => 0
                            )

                    )

相关问题