Skip to content

Instantly share code, notes, and snippets.

@alordiel
Created January 24, 2018 08:52
Show Gist options
  • Save alordiel/5afd3aeebad6858cb873b8ad34e74cce to your computer and use it in GitHub Desktop.
Save alordiel/5afd3aeebad6858cb873b8ad34e74cce to your computer and use it in GitHub Desktop.
[WooCommerce 3.2.6] Get product variations from cart items (used in cart.php)
<?php
//Loop through each item from the cart
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
//get the current product ID
$product_id = $cart_item['product_id'];
//first - check if product has variations
if(isset($cart_item['variation']) && count($cart_item['variation']) > 0 ){
//get the WooCommerce Product object by product ID
$current_product = new WC_Product_Variable($product_id);
//get the variations of this product
$variations = $current_product->get_available_variations();
//Loop through each variation to get its title
foreach($variations as $index => $data){
$variation_id = $data['variation_id'];
$variation_title = esc_html ( get_the_title(variation_id) );
$variation_price = $data['display_price'];
$variation_attr = $data['attributes'];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment