I m having a Medicine website, where customers comes and buy products..
There is 2 pages where i made combos, I want to disable coupons if any customer but products from those combos/page. Please let me know..
CodePudding user response:
Woocommerce have built in settings for coupon to do this. Exclude those products from coupon restrictions field. Check this screenshot: https://snipboard.io/pug8Oo.jpg
CodePudding user response:
You can use something like this.
add_filter( ‘woocommerce_coupon_is_valid_for_product’, ‘sw_wc_apfs_disable_on_susbcription’, 10, 4 );
function sw_wc_apfs_disable_on_susbcription( $is_valid, $product, $instance, $values ) {
if ( ! empty( $values[ ‘wcsatt_data’][ ‘active_subscription_scheme’ ] ) ) {
$is_valid = false;
}
return $is_valid;
}
CodePudding user response:
If you have multiple products, then there are multiple options for this.
- Create disable coupon field on product page. Put following code in your chid theme's functions.php file. Credits
// Create and display the custom field in product general setting tab
add_action( 'woocommerce_product_options_general_product_data', 'add_custom_field_general_product_fields' );
function add_custom_field_general_product_fields(){
global $post;
echo '<div >';
// Custom Product Checkbox Field
woocommerce_wp_checkbox( array(
'id' => '_disabled_for_coupons',
'label' => __('Disabled for coupons', 'woocommerce'),
'description' => __('Disable this products from coupon discounts', 'woocommerce'),
'desc_tip' => 'true',
) );
echo '</div>';;
}
// Save the custom field and update all excluded product Ids in option WP settings
add_action( 'woocommerce_process_product_meta', 'save_custom_field_general_product_fields', 10, 1 );
function save_custom_field_general_product_fields( $post_id ){
$current_disabled = isset( $_POST['_disabled_for_coupons'] ) ? 'yes' : 'no';
$disabled_products = get_option( '_products_disabled_for_coupons' );
if( empty($disabled_products) ) {
if( $current_disabled == 'yes' )
$disabled_products = array( $post_id );
} else {
if( $current_disabled == 'yes' ) {
$disabled_products[] = $post_id;
$disabled_products = array_unique( $disabled_products );
} else {
if ( ( $key = array_search( $post_id, $disabled_products ) ) !== false )
unset( $disabled_products[$key] );
}
}
update_post_meta( $post_id, '_disabled_for_coupons', $current_disabled );
update_option( '_products_disabled_for_coupons', $disabled_products );
}
// Make coupons invalid at product level
add_filter('woocommerce_coupon_is_valid_for_product', 'set_coupon_validity_for_excluded_products', 12, 4);
function set_coupon_validity_for_excluded_products($valid, $product, $coupon, $values ){
if( ! count(get_option( '_products_disabled_for_coupons' )) > 0 ) return $valid;
$disabled_products = get_option( '_products_disabled_for_coupons' );
if( in_array( $product->get_id(), $disabled_products ) )
$valid = false;
return $valid;
}
// Set the product discount amount to zero
add_filter( 'woocommerce_coupon_get_discount_amount', 'zero_discount_for_excluded_products', 12, 5 );
function zero_discount_for_excluded_products($discount, $discounting_amount, $cart_item, $single, $coupon ){
if( ! count(get_option( '_products_disabled_for_coupons' )) > 0 ) return $discount;
$disabled_products = get_option( '_products_disabled_for_coupons' );
if( in_array( $cart_item['product_id'], $disabled_products ) )
$discount = 0;
return $discount;
}
- If you want to disable all coupons, then add this code. Credits
function bulk_edit_coupon_restrictions(){
// Only for admin users (not accessible) for other users)
if( ! current_user_can( 'manage_options' ) ) return;
global $wpdb;
// Set HERE the product IDs without discount
$product_ids = array( 37, 67 );
$product_ids = implode( ',', $product_ids ); // String conversion
// SQL query: Bulk update coupons "excluded product" restrictions
$wpdb->query( "
UPDATE {$wpdb->prefix}postmeta as pm
SET pm.meta_value = '$product_ids'
WHERE pm.meta_key = 'exclude_product_ids'
AND post_id IN (
SELECT p.ID
FROM {$wpdb->prefix}posts as p
WHERE p.post_type = 'shop_coupon'
AND p.post_status = 'publish'
)
" );
}
// Run this function once (and comment it or remove it)
bulk_edit_coupon_restrictions();
- If you are using Yith Gift Cards Plugin, then use this code. Credits
if( defined('YITH_YWGC_PREMIUM') ){
if( !function_exists('yith_wcgc_deny_coupon_on_gift_card') ){
add_action('woocommerce_applied_coupon','yith_wcgc_deny_coupon_on_gift_card');
function yith_wcgc_deny_coupon_on_gift_card( $coupon_code ){
global $woocommerce;
$the_coupon = new WC_Coupon( $coupon_code );
$excluded_items = $the_coupon->get_excluded_product_ids();
$items = $woocommerce->cart->get_cart();
foreach ( $items as $item ):
if( has_term('gift-card','product_type',$item['product_id']) == true ):
$excluded_items[] = $item['product_id'];
endif;
endforeach;
$the_coupon->set_excluded_product_ids($excluded_items);
$the_coupon->save();
wc_add_notice( 'Coupon cannot applied to gift card product', 'error' );
}
}
}
- This option can be hiding coupon field for specific product category. Use below code for this. Credits
add_filter( 'woocommerce_coupons_enabled', 'conditionally_hide_cart_coupon_field' );
function conditionally_hide_cart_coupon_field( $enabled ) {
// Set your special category name, slug or ID here:
$special_cat = array('clothing');
$bool = false;
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$wc_product = $cart_item['data'];
// Woocommerce compatibility
$product_id = method_exists( $wc_product, 'get_id' ) ? $wc_product->get_id() : $wc_product->id;
$main_product_id = $cart_item['variation_id'] > 0 ? $cart_item['product_id'] : $product_id;
if ( has_term( $special_cat, 'product_cat', $main_product_id ) )
$bool = true;
}
if ( $bool && is_cart() ) {
$enabled = false;
}
return $enabled;
}
I hope this will help you to achieve desired outputs.