phpwordpresssortingwoocommercedokan

Woocommerce - Sort products in cart by Author/User in Multi-vendor setup


I have a multivendor Woocommerce shop using Dokan plugin and I'm trying to split out the shopping cart into sections based on who the vendor is. For example:

Dokan uses a custom role 'vendor' to extend the user class, so to get the ID of the vendors, I should be able to use something like:

$post_data = get_post( $cart_item['product_id'] ); 
$vendor_id = $post_data->post_author;

This does work, but it will only get the first vendor ID and simply repeats that for all remaining products in the cart. I know this is because I'm not retrieving an array but I can't really find anything in the WP documentation on how to get an array of author IDs (other than wp_list_authors, but that doesn't work well).

As an experiment, I managed to get the splitting + sorting working so long as I'm sorting by categories, since I can use wp_get_post_terms(). I can't replicate this for author data, though...

Current (relevant) code is below:

<?php
$cat_sort = array();

foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
  $product_id = $cart_item['product_id'];
  $cat_ids = wp_get_post_terms( $product_id, 'product_cat', array( 'fields' => 'ids' ) );

  foreach ( $cat_ids as $id ) {
    $cat_sort[$id][$cart_item_key] = $cart_item;
  }                                                    

}

ksort( $cat_sort ); 

$grouped_cart_items = array();
  foreach ( $cat_sort as $cat_id => $cart_items ) {
    $term = get_term( $cat_id, 'product_cat' );                           
?>
<tr>
  <td colspan="6" class=""><strong><?php echo $term->name; ?></strong></td>
</tr>

(After this is the actual product loop which shouldn't be important here because their sort ordering happens in the above code)

Any ideas on how I can get author info for the cart products the same way I can get the categories? I'm pretty stumped...

Thanks in advance for any and all assistance!


Solution

  • Here is an example on how to sort and display cart items by Dokan vendor store name:

    <table>
    <?php
    
    $car_items  = WC()->cart->get_cart(); // Cart items
    
    $items_sort = array(); // Initializing
    
    // Loop through cart items
    foreach ( $car_items as $cart_item_key => $cart_item ) {
        // Get the vendor_id
        $vendor_id   = get_post_field( 'post_author', $cart_item['product_id'] );
    
        $store_info  = dokan_get_store_info( $vendor_id ); // Get the store data
        $store_name  = $store_info['store_name'];          // Get the store name
    
        // Set in multidimentional array the vendor and then the cart item key
        $items_sort[$store_name][$cart_item_key] = $vendor_id;
    }
    
    if ( count($car_items) > 1 ) {
        ksort( $items_sort ); // Sorting by vendor name
    }
    
    // 1st Loop by vendor name
    foreach ( $items_sort as $store_name => $values ) {
        $vendor_id  = reset($values); // The vendor id
        /$store_url = dokan_get_store_url( $vendor_id );  // Get the store URL (if needed)
        ?>
        <tr>
            <!-- Store name display -->
            <td colspan="6" class="store-name"><strong><?php echo $store_name; ?></strong></td>
        </tr>
        <?php
        // 2nd Loop the cart items for the vendor name
        foreach( $values as $cart_item_key => $vendor_id) {
    
            // Retreive the cart item from the cart item key
            $cart_item = $car_items[$cart_item_key];
            ?>
            <tr>
                <!-- Product name display -->
                <td colspan="6" class="product-name"><?php echo $cart_item['data']->get_name(); ?></td>
            </tr>
            <?php
    
        } // End of 2nd Loop
    
    } // End of 1st Loop
    
    ?>
    </table>
    

    Related: Display dokan vendor name on Woocommerce single product pages