• Resolved jonas_op

    (@jonas_op)


    I have (not anymore) used the Sold individually since I sell courses.

    Some of my buyers purchase and then surf around and purchase again, and if the product is already in the cart, the Add to cart button only redirects them to the product page.

    Is there a way to override this? Or at least show them an error message?

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Support Shameem R. a11n

    (@shameemreza)

    Hi @jonas_op

    Yes, there is a way to handle this. You can use a WooCommerce hook to display a custom error message when a customer tries to add a product they’ve already purchased to their cart.

    Although writing or providing custom code is not within the scope of our support policy, you can add this custom code snippet to your child theme’s funcitons.php file:

    add_filter( 'woocommerce_add_to_cart_validation', 'only_one_in_cart', 99, 2 );
    function only_one_in_cart( $passed, $product_id ) {
    // If there is a product in the cart
    if( WC()->cart->get_cart_contents_count() > 0 ){
    // Display a message
    wc_add_notice( __( 'You already have a course in your cart. Please complete that purchase before adding another course.', 'woocommerce' ), 'error' );
    return false;
    }
    return $passed;
    }

    If above code doesn’t work, try this one: https://stackoverflow.com/a/70377722/23495588

    If you still need assistance, we recommend asking development questions on the #developers channel of the WooCommerce Community Slack. Many of our developers hang out there and will be able to offer insights into your question. You can also seek help from the following:

    I wish I could help more, but hopefully, this gets you going in the right direction to get the job done.

    Thread Starter jonas_op

    (@jonas_op)

    Thank’s for the code. Does it work in a snippet plugin too? And is there a way to just send them to the cart instead of the error message?

    Thread Starter jonas_op

    (@jonas_op)

    Seeing now that the code is for any product in the cart, not “this product” in the cart.

    Hey, @jonas_op!

    Thank’s for the code. Does it work in a snippet plugin too? And is there a way to just send them to the cart instead of the error message?

    Yes, you can add the code to a snippet plugin ??

    Since custom code is not under our support policy,?we are unable to assist you further with this.

    As my colleague mentioned, we do have a great community on Slack, you can check the #developers channel there and ask for help ??

    You can also seek help from the following:

    Please let us know if there’s anything else we can do to help or if you have any questions.

    Have a wonderful day!

    Plugin Support Shameem R. a11n

    (@shameemreza)

    Hi @jonas_op

    We’ve not heard back from you in a while, so I’m marking this thread as resolved. Hopefully, you were able to find a solution to your problem!

    If you have further questions, please feel free to open a new topic.

    Thanks!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Sold individually removes add to cart function’ is closed to new replies.