在 Woocommerce 中按 SKU 对订单项目进行排序
Sorting order items by SKU in Woocommerce
我正在尝试 在 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 我
- NFMUFFIN
- 因弗洛雷斯
- INFTAFLOR
- INFTAPINK
- CTEPINK4
- CTECAKE4
- INFCHOCO
- UCUBTOMA
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 文件。已测试并有效。
This will sort items by sku order ASC on backend and frontend orders and in email notifications
在将数据保存到数据库之前下订单后对项目进行排序可能是更好的方法。
除了@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;
}
我正在尝试 在 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 我
- NFMUFFIN
- 因弗洛雷斯
- INFTAFLOR
- INFTAPINK
- CTEPINK4
- CTECAKE4
- INFCHOCO
- UCUBTOMA
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 文件。已测试并有效。
This will sort items by sku order ASC on backend and frontend orders and in email notifications
在将数据保存到数据库之前下订单后对项目进行排序可能是更好的方法。
除了@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;
}