Automated Cart Update With Alert Box Each Time

New To PHP and, I am currently attempting to create a plug in that creates an alert box every time the quantity in a cart is changed. So far I have successfully implemented updating the cart automatically, but I am having trouble determining how to create an alert box on update each time that references the new cart total. Any advice would be appreciated.

TLDR: Creating a plug in that will display an alert box when you change the quantity of an item in the cart with the following text:

You just changed the quantity of ABC to 2, where ABC is the SKU name, and 2 is the current (new) quantity of the item.

?php
/**
 * Plugin Name: Order Check 2
**/
add_action( 'wp_footer', 'cart_update_qty_script', 'persistent_cart_update');

function persistent_cart_update() {
    foreach ( WC()-cart-get_cart() as $cart_item ) {
        // gets the cart item quantity
        $quantity           = $cart_item['quantity'];}
    }
function phpAlert($msg) {
    echo 'script type=text/javascriptalert(' . $msg . ')/script';
}
function cart_update_qty_script() {
    if (is_cart()) :
    ?
    script
         jQuery('div.woocommerce').on('change', '.qty', function(){
        jQuery([name='update_cart']).prop(disabled, false);
        jQuery([name='update_cart']).trigger(click); 
    });
    
    /script
?php phpAlert(   Your have changed the quantity of sweatshirts to  .$quantity..);  ?
   
    ?php
    endif;
}

?

Topic woocommerce-offtopic php plugin-development Wordpress

Category Web


// Display sku in cart add_filter( 'woocommerce_get_item_data', 'func_js_display_custom_item_data_in_cart', 100, 2 ); function func_js_display_custom_item_data_in_cart( $cart_item_data, $cart_item ) { $cart_item_data[] = array( 'name' => __( 'SKU ', 'woocommerce' ), 'value' => '' . $cart_item['data']->get_sku() . ' ', );

return $cart_item_data;

}

add_action( 'wp_footer', 'cart_update_qty_script' ); function cart_update_qty_script() { if (is_cart()) : ?> var $ = jQuery; $('div.woocommerce').on('change', '.product-quantity .quantity input[id^="quantity"]', function() {

        var qty = $(this).val();
        var sku = $(this).closest('tr.cart_item').find('.product_sku').html();
        alert("Your have changed the quantity of " + sku + " to " + qty + ".");
        
        $("[name='update_cart']").prop("disabled", false);
        $("[name='update_cart']").trigger("click");
        
    });

</script>

<?php
endif;

}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.