我使用此功能按菜单顺序对 Woocommerce 订单管理项目进行排序,但带有变量的产品无法正确显示。如果订单中有多个带有变量的产品,则只显示其中一个。
编辑:我们对具有不同属性的产品的多个项目有问题:
item1:产品A,变量a,属性:红色,数量12
item2:产品A,变量a,属性:绿色,数量18
排序后只显示:
item1:产品A,变量a,属性:红色,数量12
换句话说,具有相同变体 ID 的产品项目有问题。
add_filter('woocommerce_order_get_items', 'custom_woocommerce_order_get_items', 10, 2);
function custom_woocommerce_order_get_items($items, $object)
{
//no need to reorder if less than 2 products
if(count($items) < 2)
return $items;
//create a list of products within the order
$products = array();
foreach($items as $key => $item)
{
$products[ $item['product_id'] ] = $key;
}
$sorted_items = array();
global $post;
$args = array(
'posts_per_page' => -1,
'post_type' => 'product',
'orderby' => 'menu_order',
'order' => 'ASC',
'post__in' => array_keys($products)
);
$custom_query = new WP_Query($args);
while($custom_query->have_posts())
{
$custom_query->the_post();
$sorted_items[ $products[$post->ID] ] = $items[ $products[$post->ID] ];
}
//check for any left outside items
foreach($items as $key => $item)
{
if(isset($sorted_items[$key]))
$sorted_items[ $key ] = $item;
}
return $sorted_items;
}
最佳答案
已更新:(当产品是可变产品时包含变体 ID)
您的代码中的主要问题是在查询中您还需要获取 product_variation
帖子类型,并且在第一个循环中您需要获取可变产品的变体 ID。
此外,此代码对于 WooCommerce 3+ 已过时,因为订单项现在是 WC_Order_Item_Product
对象,您需要改用此类的可用方法。
您不需要 global $post;
对象,因为它已经作为函数中的参数。
我已经重新访问了您的所有代码:
add_filter( 'woocommerce_order_get_items', 'filter_order_get_items', 10, 2 );
function filter_order_get_items( $items, $order ){
// no need to reorder if less than 2 items
if(count($items) < 2) return $items;
$sorted_items = $products_items_ids = array();
// Get the array of product/variation IDs with Item IDs within the order
foreach( $items as $item_id => $item ){
// Get the product ID (Added WC 3+ compatibility)
$product_id = method_exists( $item, 'get_product_id' ) ? $item->get_product_id() : $item['product_id'];
// Get the variation ID (Added WC 3+ compatibility)
$variation_id = method_exists( $item, 'get_variation_id' ) ? $item->get_variation_id() : $item['variation_id'];
if( $variation_id > 0 )
$product_id = $variation_id;
$products_items_ids[ $product_id ] = $item_id;
}
// The WP Query based on the product Ids from this order
$query = new WP_Query( array(
'posts_per_page' => -1,
'post_type' => array( 'product', 'product_variation' ), // <== HERE MISSING
'orderby' => 'menu_order',
'order' => 'ASC',
'post__in' => array_keys( $products_items_ids ),
) );
// Loop in the Query
if( $query->have_posts() ){
while( $query->have_posts() ): $query->the_post();
// Get the post ID
$post_id = $query->post->ID;
// Get the corresponding item ID for the current product ID
$item_id = $products_items_ids[ $post_id ];
// Get the new sorted array of items
$sorted_items[$item_id] = $items[$item_id];
endwhile;
}
wp_reset_query();
return $sorted_items;
}
代码位于事件子主题(或事件主题)的 function.php 文件或任何插件文件中。
测试并适用于所有产品,包括 WooCommerce v2.5.x 到 v3.2+ 上的产品变体
关于php - 在 WooCommerce 订单编辑页面中按 “menu order” 对订单项目进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47495141/