在 WooCommerce 订单编辑页面中按“菜单顺序"对订单项进行排序 [英] Sort Order Items by “menu order” in WooCommerce order edit pages

查看:83
本文介绍了在 WooCommerce 订单编辑页面中按“菜单顺序"对订单项进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此功能按菜单顺序对 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);函数 custom_woocommerce_order_get_items($items, $object){//如果少于2个产品不需要重新排序if(count($items) <2)返回 $items;//创建订单内的产品列表$products = array();foreach($items as $key => $item){$products[ $item['product_id'] ] = $key;}$sorted_items = array();全球 $post;$args = 数组('posts_per_page' =>-1,'post_type' =>'产品','orderby' =>'menu_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] ];}//检查是否有任何遗留在外面的项目foreach($items as $key => $item){if(isset($sorted_items[$key]))$sorted_items[ $key ] = $item;}返回 $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 );函数 filter_order_get_items( $items, $order ){//如果少于 2 个项目,则无需重新排序if(count($items) <2) 返回 $items;$sorted_items = $products_items_ids = array();//获取订单中包含商品 ID 的产品/变体 ID 数组foreach( $items as $item_id => $item ){//获取产品ID(添加WC 3+兼容性)$product_id = method_exists( $item, 'get_product_id' ) ?$item->get_product_id() : $item['product_id'];//获取变体 ID(添加 WC 3+ 兼容性)$variation_id = method_exists( $item, 'get_variation_id' ) ?$item->get_variation_id() : $item['variation_id'];如果( $variation_id > 0 )$product_id = $variation_id;$products_items_ids[ $product_id ] = $item_id;}//基于此订单的产品 ID 的 WP 查询$query = new WP_Query( 数组('posts_per_page' =>-1,'post_type' =>array( 'product', 'product_variation' ),//<== 这里缺少'orderby' =>'menu_order','订单' =>'ASC','post__in' =>array_keys( $products_items_ids ),) );//在查询中循环if( $query->have_posts() ){while( $query->have_posts() ): $query->the_post();//获取帖子ID$post_id = $query->post->ID;//获取当前商品ID对应的商品ID$item_id = $products_items_ids[ $post_id ];//获取新排序的项目数组$sorted_items[$item_id] = $items[$item_id];终了;}wp_reset_query();返回 $sorted_items;}

代码位于活动子主题(或活动主题)的 function.php 文件或任何插件文件中.

经过测试并适用于所有产品,包括 WooCommerce v2.5.x 到 v3.2+ 上的产品变体

I use this function to sort Woocommerce order admin items by menu order but But products with variables do not display properly. And if there are several product with variables in the order, only one of them will be displayed.

edit: we have problem with multiple items of a product with different attributes:

item1: product A,variable a,attribute: red color, qty 12

item2: Product A, variable a, attribute: green color, qty 18

after sort it only shows :

item1: product A,variable a,attribute: red color, qty 12

In other words product items with same variation id have problem.

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;

   }

解决方案

Updated: (to include variations IDs when product it's a variable product)

The main issues in your code is in the query where you need to get the also product_variation post type and also in the first loop where you need to get the variation ID for variable products.

Also this code is outdated for WooCommerce 3+ as Order items are now a WC_Order_Item_Product Object and you need to use the available methods of this class instead.

You don't need the global $post; object as it's already as an argument in the function.

I have revisited all your code:

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;
}

Code goes in function.php file of your active child theme (or active theme) or in any plugin file.

Tested and works for all products including product variations on WooCommerce v2.5.x to v3.2+

这篇关于在 WooCommerce 订单编辑页面中按“菜单顺序"对订单项进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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