{"id":75165,"date":"2017-02-18T07:23:34","date_gmt":"2017-02-18T07:23:34","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=75165"},"modified":"2023-05-31T06:15:55","modified_gmt":"2023-05-31T06:15:55","slug":"woocommerce-cart-restriction","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/woocommerce-cart-restriction\/","title":{"rendered":"WooCommerce Cart Restriction"},"content":{"rendered":"<p>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,\u00a0\u00a0customer 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.<\/p>\n<p>&nbsp;<\/p>\n<h3>WooCommerce Cart Restriction With ID<\/h3>\n<p>&nbsp;<\/p>\n<p>Lets say a product named A is exclusive and we know it&#8217;s ID i.e., product id is known then we can approach like this:-<\/p>\n<pre class=\"brush:php\">&lt;?php\r\n\r\nclass Exclusive_Product{\r\n\r\n\tfunction __construct(){\r\n\r\n\t\tadd_filter( 'woocommerce_before_cart', array($this, 'check_cart_product') ,10, 1);\r\n\r\n\t}\r\n\r\n\tpublic function check_cart_product( $cart_item_data ){\r\n\t\tglobal $woocommerce;\r\n\t\t$id; \/\/ exclusive product id\r\n<code class=\"php variable\">                $cart<\/code> <code class=\"php plain\">= WC()-&gt;cart;<\/code>\r\n\t\t$get_cart = WC()-&gt;cart-&gt;cart_contents; \/\/all cart contents\r\n\t\tif(!empty($get_cart)){\r\n\t\t\tforeach($get_cart as $key =&gt; $value ){\r\n\t\t\t\t$product_id = $value['product_id'];\r\n\t\t\t\tif($product_id == $id){ \/\/comparing exclusive product id i.e.\"$id\" with all cart products id's i.e.\"$product_id\"\r\n\t\t\t\t\t$woocommerce-&gt;cart-&gt;empty_cart();\r\n\t\t\t\t\tWC()-&gt;cart-&gt;add_to_cart($id);\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\treturn WC()-&gt;cart-&gt;cart_contents;\r\n\t\t}\r\n\t}\r\n\r\n}<\/pre>\n<p>As we can see WooCommerce already provides a filter named &#8220;woocommerce_before_cart&#8221; so we have to load our function on that very hook. The filter provides cart data, so we will get all cart data and \u00a0perform check for the exclusive product A with id &#8220;$id&#8221; among all products in cart. If the id&#8217;s matches then we will empty cart and add the exclusive product only in cart else we will do no changes in cart.<\/p>\n<p>&nbsp;<\/p>\n<h3>WooCommerce Cart Restriction With Title<\/h3>\n<p>&nbsp;<\/p>\n<p>There is also a situation when we don&#8217;t know the id of exclusive product so in that case we can approach like this:-<\/p>\n<pre class=\"brush:php\">&lt;?php\r\n\r\nclass Exclusive_Product{\r\n\r\n\tfunction __construct(){\r\n\r\n\t\tadd_filter( 'woocommerce_before_cart', array($this, 'check_cart_product') ,10, 1);\r\n\r\n\t}\r\n\r\n\tpublic function check_cart_product( $cart_item_data ){\r\n\t\tglobal $woocommerce;\r\n\t\t$exclusive_product = get_page_by_title('Product Name', OBJECT, 'product');\r\n\t\t$id = $exclusive_product-&gt;ID; \/\/ID of exclusive product\r\n<code class=\"php variable\">                $cart<\/code> <code class=\"php plain\">= WC()-&gt;cart;<\/code>\r\n\t\t$get_cart = WC()-&gt;cart-&gt;cart_contents; \/\/all cart contents\r\n\t\tif(!empty($get_cart)){\r\n\t\t\tforeach($get_cart as $key =&gt; $value ){\r\n\t\t\t\t$product_id = $value['product_id'];\r\n\t\t\t\tif($product_id == $id){ \/\/comparing exclusive product id i.e.\"$id\" with all cart products id's i.e.\"$product_id\"\r\n\t\t\t\t\t$woocommerce-&gt;cart-&gt;empty_cart();\r\n\t\t\t\t\tWC()-&gt;cart-&gt;add_to_cart($id);\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\treturn WC()-&gt;cart-&gt;cart_contents;\r\n\t\t}\r\n\t}\r\n\r\n}<\/pre>\n<p>I hope its quite easy to understand the above code. We can simply get the product details by using \u00a0get_page_by_title() and then we can ghet it&#8217;s ID.<\/p>\n<p>We also face some cases where we don&#8217;t know \u00a0id and title of custom post type. So, in such cases we can perform the same using &#8220;post type&#8221;.<\/p>\n<div class=\"panel panel-primary\">\n<div class=\"panel-heading\">\n<div>\n<h3 class=\"panel-title\">Support<\/h3>\n<\/div>\n<\/div>\n<div class=\"panel-body\">\n<div>\n<p>Still have any issue feel free to add a ticket and let us know your views to make the code better\u00a0<a href=\"https:\/\/webkul.uvdesk.com\/en\/customer\/create-ticket\/\" target=\"_blank\" rel=\"noopener\">https:\/\/webkul.uvdesk.com\/en\/customer\/create-ticket\/<\/a><\/p>\n<p>Thanks for Your Time! Have a Good Day!<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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,\u00a0\u00a0customer is bound to either remove that very product A from cart or place order for <a href=\"https:\/\/webkul.com\/blog\/woocommerce-cart-restriction\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":111,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7966,1260],"tags":[1060,4508,1468,4507,1258],"class_list":["post-75165","post","type-post","status-publish","format-standard","hentry","category-wordpress-woocommerce","category-wordpress","tag-cart","tag-cart-restriction","tag-woocommerce","tag-woocommerce-cart","tag-wordpress"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>WooCommerce Cart Restriction - Webkul Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/woocommerce-cart-restriction\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"WooCommerce Cart Restriction - Webkul Blog\" \/>\n<meta property=\"og:description\" content=\"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,\u00a0\u00a0customer is bound to either remove that very product A from cart or place order for [...]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/woocommerce-cart-restriction\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2017-02-18T07:23:34+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-05-31T06:15:55+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Ayush Singh\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ayush Singh\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/woocommerce-cart-restriction\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/woocommerce-cart-restriction\/\"},\"author\":{\"name\":\"Ayush Singh\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/5deb39718ae59fa5659a825549b7df4a\"},\"headline\":\"WooCommerce Cart Restriction\",\"datePublished\":\"2017-02-18T07:23:34+00:00\",\"dateModified\":\"2023-05-31T06:15:55+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/woocommerce-cart-restriction\/\"},\"wordCount\":306,\"commentCount\":3,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"keywords\":[\"cart\",\"Cart Restriction\",\"WooCommerce\",\"WooCommerce Cart\",\"wordpress\"],\"articleSection\":[\"WooCommerce\",\"WordPress\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/woocommerce-cart-restriction\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/woocommerce-cart-restriction\/\",\"url\":\"https:\/\/webkul.com\/blog\/woocommerce-cart-restriction\/\",\"name\":\"WooCommerce Cart Restriction - Webkul Blog\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"datePublished\":\"2017-02-18T07:23:34+00:00\",\"dateModified\":\"2023-05-31T06:15:55+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/woocommerce-cart-restriction\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/woocommerce-cart-restriction\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/woocommerce-cart-restriction\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"WooCommerce Cart Restriction\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/5deb39718ae59fa5659a825549b7df4a\",\"name\":\"Ayush Singh\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/3488792c5856a76629020ea17053d952aa9915d88309f1f38e901acce88408d2?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/3488792c5856a76629020ea17053d952aa9915d88309f1f38e901acce88408d2?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"caption\":\"Ayush Singh\"},\"sameAs\":[\"http:\/\/webkul.com\"],\"url\":\"https:\/\/webkul.com\/blog\/author\/ayush-singh265\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"WooCommerce Cart Restriction - Webkul Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webkul.com\/blog\/woocommerce-cart-restriction\/","og_locale":"en_US","og_type":"article","og_title":"WooCommerce Cart Restriction - Webkul Blog","og_description":"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,\u00a0\u00a0customer is bound to either remove that very product A from cart or place order for [...]","og_url":"https:\/\/webkul.com\/blog\/woocommerce-cart-restriction\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2017-02-18T07:23:34+00:00","article_modified_time":"2023-05-31T06:15:55+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png","type":"image\/png"}],"author":"Ayush Singh","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Ayush Singh","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/woocommerce-cart-restriction\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/woocommerce-cart-restriction\/"},"author":{"name":"Ayush Singh","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/5deb39718ae59fa5659a825549b7df4a"},"headline":"WooCommerce Cart Restriction","datePublished":"2017-02-18T07:23:34+00:00","dateModified":"2023-05-31T06:15:55+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/woocommerce-cart-restriction\/"},"wordCount":306,"commentCount":3,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"keywords":["cart","Cart Restriction","WooCommerce","WooCommerce Cart","wordpress"],"articleSection":["WooCommerce","WordPress"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/woocommerce-cart-restriction\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/woocommerce-cart-restriction\/","url":"https:\/\/webkul.com\/blog\/woocommerce-cart-restriction\/","name":"WooCommerce Cart Restriction - Webkul Blog","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"datePublished":"2017-02-18T07:23:34+00:00","dateModified":"2023-05-31T06:15:55+00:00","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/woocommerce-cart-restriction\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/woocommerce-cart-restriction\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/woocommerce-cart-restriction\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"WooCommerce Cart Restriction"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/5deb39718ae59fa5659a825549b7df4a","name":"Ayush Singh","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/3488792c5856a76629020ea17053d952aa9915d88309f1f38e901acce88408d2?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3488792c5856a76629020ea17053d952aa9915d88309f1f38e901acce88408d2?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","caption":"Ayush Singh"},"sameAs":["http:\/\/webkul.com"],"url":"https:\/\/webkul.com\/blog\/author\/ayush-singh265\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/75165","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/111"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=75165"}],"version-history":[{"count":10,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/75165\/revisions"}],"predecessor-version":[{"id":384663,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/75165\/revisions\/384663"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=75165"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=75165"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=75165"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}