1

Problem

I have some products which are marked as "individually sold" in WooCommerce. If a user adds more than one of the same product, I want an alert box to appear to say "you can't add more than one of that item". Something like this: enter image description here

What I tried

Using the information from these two threads:

Change "You cannot add another (product) to your cart" notice in Woocommerce

How to pop an alert message box using PHP?

I created used this code:

add_filter(  'gettext',  'change_specific_add_to_cart_notice', 10, 3 );
add_filter(  'ngettext',  'change_specific_add_to_cart_notice', 10, 3 );
function change_specific_add_to_cart_notice( $translated, $text, $domain  ) {
    if( $text === 'You cannot add another "%s" to your cart.' && $domain === 'woocommerce' && ! is_admin() ){
echo "<script type='text/javascript'>alert("You cannot add more than one of the same item");</script>";
}

However, this alert message doesn't just appear when the user is adding the same product twice to cart. It appears everywhere throughout the site, even when users just land on the homepage.

Anyone knows how to solve this? Thanks.

xojijog684
  • 190
  • 2
  • 8

1 Answers1

1

There are some typo's in your code. You are missing a closing } and in your echo statement you haven't correctly escaped your double quotes.

Try the following:

add_filter( 'gettext', 'change_specific_add_to_cart_notice', 10, 3 );
add_filter( 'ngettext', 'change_specific_add_to_cart_notice', 10, 3 );
function change_specific_add_to_cart_notice( $translated, $text, $domain  ) {
    if ( $text === 'You cannot add another "%s" to your cart.' && $domain === 'woocommerce' && ! is_admin() ) {
        $message = 'You cannot add more than one of the same item';
        ?>
        <script type='text/javascript'>
            window.onload = function() {
                alert('<?php echo $message; ?>');
            }
        </script>
        <?php
    }
    return $translated;
}

This only works when the add to cart buttons do not make use of AJAX. You can disable AJAX for these buttons via WooCommerce > Settings > Products > Enable AJAX add to cart buttons on archives.

Terminator-Barbapapa
  • 3,063
  • 2
  • 5
  • 16
  • 1
    If this answer works for you could you mark the answer as accepted and possibly vote it up? Thanks! – Terminator-Barbapapa Jun 29 '20 at 14:59
  • hey @Terminator-Barbapapa, apologies I hadn't checked my account. Nope, it doesn't work (I do not see an alert box appearing). Agreed with your above comment that it is bad UX to show the message twice. I have hidden the default WooCommerce message using CSS and used the following code to just show an alert box. Once again it does not work. could you help me out? thank you. https://jsfiddle.net/lindychen/kpae482w/3/ – xojijog684 Jun 30 '20 at 12:46
  • 1
    My apologies, this only works when the add to cart buttons do not make use of AJAX. You can disable AJAX for these buttons via WooCommerce > Settings > Products > Enable AJAX add to cart buttons on archives. (Edited my answer.) – Terminator-Barbapapa Jun 30 '20 at 13:56
  • Thanks for the reply. May I know if there is a way for this to work with AJAX? You are right it is currently turned on. I have marked your answer as correct as I had not previously mentioned about the AJAX requirement but just wondering if you could help me out? – xojijog684 Jun 30 '20 at 14:14
  • 1
    As far as I can see there are only two filters that will let you determine if a 'single sold' product was added more than once to the cart; `woocommerce_add_to_cart_sold_individually_found_in_cart` and the the `gettext` filter. Since these are both filters and not actions these will stop the whole process, of adding to the cart, dead in its tracks if done via AJAX. My answer is already a bit hacky as it is not done to output anything in a filter. That's what actions are for. So I don't think this can be done easily any other way, but I don't know what I don't know of course. :) – Terminator-Barbapapa Jun 30 '20 at 14:42