【问题标题】:Sorting order items by SKU in Woocommerce在 Woocommerce 中按 SKU 对订单商品进行排序
【发布时间】:2018-10-07 08:19:55
【问题描述】:

我正尝试在 Woocommerce在电子邮件中的订单中按 sku 订购产品

我对以下代码没有任何运气。

一些帮助?从现在开始谢谢!

add_filter( 'woocommerce_order_get_items', function( $items, $order ) {
    uasort( $items,
        function( $a, $b ) {
            return strnatcmp( $a['_sku'], $b['_sku'] );
        }
    );
    return $items;
}, 10, 2 );

样本排序结果:

  • INFSTRAW I
  • NFMUFFIN
  • INFFLORES
  • INFTAFLOR
  • INFTAPINK
  • CTEPINK4
  • CTECAKE4
  • INFCHOCO
  • UCUBTOMA

【问题讨论】:

  • “我没有运气” 不是问题描述。为什么不?发生了什么,而不是你想要发生的?
  • 抱歉没有解释清楚。我添加了它们如何排序的细节......

标签: php sorting woocommerce orders email-notifications


【解决方案1】:

2020 年 7 月更新

方法如下:

add_filter( 'woocommerce_order_get_items', 'filter_order_get_items_by_sku', 10, 3 );
function filter_order_get_items_by_sku( $items, $order, $types ) {
    if( count($items) > 1 ) {
        $item_skus = $sorted_items = array();

        // Loop through order line items
        foreach( $items as $items_id => $item ){
            // Check items type: for versions before Woocommerce 3.3
            if( $item->is_type('line_item') && method_exists( $item, 'get_product' ) ){
                $product = $item->get_product(); // Get the product Object
                if( is_a( $product, 'WC_Product' ) ) {
                    $item_skus[$product->get_sku()] = $items_id;
                }
            }
        }

        // Only for line items when our sku array is not empty
        if( ! empty($item_skus) ) {
            // Sorting in ASC order based on SKUs;
            ksort($item_skus); // or use krsort() for DESC order

            // Loop through sorted $item_skus array
            foreach( $item_skus as $sku => $item_id ){
                // Set items in the correct order
                $sorted_items[$item_id] = $items[$item_id];
            }
            $items = $sorted_items;
        }
    }
    return $items;
}

代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。

这将在后端和前端订单以及电子邮件通知中按 sku 订单 ASC 对项目进行排序


在将数据保存到数据库之前下订单后也对项目进行排序,这可能是一种更好的方法。

【讨论】:

  • 我在一些旧订单中收到此错误:在布尔值上调用成员函数 get_sku() 知道吗?谢谢
  • @jpussacq 这意味着您的数据库没有正确更新 WC 版本 3+,在这个旧订单上。
  • 我能做什么??
  • @jpussacq 我还没有遇到过这个问题,所以我现在没有答案。这些订单有多长时间了?
  • 有些东西很奇怪。测试环境没有产生错误,是生产过程的拷贝。
【解决方案2】:

除了@LoicTheAztec,我还在代码中添加了以下检查:

if ($product instanceof WC_Product) {
                    $item_skus[$product->get_sku()] = $items_id;
                }

这样可以防止前端客户订单页面出现错误。

add_filter( 'woocommerce_order_get_items', 'filter_order_get_items_by_sku', 10, 3 );
function filter_order_get_items_by_sku( $items, $order, $types ) {
    if( count($items) > 1 ) {
        $item_skus = $sorted_items = array();

        // Loop through order line items
        foreach( $items as $items_id => $item ){
            // Check items type: for versions before Woocommerce 3.3
            if( $item->is_type('line_item') ){
                $product = $item->get_product(); // Get the product Object
                
                if ($product instanceof WC_Product) {
                    $item_skus[$product->get_sku()] = $items_id;
                }
            }
        }

        // Only for line items when our sku array is not empty
        if( ! empty($item_skus) ) {
            // Sorting in ASC order based on SKUs;
            ksort($item_skus); // or use krsort() for DESC order

            // Loop through sorted $item_skus array
            foreach( $item_skus as $sku => $item_id ){
                // Set items in the correct order
                $sorted_items[$item_id] = $items[$item_id];
            }
            $items = $sorted_items;
        }
    }
    return $items;
}

【讨论】:

    猜你喜欢
    • 2019-07-14
    • 2020-10-16
    • 2015-03-19
    • 1970-01-01
    • 2021-08-13
    • 1970-01-01
    • 1970-01-01
    • 2018-05-09
    • 1970-01-01
    相关资源
    最近更新 更多