admin管理员组

文章数量:1390204

I need to create notification when user's added quantity is reaching item's quantity. This is working function without ajax.

add_action( 'woocommerce_single_product_summary', 'bbloomer_show_return_policy', 40 );
function bbloomer_show_return_policy() { 
    global $product;
    global $woocommerce;
    $idP = $product->get_id();
    foreach ( $woocommerce->cart->get_cart() as $cart_item ) { 
    if($cart_item['product_id'] == $idP ){
        $qty =  $cart_item['quantity'];
        break; // stop the loop if product is found
    }
    }
    if($qty >= $product->get_stock_quantity())
    {
        echo 'notification';
    }
}

How can I achieve that notification will throw when user added extra quantity of item.

Also, I tried to make ajax call, but item's cart quantity still the same.

add_action('wp_head', 'ajx_action_javascript_wp');
function ajx_action_javascript_wp() {
?>

<script type="text/javascript" >
jQuery(document).ready(function($) {
jQuery("body").on("click", ".single_add_to_cart_button", function(event){
            <?php
            global $product;
    global $woocommerce;
    $idP = $product->get_id();
    foreach ( $woocommerce->cart->get_cart() as $cart_item ) { 
    if($cart_item['product_id'] == $idP ){
        $qty =  $cart_item['quantity'];
        echo $qty;
         break;
    }     
    }   
    ?>
      var mydata = <?php echo $qty; ?>;
        var data = {
            action: 'custom_action',
            //whatever: 1234,
            qTY: mydata
        };
        console.log(data);
    });
});

</script>
<?php
}

What I'm doing wrong?

本文标签: woocommerce offtopicPass values to ajax