在Woocommerce电子邮件通知中按SKU对订单商品进行排序

时间:2018-04-26 15:00:48

标签: php sorting woocommerce orders email-notifications

我正在尝试在电子邮件中通过sku订购产品。在 Woocommerce

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

有些帮助吗?谢谢你们!

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

2 个答案:

答案 0 :(得分:2)

以下是这样做的方法:

add_filter( 'woocommerce_order_get_items', 'filter_order_get_items', 10, 3 );
function filter_order_get_items( $items, $order, $types = 'line_item' ) {
    // Check items type: Only "line_item" types for Woocommerce version 3.3+
    if( $types != 'line_item') return $items;

    $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(); //
            $item_skus[$product->get_sku()] = $items_id;
        }
    }
    // Check items type: for versions before Woocommerce 3.3 (2)
    if( count($item_skus) == 0 ) return $items;

    // 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];
    }
    return $sorted_items;
}

代码放在活动子主题(或活动主题)的function.php文件中。经过测试和工作。

  

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

在将数据保存到数据库之前放置订单后对项目进行排序可能是更好的方法。

答案 1 :(得分:0)

除了@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;
}
相关问题