【问题标题】:Count for complete quantity of all order items in WooCommerce计算 WooCommerce 中所有订单商品的完整数量
【发布时间】:2020-10-26 14:53:01
【问题描述】:

我有一个问题,我需要完整数量的商店商品才能进行 for 循环。

我已经试过了:

$total_quantity += $item->get_quantity()
$total_quantity = $order->get_item_count();
$items_count = count( $order->get_items() );

很遗憾没有成功。

这是我的代码:

foreach ($order->get_items() as $key => $item) {
        $total_quantity = $order->get_item_count(); <-- NOT WORKING
    for ( $i = 0; $i < $total_quantity; $i++ ) {
        $content .= '<button class="accordion">' . $item->get_name() . '</button>';
  }
}

foreach 循环中的 for 循环应该会导致生成 PDF 按钮。根据您按下的 PDF 按钮,索引将传输到 JavaScript 并检查刚刚单击了哪个按钮。

使用 $ item-> get_quantity 它几乎可以工作。但是,对于另一个产品,索引 ($i) 重置为 0。然而,它必须继续下去。这意味着,或者如果产品被放入购物车 5 次,而另一个产品只被放入购物车一次,那么按钮首先正确显示 5 次,索引正确,而不是第二个产品。

如果您对代码有任何疑问或需要示例,请告诉我,我会更新这篇文章。

【问题讨论】:

    标签: php wordpress woocommerce orders product-quantity


    【解决方案1】:

    对于订购商品,请在 FOR 循环中使用WC_Order_Item_Product get_quantity() 方法,如下所示:

    $content = ''; // Initializing
    
    // Loop through order items
    foreach ( $order->get_items() as $item_id => $item ) {
        // Loop through item quantity
        for ( $i = 1; $i <= $item->get_quantity(); $i++ ) {
            $content .= '<button class="accordion">' . $item->get_name() . ' (' . $i . ')</button>';
        }
    }
    

    现在应该可以工作了……


    要计算您可以使用的完整全球订单数量:

    $total_quantity = 0; // Initializing
    
    // Loop through order items
    foreach ( $order->get_items() as $item_id => $item ) {
        $total_quantity += $item->get_quantity();
    }
    // Output
    echo '<p>Total items quantity: '.$total_quantity.'</p>';
    

    【讨论】:

    • 感谢您昨天的回答和帮助。这完全解决了。但我现在有另一个问题。对于每个订单项目,我需要按钮的项目名称。此外,我需要为我放置在代码中的每个按钮设置正确的项目名称。但是,每个项目都需要一个唯一的类名。我追求的目标是下载 PDF - File foreach order item。此外,每个订单都需要分配给用户。这就是为什么我需要这么多帮助。如果你愿意,我可以更新我的代码或为这个问题打开另一个问题?对此我有点不知所措。
    猜你喜欢
    • 1970-01-01
    • 2019-08-31
    • 2021-02-07
    • 2017-04-04
    • 2021-01-07
    • 2021-05-21
    • 1970-01-01
    • 2021-04-02
    • 1970-01-01
    相关资源
    最近更新 更多