Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author YITHEMES

    (@yithemes)

    Hello there,
    hope you are doing well! ??

    We have developed custom code to allow restrict the use of gift cards depending on product categories.

    To achieve it, you have to copy the following code in the functions.php file of your active theme (wp-content/themes/your_active_theme/functions.php).

    if(!function_exists('yith_ywgc_check_gift_card_return_no_allow_subscription')) {
     function yith_ywgc_check_gift_card_return_no_allow_subscription($bool) {
     $items = WC()->cart->get_cart();
     foreach ($items as $cart_item_key => $values) {
     $product = $values['data'];
     
     $product_categories = array(20, 16);
     
     if (array_intersect($product_categories, $product->get_category_ids())) {
     $category_names = '';
     $categories = array_intersect($product_categories, $product->get_category_ids());
     
     foreach ($categories as $category) {
     $category_names .= end($categories) == $category ? get_the_category_by_ID($category) . '' : get_the_category_by_ID($category). ', ';
                    }
     
                    wc_add_notice(__('It is not possible to apply gift cards when the cart contains a product of ' . $category_names . ' category', 'yith-woocommerce-gift-cards'), 'error');
     
     $bool = false;
                }
            }
     
     return $bool;
        }
     
        add_filter('yith_ywgc_check_gift_card_return', 'yith_ywgc_check_gift_card_return_no_allow_subscription', 10);
    }

    Also, you have to fill the following array with the categories IDS you want to restrict when redeeming a gift card:

    $product_categories = array(20, 16);

    Could you check it and let us know if all is working properly for you, please?

    Have a nice day!

    Thread Starter semplicewebsite

    (@semplicewebsite)

    Thank you so much!! Tomorrow I’ll try this solution. You have save me… thank you again…

    Thread Starter semplicewebsite

    (@semplicewebsite)

    I managed to test the code but unfortunately it does not work … the product category is number 19, I inserted (19) instead of (20, 16) but when I go to add a different product category to the cart the gift card is applied the same

    Thread Starter semplicewebsite

    (@semplicewebsite)

    I don’t know why doesn’t work :'(

    Thread Starter semplicewebsite

    (@semplicewebsite)

    My Custom Code with the category product id is this:

    if(!function_exists('yith_ywgc_check_gift_card_return_no_allow_subscription')) {
     function yith_ywgc_check_gift_card_return_no_allow_subscription($bool) {
     $items = WC()->cart->get_cart();
     foreach ($items as $cart_item_key => $values) {
     $product = $values['data'];
     
     $product_categories = array(19);
     
     if (array_intersect($product_categories, $product->get_category_ids())) {
     $category_names = '';
     $categories = array_intersect($product_categories, $product->get_category_ids());
     
     foreach ($categories as $category) {
     $category_names .= end($categories) == $category ? get_the_category_by_ID($category) . '' : get_the_category_by_ID($category). ', ';
                    }
     
                    wc_add_notice(__('It is not possible to apply gift cards when the cart contains a product of ' . $category_names . ' category', 'yith-woocommerce-gift-cards'), 'error');
     
     $bool = false;
                }
            }
     
     return $bool;
        }
     
        add_filter('yith_ywgc_check_gift_card_return', 'yith_ywgc_check_gift_card_return_no_allow_subscription', 10);
    }
    
    
    Plugin Author YITHEMES

    (@yithemes)

    Hi there,
    hope you are doing well!

    Could you check if the problem is solved replacing the previous code with this new one, please?

    if(!function_exists('yith_ywgc_check_gift_card_return_no_allow_subscription')) {
    	function yith_ywgc_check_gift_card_return_no_allow_subscription($bool) {
    		$items = WC()->cart->get_cart();
    		foreach ($items as $cart_item_key => $values) {
    			$product = $values['data'];
    
    			$product_categories = array(19);
    
    			if (array_diff($product_categories, $product->get_category_ids())) {
    				$category_names = '';
    				$categories = array_intersect($product_categories, $product->get_category_ids());
    
    				foreach ($categories as $category) {
    					$category_names .= end($categories) == $category ? get_the_category_by_ID($category) . '' : get_the_category_by_ID($category). ', ';
    				}
    
    				wc_add_notice(__('Only is possible to apply gift cards when the cart contains a product of ' . $category_names . ' category', 'yith-woocommerce-gift-cards'), 'error');
    
    				$bool = false;
    			}
    		}
    
    		return $bool;
    	}
    
    	add_filter('yith_ywgc_check_gift_card_return', 'yith_ywgc_check_gift_card_return_no_allow_subscription', 10);
    }

    Please, note that it will only work if the gift card code is applied in the default plugin forms, not in the WooCommerce coupons forms.

    Let me know any news, please.

    Thread Starter semplicewebsite

    (@semplicewebsite)

    thanks for the availability … i will try this solution. I must, however, point out that in reality if I apply any Gift Card code in the coupon section, the same is applied … Unless you are telling me that this code inhibits the application of the GiftCard to some categories only if it is entered in the GiftCard section, because otherwise if a user were to make a mistake by entering the code in the coupon he could make it valid for each category even those excluded

    Thread Starter semplicewebsite

    (@semplicewebsite)

    Ok it works! But it is not good, because if I enter the code in the coupon form the GiftCard is applied indiscriminately … it follows that it is not a safe method. Thanks anyway for your work.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Gift card only for a certain category’ is closed to new replies.