通过 WooCommerce 中的挂钩函数对订单项进行排序 [英] Sorting order items via a hooked function in WooCommerce

查看:80
本文介绍了通过 WooCommerce 中的挂钩函数对订单项进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 WooCommerce 中,我有一个位置"的产品属性 pa_location,我用它对我的订单商品进行排序.当订单商品的位置相同时,我想按 SKU 对它们进行排序.因此,请先按位置排序,然后按 SKU 排序.

我查看了"PHP 对多个字段进行排序",但我无法弄清楚它是如何应用的,以及如何适用于我的功能.

我有这个自定义函数可以按 pa_location 排序,但不能让它也按 _sku 排序.

我尝试实现参考文章中关于排序的一些示例.

add_filter( 'woocommerce_order_get_items', function( $items, $order ) {uasort( $items,函数($a,$b){return strnatcmp( $a['pa_location'], $b['pa_location'] );});返回 $items;}, 10, 2 );

我希望使用此功能按位置然后按 SKU 排序.现在它只是按位置排序.

解决方案

尝试以下基于 "PHP 对多个字段进行排序排序" 回答线程:

add_filter( 'woocommerce_order_get_items', 'filter_order_get_items_callback', 10, 2 );函数 filter_order_get_items_callback( $items, $order ) {uasort( $items,函数($a,$b){if( $a['pa_location'] == $b['pa_location'] ) {返回 strcmp( $a['_sku'], $b['_sku'] );}return strnatcmp( $a['pa_location'], $b['pa_location'] );});返回 $items;}

<小时>

测试:

$items = 数组 (0 =>数组('pa_location' => '马赛','_sku' =>'27458C'),1 =>数组('pa_location' => '不错','_sku' =>'32458A'),2 =>数组('pa_location' => '戛纳','_sku' =>'35C7L5'),3 =>数组('pa_location' => '马赛','_sku' =>'387B85'),4 =>数组('pa_location' => '戛纳','_sku' =>'25A587'));uasort( $items, function( $a, $b ) {if( $a['pa_location'] == $b['pa_location'] ) {返回 strcmp( $a['_sku'], $b['_sku'] );}return strnatcmp( $a['pa_location'], $b['pa_location'] );});//原始输出echo '

';print_r( $items );echo '

';

我们得到这个输出:

数组([2] =>大批([pa_location] =>戛纳[_sku] =>25A587)[4] =>大批([pa_location] =>戛纳[_sku] =>35C7L5)[0] =>大批([pa_location] =>马赛[_sku] =>27458C)[3] =>大批([pa_location] =>马赛[_sku] =>387B85)[1] =>大批([pa_location] =>好的[_sku] =>32458A))

这是我们所期望的,先排序 pa_location,然后是 _sku.

In WooCommerce, I have a product attribute pa_location for "location" with which I sort my order items. When order items have the same location, I'd like to sort them by SKU. So, sort first by location and then by SKU.

I looked to "PHP usort sorting multiple fields" but I am unable to figure out how it applies and would work for my function.

I have this custom function that works to sort by pa_location, but cannot get it to also sort by _sku.

I tried implementing some of the examples in the referenced post about sorting.

add_filter( 'woocommerce_order_get_items', function( $items, $order ) {

    uasort( $items,
        function( $a, $b ) {
            return strnatcmp( $a['pa_location'], $b['pa_location'] );
        }
    );

    return $items;
}, 10, 2 );

I hope to sort by location then by SKU using this function. Right now it is only sorting by location.

解决方案

Try the following based on "PHP usort sorting multiple fields" answers thread:

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

  return $items;

}


TESTING:

$items = array (
    0 => array ( 'pa_location' => 'Marseille',
        '_sku' => '27458C' ),
    1 => array ( 'pa_location' => 'Nice',
        '_sku' => '32458A' ),
    2 => array ('pa_location' => 'Cannes',
        '_sku' => '35C7L5' ),
    3 => array ('pa_location' => 'Marseille',
        '_sku' => '387B85'),
    4 => array ( 'pa_location' => 'Cannes',
        '_sku' => '25A587' )
);

uasort( $items, function( $a, $b ) {
    if( $a['pa_location'] == $b['pa_location'] ) {
        return strcmp( $a['_sku'], $b['_sku'] );
    }
    return strnatcmp( $a['pa_location'], $b['pa_location'] );
} );
// Raw output
echo '<pre>'; print_r( $items ); echo '<pre>';

We get this output:

Array
(
    [2] => Array
        (
            [pa_location] => Cannes
            [_sku] => 25A587
        )
    [4] => Array
        (
            [pa_location] => Cannes
            [_sku] => 35C7L5
        )
    [0] => Array
        (
            [pa_location] => Marseille
            [_sku] => 27458C
        )
    [3] => Array
        (
            [pa_location] => Marseille
            [_sku] => 387B85
        )
    [1] => Array
        (
            [pa_location] => Nice
            [_sku] => 32458A
        )
)

Which is what we expect, sorting first pa_location and after _sku.

这篇关于通过 WooCommerce 中的挂钩函数对订单项进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆