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_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?

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>';
        }
    }
}

Similar questions

Custom product type with variation, variation name not show in cart/checkout
I created a CUSTOM product type variation, everything works good in backend and front end, the only problem is, in the shopping cart / Checkout details, the variable product name not showing, also after purchase item the stock is not reducing, 1 item available for the variable, after purchase, keep the 1 item available instead of out of stock. This...
Get quantity of the active selected product variation in WooCommerce
I am building a shop online (with Woocommerce) that will display single and variable products. When in the single products page, I need some output/text to depend upon the selected product in the single product page being in or out of stock. I am building this conditioning in PHP. For single products this is trivial: I am hooking in 'woocommerce_be...
How to get the particular product quantity in orders in Woocommerce
I am working on my WooCommerce project and I am sending mail to the customer and in that I am sending the particular product quantity. So, I am not able to get that particular product quantity from the order. This is code: I want to get the '14988' product quantity from the orders. Any help is much appreciated.
Add to cart validation in WooCommerce based on attribute value and product quantity from the cart page
Actually, I am trying to set a limit for adding to cart for some product ids based on product attribute. What i've done is: It works in a loop, but on the product page, it doesn't take into account quantity input. Is there a way to get quantity add input in my function?
How do I get Woocommerce product variation name and variation description in a WP_Query?
I get the product variations each as their own button with below code that I have pieced together. Echo $product->name outputs something like "Product - variation" but I need "Variation price". Price would be entered in description field in the dashboard. I have gone through several related questions and answers but I'm not skilled enough to apply ...
How to create an array for simple product sku and if variation product variation sku and parent sku
Hello I need to create an array that if the product is simple, product sku. If the product is a variation, variation sku and parent product sku. It doesn't seem to be clearing the "is_type('variable') check and just put the product id sku for both itemID and itemGroupID. Please see my code: I am then passing the array in javascript What i am t...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.