【问题标题】:Get non formatted value from $order->get_formatted_line_subtotal() in WooCommerce从 WooCommerce 中的 $order->get_formatted_line_subtotal() 获取非格式化值
【发布时间】:2021-02-16 23:50:23
【问题描述】:

我需要对文件 order-details-item.php 进行某些更改 产品价格在自定义元字段中形成。所以在我的情况下,价值是: $qty = $item->get_quantity();是不正确的。它总是一样的。

为了解决这个问题,我可以使用最简单的arephmetic操作。将总订单价格除以产品价格。例如,如果客户以每公斤 14.5 的成本订购了 10 公斤苹果,那么总成本将为 145。这意味着为了正确显示数量,我需要 145/10。

    $price_weight_array = $product_attr['_mia_cup_price_weight'];
    $price_unit_array = $product_attr['_mia_cup_price_unit'];
    $sale_price_array = $product_attr['_mia_cup_sale_price_unit'];  
    
    $price_weight = $price_weight_array[0];
    $price_unit   = $price_unit_array[0];
    $sale_price = $sale_price_array[0];
    $prod_item_total = $order->get_formatted_line_subtotal();

    custom_quantity = $prod_item_total / $price_weight

这就是问题 $order->get_formatted_line_subtotal();返回我一个带有货币符号的数字。而且我不能在算术运算中使用它。如何删除此符号?

【问题讨论】:

  • 第一个代码不完整且不可重现,请记住您的 " 问题应更新以包含所需的行为、特定问题或错误以及重现问题所需的最短代码".此外,get_formatted_line_subtotal() 方法需要一个强制参数$item,即WC_Order_Item_Product 对象,因此在您的代码中它会引发错误……对于订单“行”项目,请参阅Get Order items and WC_Order_Item_Product in WooCommerce 3

标签: php wordpress woocommerce orders price


【解决方案1】:

您的代码不完整,并且您没有使用正确的方法......另外get_formatted_line_subtotal() method 需要一个强制参数才能工作,并且您知道显示格式化订单项目小计(货币符号)

基于 How to get WooCommerce order detailsGet Order items and WC_Order_Item_Product in WooCommerce 3,您需要先获取订单商品,然后在 foreach 循环中使用您的代码。

要获得非格式化和非四舍五入的订单项小计,您将改用get_line_subtotal() 方法,如下所示:

$order = wc_get_order($order_id); // (optional - if needed) get the WC_Order object

// Loop through order items
foreach( $order->get_items() as $item_id => $item ) {
    $product = $item->get_product(); // get the WC_Product Object

    // Your other code (missing from your question) Here …

    $price_weight    = reset($product_attr['_mia_cup_price_weight']);
    $price_unit      = reset($product_attr['_mia_cup_price_unit']);
    $sale_price_unit = reset($product_attr['_mia_cup_sale_price_unit']); 

    // Get the non formatted order item subtotal (and not rounded)
    $item_subtotal   = $order->get_line_subtotal( $item, $order->get_prices_include_tax(), false );

    $custom_quantity = $item_subtotal / $price_weight;
}

应该会更好

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-11
    • 1970-01-01
    • 1970-01-01
    • 2017-09-13
    • 2019-06-27
    • 1970-01-01
    相关资源
    最近更新 更多