【问题标题】:Sort order items by SKU in Woocommerce admin order pages [duplicate]在 Woocommerce 管理订单页面中按 SKU 对订单商品进行排序 [重复]
【发布时间】:2019-07-14 19:50:25
【问题描述】:

如果可能的话,我很难过。我已经发现只能在前端看到按 SKU 排序的结果。不是后端。当您编辑订单时,我希望有一个可以按 ASC/DESC 排序的 SKU 列。是否有实现此目的的插件,或者可以添加到附加列中的代码 sn-p?任何帮助将不胜感激。下面是一张图片,说明了我正在谈论的 SKU,并且希望将其移动/复制到自己的列中

【问题讨论】:

    标签: wordpress woocommerce hook-woocommerce orders


    【解决方案1】:
    add_filter( 'woocommerce_order_get_items', 'filter_order_get_items', 10, 3 );
    function filter_order_get_items( $items, $order, $types = 'line_item' ) {
    
        if(count($items) < 2) return $items;
    
        if( $types != 'line_item') return $items;
    
        $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(); //
                $item_skus[$product->get_sku()] = $items_id;
            }
        }
        // Check items type: for versions before Woocommerce 3.3 (2)
        if( count($item_skus) == 0 ) return $items;
    
        // 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];
        }
        return $sorted_items;
    }
    

    【讨论】:

      【解决方案2】:

      @mujuonly 有一个很好的答案。我一直在使用它,直到发现与 woocommerce 订阅续订有冲突。原始订单很好,但续订订单缺少运输方式。

      相反,我选择按 sku 对购物篮中的项目进行排序,以便以这种方式将它们保存在数据库中。

      add_action( 'woocommerce_cart_loaded_from_session', 'sort_cart_items_sku' );
      
      function sort_cart_items_sku() {
      
          // READ CART ITEMS
          $products_in_cart = array();
          foreach ( WC()->cart->get_cart_contents() as $key => $item ) {
              $products_in_cart[ $key ] = $item['data']->get_sku();
          }
      
          // SORT CART ITEMS
          natsort( $products_in_cart );
      
          // ASSIGN SORTED ITEMS TO CART
          $cart_contents = array();
          foreach ( $products_in_cart as $cart_key => $product_title ) {
              $cart_contents[ $cart_key ] = WC()->cart->cart_contents[ $cart_key ];
          }
          WC()->cart->cart_contents = $cart_contents;
      
      }
      

      信用:https://businessbloomer.com/woocommerce-sort-cart-items-alphabetically-az/

      【讨论】:

        猜你喜欢
        • 2018-10-07
        • 2015-03-19
        • 2018-05-09
        • 1970-01-01
        • 2022-01-26
        • 1970-01-01
        • 2019-04-10
        • 1970-01-01
        • 2021-08-13
        相关资源
        最近更新 更多