Menu Close
    Hire us Request quote Reading list Switch to dark mode

    WooCommerce Cart Restriction

    Many times we face an issue regarding exclusive addition of a product in cart. For example, if a product say A is in cart then customer cannot add any new product B that is called exclusive products. In this case,  customer is bound to either remove that very product A from cart or place order for it. So, here we will put some WooCommerce Cart Restriction.


    WooCommerce Cart Restriction With ID


    Lets say a product named A is exclusive and we know it’s ID i.e., product id is known then we can approach like this:-

    As we can see WooCommerce already provides a filter named “woocommerce_before_cart” so we have to load our function on that very hook. The filter provides cart data, so we will get all cart data and  perform check for the exclusive product A with id “$id” among all products in cart. If the id’s matches then we will empty cart and add the exclusive product only in cart else we will do no changes in cart.


    WooCommerce Cart Restriction With Title


    There is also a situation when we don’t know the id of exclusive product so in that case we can approach like this:-

    I hope its quite easy to understand the above code. We can simply get the product details by using  get_page_by_title() and then we can ghet it’s ID.

    We also face some cases where we don’t know  id and title of custom post type. So, in such cases we can perform the same using “post type”.


    Still have any issue feel free to add a ticket and let us know your views to make the code better

    Thanks for Your Time! Have a Good Day!


    . . .

    Comments (3)

    Add Your Comment

  • JL
    Very useful code! But where should we insert it? functions.php? Any woocommerce .php file?


    • Varun Kumar
      You can create separate plugin using this code or insert the code in themes function file.
  • Thakur Amit Chauhan
    Much appreciated thanks for this wonderful blog
  • Back to Top
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just received your message and our expert will get back to you shortly.
    Send Again

    Table of Content

    Hide Index