【问题标题】:WooCommerce get order item single price (without quantity multiplication)WooCommerce 获取订单商品单价(无数量乘法)
【发布时间】:2021-01-07 20:52:50
【问题描述】:

我正在寻找一种在 WooCommerce 中获取订单商品单一价格的方法。我在这里关注了这篇文章并使用了get_price() 方法,但这种方法似乎不再可用:

Woocommerce - Getting the order item price and quantity.

foreach ( $order_items as $order_item ) {
    error_log( $order_item->get_price() );
    error_log( print_r( $order_item, true ) );
}

未捕获的错误:调用未定义的方法 WC_Order_Item_Product::get_price()

问题是我不能只拿到产品并在那里读取正常价格,因为我需要在下订单期间设置价格,并且产品价格可以在以后多次更改。

我还打印了整个订单项目以找到那里的单个价格字段,但什么也没找到:

[data:protected] => Array
    (
        [order_id] => 24
        [name] => Dings Teil
        [product_id] => 23
        [variation_id] => 0
        [quantity] => 2
        [tax_class] => 
        [subtotal] => 42.4
        [subtotal_tax] => 6.78
        [total] => 42.4
        [total_tax] => 6.78
        [taxes] => Array
            (
                [total] => Array
                    (
                        [6] => 6.784
                    )
                [subtotal] => Array
                    (
                        [6] => 6.784
                    )
            )
    )

总而言之,我需要以某种方式从我的订单商品中获得单一价格。 WooCommerce 似乎有办法在订单项视图中获取它,但我找不到他们处理此问题的方式:

因为我正在编写一个插件,所以对 WooCommerce 的任何更改都不是一个好主意。

更新:

是的,我也有过将小计除以数量的想法,但如果我的舍入不像 WooCommerce 舍入那样 100%,这可能会导致一些舍入问题。

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    get_price() 类使用方法 get_price()... 请改用以下方法:

    foreach ( $order->get_items() as $item ) {
        $product = $item->get_product(); // Get the WC_Product Object
        $price   = $product->get_price();
        error_log( $price );
        error_log( print_r( $order_item, true ) );
    }
    

    它应该更好地工作。


    您还可以使用以下方法(将项目小计除以数量):

    foreach ( $order->get_items() as $item ) {
        $quantity      = $item->get_quantity(); // Quantity
        $subtotal     = $item->get_subtotal(); // Line subtotal
        $subtotal_tax = $item->get_subtotal_tax(); // Line subtotal tax
    
        $price_excl_tax = $subtotal / $quantity;
        $price_incl_tax = ( $subtotal + $subtotal_tax ) / $quantity
        error_log( 'Price without tax: ' . $price_excl_tax );
        error_log( 'Price with tax: ' . $price_incl_tax );
    }
    

    【讨论】:

    • 因为产品价格可以变动,所以我决定采用较低的方案(除以数量)。
    • 您知道是否有办法获得鳕鱼成本吗?如果是的话,我会问一个关于这个的问题。我只知道 get_fees() 方法,但成本取决于商店语言,这使得获得正确的费用非常复杂......
    • 没有COD成本,但是为了更干净有效,您可以克隆扩展WC_Payment_gatewayas shown in this thread (in a custom plugin)的COD支付网关,另请参见this related thread...然后您可以更改为这个克隆的支付网关,包括根据您需要的任何费用,但这是先进的。
    【解决方案2】:

    WooCommerce 在订单的管理视图中自行执行此操作:

    $order->get_item_subtotal( $item, false, true )
    

    以下是如何调整代码以在不增加数量的情况下获得商品的单一价格:

    foreach ( $order_items as $order_item ) {
        $order = $order_item->get_order(); // Returns WC_Order object
        error_log( $order->get_item_subtotal( $order_item, false, true ) ); // Returns item price without multiplying by quantity.
    }
    

    参见:woocommerce/includes/admin/meta-boxes/views/html-order-item.php

    【讨论】:

      猜你喜欢
      • 2017-04-04
      • 2021-04-02
      • 2017-09-17
      • 2019-02-23
      • 1970-01-01
      • 2019-08-31
      • 2018-10-14
      • 2018-01-24
      • 2019-03-24
      相关资源
      最近更新 更多