Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WooCommerce - Get selected variation for a product in cart

Hello,

Any one please help me find the solution.

My client has a wholesale business, in which he don't need woocommerce checkout functionality. He needs woocommerce functionality upto cart, but instead of checkout he wants a "Place Order" button.

Now, everything is working fine, placing order correctly, order is storing into database and mailing to admin, but the problem is that I want to store variations as well, my question is how to get selected variation (if there is any) of a product inside functions.php for that product, which is already in cart?

Any hint will be much appreciated.

like image 867
Habib-ur-Rahman Avatar asked Apr 02 '14 11:04

Habib-ur-Rahman


1 Answers

Hope that I've understood your query correctly.

You are saying that you want to get variations detail (if available) of the product, which is there in cart.

Cart contains many items. You can loop over items & can get variation details of each items.

A cart item is an associative array & you may find product id in $item['product_id'] & variation id in $item['variation_id']

Please use following function & pass variation id to get variation detail:

function get_variation_data_from_variation_id( $item_id ) {
    $_product = new WC_Product_Variation( $item_id );
    $variation_data = $_product->get_variation_attributes();
    $variation_detail = woocommerce_get_formatted_variation( $variation_data, true );  // this will give all variation detail in one line
    // $variation_detail = woocommerce_get_formatted_variation( $variation_data, false);  // this will give all variation detail one by one
    return $variation_detail; // $variation_detail will return string containing variation detail which can be used to print on website
    // return $variation_data; // $variation_data will return only the data which can be used to store variation data
}

Now let's see how to use this function

$item_id = ( !empty( $cart_item['variation_id'] ) ) ? $cart_item['variation_id'] : '';
if ( !empty( $item_id ) ) {
   $variations = get_variation_data_from_variation_id( $item_id );
}

Hope it'll be useful.

like image 54
Ratnakar - StoreApps Avatar answered Sep 28 '22 06:09

Ratnakar - StoreApps