1

We are working on a solution to sort the products in cart and checkout by the vendors store names. We found this here: Woocommerce sort cart products by product category but we have to extend this code for our case. Every product has a vendor. So we do not have to check if a product has no vendor.

I'm not sure how I can output the array in the right way. Can I do it like this?

add_action( 'woocommerce_cart_loaded_from_session', function() {

    global $woocommerce;
    
    // Build product array
    $products_in_cart = array();

    // Loop through cart items
    foreach ( $woocommerce->cart->cart_contents as $key => $item ) {
        
         // Get product object
        $product = $item->get_product();
        
        // Author id
        $author_id = $product->post->post_author;
        
        // Shopname
        $vendor = dokan()->vendor->get( $author_id );
        $shop_name = $vendor->get_shop_name();
        
        $products_in_cart[ $key ] = $shop_name[0]->name;
    
    }

    ksort( $products_in_cart );
    
    // Build cart array
    $cart_contents = array();
        
    foreach ( $products_in_cart as $cart_key => $Vendor_store ) {
        $cart_contents[ $cart_key ] = $woocommerce->cart->cart_contents[ $cart_key ];
    }
    
    // Output sorted cart
    $woocommerce->cart->cart_contents = $cart_contents;

}, 100 );
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Nik7
  • 346
  • 2
  • 16

1 Answers1

3

Updated: Try the following simplified and revisited code:

add_action( 'woocommerce_cart_loaded_from_session', 'sort_cart_items_by_vendor', 100 );
function sort_cart_items_by_vendor() {
    $items_to_sort = $cart_contents = array(); // Initializing

    // Loop through cart items
    foreach ( WC()->cart->cart_contents as $item_key => $cart_item ) {
        $vendor_id  = get_post_field( 'post_author', $cart_item['product_id']);
        $store_info = dokan_get_store_info( $vendor_id );
        $items_to_sort[ $item_key ] = $store_info['store_name'];
    }

    ksort( $items_to_sort );
    
    // Loop through sorted items key
    foreach ( $items_to_sort as $cart_item_key => $store_name ) {
        $cart_contents[ $cart_item_key ] = WC()->cart->cart_contents[ $cart_item_key ];
    }
    
    // Set sorted items as cart contents
    WC()->cart->cart_contents = $cart_contents;
}

Code goes in functions.php file of the active child theme (or active theme). It should work.

Related:

Baezid Mostafa
  • 2,680
  • 2
  • 14
  • 26
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • On line 10 is a syntax error. A ; is missing there. And it always clears the cart after add a new product. But I found in the mean time this question here from you: https://stackoverflow.com/questions/61992935/woocommerce-sort-products-in-cart-by-author-user-in-multi-vendor-setup This would fits better actually than just sort by vendor name. There we would split the cart in sections... Lets talk there. – Nik7 Feb 08 '21 at 20:33
  • 1
    @Nik7 I doesn't use anymore Dokan since some time, so I can't test it anymore. – LoicTheAztec Feb 08 '21 at 20:35
  • Thanks. It works. But I noticed that sometimes after a product add the logic breaks. But it's hard to say then that happens. Some products from vendors are working good and some not. It it possible that products can ignore this new sorting or why is this happening? – Nik7 Feb 08 '21 at 21:26
  • I guess this is because the code messed up with a plugin code (Quantity check). So when I activate the code, then all the quantity checks does not work anymore. Can that be a possible issue? – Nik7 Feb 08 '21 at 21:58
  • 1
    @Nik7 The code works. So in your case there is something that is making trouble. It can be your theme, a plugin or some other custom code added by you… *(sleeping now)*. – LoicTheAztec Feb 08 '21 at 22:31
  • I gave the vote, but I just realized, this code does not always work for me, similarly. For example, when you add 3 items from 1 vendor and 4 items from another. –  Feb 04 '22 at 04:42