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_id
variable 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?