0

I want to create an alert when I add a product with the shipping class "FRESH" to the shopping cart with Woocommerce.

I have created this function although I know that they contain 2 Hooks because I have not found the correct one yet, the alert appears even when I do not add a product with the "FRESH" category.

My idea that it may be misdirected was to verify once a product added to the cart if there is one with "FRESH", and that there is only one but it is not well raised.

My wish would be when I add a product to the cart if that product I am adding has the shipping class "Fresh" to notify the customer with a PopUp / alert that says for example. "You have added a FRESH product."

thanks for your help

function action_woocommerce_after_add_to_cart_form_alert_if_cart_add_fresh($product) { 
        
    $shippingclass_array = array( 'fresh' );
    $cart_contain_product_fresh_for_alert = 0;

    foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) 
    {
        $shipping_class = get_the_terms( $values['product_id'], 'product_shipping_class' );
        $quantity = $values['quantity'];

        if ($quantity < 2 ){
            if ( isset( $shipping_class[0]->slug ) && in_array( $shipping_class[0]->slug, $shippingclass_array ) ) {
                $cart_contain_product_fresh_for_alert ++; 
                
            }
        }
    }

    if($cart_contain_product_fresh_for_alert === 1 ){
        $freshInCart = true;
        ?>
            <script>
                jQuery(function($){
                    alert('Se ha añadido un producto fresco a su carrito');
                });
            </script>
        <?php
    }
}; 
add_action( 'woocommerce_after_add_to_cart', 'action_woocommerce_after_add_to_cart_form_alert_if_cart_add_fresh'); 
add_action( 'woocommerce_after_add_to_cart_button', 'action_woocommerce_after_add_to_cart_form_alert_if_cart_add_fresh', 10, 1 );
  • 1
    The `woocommerce_after_add_to_cart_button` hook does not mean 'do an action after adding a product to the cart' but is used to display something after the cart button in your theme. For example a message, image or additional HTML – 7uc1f3r Jan 04 '22 at 15:59
  • Thank you very much for the information 7uc1f3r. Would you know then how this action could be done? – Jose Miguel Jan 04 '22 at 16:30
  • 1
    See [Run javascript code after ajax add to cart event in Woocommerce](https://stackoverflow.com/q/49281966/11987538) **or** [Trigger a JQuery click once product is added to cart on WooCommerce single products](https://stackoverflow.com/q/64918848/11987538) **or** [JS alert on ajax add to cart for multiple product categories count in Woocommerce](https://stackoverflow.com/q/50555448/11987538) **or** [Display a sweet alert on ajax add to cart for a specific Woocommerce cart item count](https://stackoverflow.com/q/50435374/11987538), etc.. depending on your wishes/needs – 7uc1f3r Jan 04 '22 at 16:34
  • thanks a lot 7uc1f3r. I'm going to look at what happened to me and I'll inform you of anything. – Jose Miguel Jan 04 '22 at 16:39

0 Answers0