How to get particular product variation quantity dynamically from the cart page in the woocommerce by variation_ID?
I'm trying to display quantity of product variation which added to cart and display this in the loop dynamically.
I'm tried this PHP code:
<?php
$targeted_id = $product->get_id();
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ) {
if( in_array( $targeted_id, array( $cart_item['product_id'],$cart_item['variation_id']) )){
$quantity = $cart_item['quantity'];
break; // stop the loop if product is found
}
}
// Displaying the quantity if targeted product is in cart
if( isset( $quantity ) && $quantity > 0 ) {
echo '<a href="http://adel-hat.ru/cart/"><div class="qty_on_product"><i class="tb-icon tb-icon-shopping-cart on_product"></i>';
printf( '<span class="product-cart-items">' . __("%d") . '</span>', $quantity );
echo '</span></div></a>';
}
?>
With this code I'm display all variations added to cart(because here I'm taking parent id) and it works only on page load.
I want to get into
$tageted_idvariable variation_id dynamically when variation selector changing without page load. How I can do this?
From another hand of my problem I'm trying to get variation_id through jQuery
<script>
jQuery(document).ready(function() {
jQuery( '.variations_form' ).each( function() {
jQuery(this).on( 'found_variation', function( event, variation ) {
console.log(variation);//all details here
var variation_id= variation.variation_id;//selectedvariation
console.log(variation_id);
});
});
});
</script>
And when I use this one i'm getting selected variation ID in console. May be is there any way to send
variation_id
variable to PHP
$tageted_id
variable dynamically without page reload?
Solutions
Now I have update functionality so you can get product selected variation id and use accordingly:
function find_matching_product_variation_id($product_id, $attributes)
{
return (new \WC_Product_Data_Store_CPT())->find_matching_product_variation(
new \WC_Product($product_id),
$attributes
);
}
add_action( 'woocommerce_before_cart', 'woo_find_product_in_cart' );
function woo_find_product_in_cart(){
$targeted_product_variation_ids = array();
$targeted_id = 4215; // product_id
//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'];
$quantity = $cart_item['quantity'];
//first - check if product has variations
if(($targeted_id == $product_id) && (isset($cart_item['variation']) && count($cart_item['variation']) > 0 ))
{
// Cart Product Selected Variation id
$variation_id = find_matching_product_variation_id($product_id,$cart_item['variation']);
//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 $data)
{
$targeted_product_variation_ids[] = $data['variation_id'];
}
}else{
$targeted_product_variation_ids[] = $product_id;
}
if( in_array( $variation_id, $targeted_product_variation_ids) && isset( $quantity ) && $quantity > 0){
echo '<a href="http://adel-hat.ru/cart/"><div class="qty_on_product"><i class="tb-icon tb-icon-shopping-cart on_product"></i>';
printf( '<span class="product-cart-items">Variation Product Qty: ' . __("%d") . '</span>', $quantity );
echo '</span></div></a>';
}
}
}