Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.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
Php WooCommerce获取订单项目单一价格(无数量乘法)_Php_Wordpress_Woocommerce - Fatal编程技术网

Php WooCommerce获取订单项目单一价格(无数量乘法)

Php WooCommerce获取订单项目单一价格(无数量乘法),php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我正在寻找一种方法,以获得单一价格的订单项目在WooCommerce。我在这里关注了这篇文章,并使用了get\u price()方法,但该方法似乎不再可用: 未捕获错误:调用未定义的方法 WC\订单\项目\产品::获取\价格() 问题是,我不能仅仅得到产品并在那里读取正常价格,因为我需要在下单时设置价格,并且产品价格可以在以后多次更改 我还打印了整个订单项目,以便在那里找到单个价格字段,但没有找到任何内容: [data:protected] => Array (

我正在寻找一种方法,以获得单一价格的订单项目在WooCommerce。我在这里关注了这篇文章,并使用了
get\u price()
方法,但该方法似乎不再可用:

未捕获错误:调用未定义的方法 WC\订单\项目\产品::获取\价格()

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

我还打印了整个订单项目,以便在那里找到单个价格字段,但没有找到任何内容:

[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似乎有办法在order items视图中找到它,但我找不到他们处理此问题的方法:

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

更新:

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

方法
get\u price()
WC\u产品
类使用……请使用以下方法:

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 ) );
}
它应该更有效


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


因为产品价格可能会变化,我决定采用较低的解决方案(除以数量)。你知道有没有办法获得cod成本?如果是的话,我会就此提出一个问题。我只知道get_fees()方法,但成本取决于商店语言,这使得获得正确的费用非常复杂…没有COD成本,但为了更干净和有效,您可以克隆COD支付网关扩展
WC_支付网关
Class,另请参阅…然后您可以对这个克隆的支付网关进行更改,以根据您需要的任何内容包含成本,但这是一个高级功能。
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 );
}