0

I need to add a script to the footer if a product is added to the cart. When a product is added, it goes to the checkout page. So here on the checkout page, I need to insert the script. (Only once, when added. It will not show up after reloading the page.)

add_action( 'wp_footer', 'clearlocal' );
function clearlocal(){
  add_filter( 'woocommerce_add_cart_item_data', 'wdm_empty_cart', 10,  3);
  function wdm_empty_cart( $cart_item_data, $product_id, $variation_id ) {
     global $woocommerce;
     //Check if product ID is in a certain category
     if( has_term( 'packages', 'product_cat', $product_id ) ){
         ?><script type="text/javascript"> localStorage.clear();
         </script><?php
     }
     //Do nothing with the data and return
     return $cart_item_data;
  }
}

If a product is added from the category called packages, then I need to add the script. But I need it only when added, not always as soon as the product is in the cart.

If my goal is to show a message. Then it works just as I need.

  add_filter( 'woocommerce_add_cart_item_data', 'wdm_empty_cart', 10,  3);
  function wdm_empty_cart( $cart_item_data, $product_id, $variation_id ) {
    global $woocommerce;
    //Check if product ID is in a certain category
    if( has_term( 'packages', 'product_cat', $product_id ) ){
        wc_add_notice('Some texts','success');
    }
    //Do nothing with the data and return
    return $cart_item_data;
  }

I think the problem is add_filter is not working with add_action. I also tried this but did not work.

add_filter( 'woocommerce_add_cart_item_data', 'wdm_empty_cart', 10,  3);
function wdm_empty_cart( $cart_item_data, $product_id, $variation_id ) {
 global $woocommerce;
 //Check if product ID is in a certain category
 if( has_term( 'packages', 'product_cat', $product_id ) ){
     add_action( 'wp_footer', 'clearlocal' );
     function clearlocal(){
     ?><script type="text/javascript"> localStorage.clear();
     </script><?php
 }
 }
 //Do nothing with the data and return
 return $cart_item_data;
}
wp-ap
  • 109
  • 1
  • 8
  • 2
    I believe it would be better to use `wc_enqueue_js` and use an [anonymous function](https://www.php.net/manual/en/functions.anonymous.php) for your add_action. – Howard E Jun 24 '22 at 16:09
  • Thank you. Can you give some examples? – wp-ap Jun 24 '22 at 17:33

1 Answers1

1

Use wc_enqueue_js

add_filter( 'woocommerce_add_cart_item_data', 'he_check_empty_cart', 10,  3);
function he_check_empty_cart( $cart_item_data, $product_id, $variation_id ) {
    //Check if product ID is in a certain category.
    if( has_term( 'packages', 'product_cat', $product_id ) ){
        wc_enqueue_js(' localStorage.clear();' );
        }
    }
    //Do nothing with the data and return.
    return $cart_item_data;
}
Howard E
  • 5,454
  • 3
  • 15
  • 24
  • Thank you. wc_enqueue_js is working itself but inside a function, it does not work. – wp-ap Jun 24 '22 at 20:50
  • That is exactly the purpose of wc_enqueue_js. Are you sure you're condition is correct? – Howard E Jun 24 '22 at 22:48
  • I understand the problem. I redirect to the checkout page after adding the cart. So without redirecting it works. But with `woocommerce_add_to_cart_redirect` it does not work. – wp-ap Jun 24 '22 at 23:15
  • 1
    You should look at triggering this with another hook. If this can fire on `woocommerce_before_checkout_form` or some other action, as opposed to a filter. – Howard E Jun 24 '22 at 23:34
  • I found another way to achieve my goal. When clicking the add to cart button, a script is adding. ` $(".add_to_cart_button").click(function(){ $.getScript("/script.js"); }); '; } ?>` – wp-ap Jun 25 '22 at 06:38