phpwordpresswoocommercecartvendor

How to get the vendor_ids for each of the vendors with products on the cart page in woocommerce


global $woocommerce;
$items = $woocommerce->cart->get_cart();

foreach($items as $item => $values) { 
    $_product = $values['data']->post; 
    echo "<b>".$_product->post_title.'</b>  <br> Quantity: '.$values['quantity'].'<br>'; 
    $price = get_post_meta($values['product_id'] , '_price', true);
   echo "  Price: ".$price."<br>";

I have used the following to get data about the products on the cart and display it. is it possible to get the vendor of each product too and how can I do that in woo commerce? again from the cart page because I want to echo the vendor address back under each product on the cart page.


Solution

  •   add_filter( 'woocommerce_get_item_data', 'wc_add_address_to_cart', 10, 2 ); 
    function wc_add_address_to_cart( $other_data, $cart_item ) { 
        $post_data = get_post( $cart_item['product_id'] ); 
        $post_data->post_author; 
        $vendor_id = $post_data->post_author;
        echo '<br>';
        $Add = 'Address: ';
        $city = 'City: ';
        $tel = 'Phone: ';
        echo $test;
        $user_id = $vendor_id;
    
        // Get all user meta data for $user_id
        $meta = get_user_meta( $user_id );
    
        // Filter out empty meta data
        $meta = array_filter( array_map( function( $a ) {
            return $a[0];
        }, $meta ) );
    
        echo $Add;
        print_r( $meta ['_vendor_address_1'] );
    
        echo '<br>';
    
        echo $city;
        print_r( $meta['_vendor_city'] );
    
        echo '<br>';
    
        echo $tel;
        print_r( $meta['_vendor_phone'] ); 
        return $other_data; 
    
    } 
    

    Finally got what I was looking for. Just add that to the functions.php