Skip to content

Instantly share code, notes, and snippets.

@reandimo
Last active May 19, 2021 16:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reandimo/69ab841eb036a460f74edb6a394b9ffd to your computer and use it in GitHub Desktop.
Save reandimo/69ab841eb036a460f74edb6a394b9ffd to your computer and use it in GitHub Desktop.
Woocommerce Restrinct Certain Categories Combinations
<?php
//Renan Diaz - 2018
//Woocommerce restrinct certain categories combinations in the Woocommerce cart
//Restrinctions Begin
add_action('woocommerce_before_single_product', 'fs_check_category_in_cart');
function fs_check_category_in_cart() {
// Set $cat_in_cart
$cat_in_cart = '';
// Loop through all products in the Cart
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = $cart_item['data'];
// If Cart has category "fotografia", save
if ( has_term( 'fotografia', 'product_cat', $product->get_id() ) ) {
$cat_in_cart = 'fotografia';
break;
}
// If Cart has category "diseno", save
elseif( has_term( 'diseno', 'product_cat', $product->get_id() ) ) {
$cat_in_cart = 'diseno';
break;
}
}
switch ($cat_in_cart) {
case 'fotografia':
global $product;
$terms = get_the_terms( $product->ID, 'product_cat' );
foreach ($terms as $term) {
$product_cat_id[] = $term->term_id;
break;
}
//If ID 148 is in cart, print notice
if( in_array( 148 , $product_cat_id ) ){
// Print a notice - Explain to customer why can't buy the product
$explain = '<p>Ha agregado productos de <b>Fotografía</b> al carrito, temporalmente no podemos aceptar compras conjuntas. Si desea comprar este producto debe finalizar primero el pedido actual e intentar nuevamente. Ofrecemos disculpas y estaremos trabajando para solventarlo lo mas pronto posible.</p>';
wc_print_notice( $explain, 'notice' );
}
add_filter('woocommerce_is_purchasable', 'dis_pur', 10, 2);
function dis_pur( $is_purchasable, $product ) {
//Not purchasable categories ID if cart has "diseno"
$not_purchasable_cat_ids = array( 148 );
$categories = get_the_terms($product->ID, 'product_cat');
//Check if current product ID is forbidden
foreach($categories AS $category)
{
if( in_array( $category->term_id, $not_purchasable_cat_ids ) )
{
return false;
}
return true;
}
}
break;
case 'diseno':
global $product;
//Get current product categories
$terms = get_the_terms( $product->ID, 'product_cat' );
foreach ($terms as $term) {
$product_cat_id[] = $term->term_id;
break;
}
//If ID 149 is in cart, print notice
if( in_array( 149 , $product_cat_id ) ){
// Print a notice - Explain to customer why can't buy the product
$explain = '<p>Ha agregado productos de <b>Diseño</b> al carrito, temporalmente no podemos aceptar compras conjuntas. Si desea comprar este producto debe finalizar primero el pedido actual e intentar nuevamente. Ofrecemos disculpas y estaremos trabajando para solventarlo lo mas pronto posible.</p>';
wc_print_notice( $explain, 'notice' );
}
add_filter('woocommerce_is_purchasable', 'dis_pur', 10, 2);
function dis_pur( $is_purchasable, $product ) {
//Not purchasable categories ID if cart has "diseno"
$not_purchasable_cat_ids = array( 149 );
$categories = get_the_terms($product->ID, 'product_cat');
//Check if current product ID is forbidden
foreach($categories AS $category)
{
if( in_array( $category->term_id, $not_purchasable_cat_ids ) )
{
return false;
}
return true;
}
}
break;
default:
//Default function...
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment