按WooCommerce订单编辑页面中的“菜单顺序”对订单商品进行排序

时间:2017-11-26 10:30:30

标签: php wordpress woocommerce orders variations

我使用此功能按菜单顺序对Woocommerce订单管理项目进行排序,但是带有变量的产品无法正常显示。如果订单中有多个带变量的产品,则只会显示其中一个。

编辑:我们对具有不同属性的产品的多个项目存在问题:

item1:产品A,变量a,属性:红色,数量12

第2项:产品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;

   }

1 个答案:

答案 0 :(得分:1)

已更新:(在产品变量产品时包含变体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 +上的产品变体

相关问题