我有一個醫學網站,客戶來這里購買產品。
有 2 個頁面我制作了組合,如果有任何客戶,但來自這些組合/頁面的產品,我想禁用優惠券。請告訴我..
uj5u.com熱心網友回復:
Woocommerce 內置了優惠券設定來執行此操作。從優惠券限制欄位中排除這些產品。檢查此螢屏截圖:https ://snipboard.io/pug8Oo.jpg
uj5u.com熱心網友回復:
你可以使用這樣的東西。
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;
}
uj5u.com熱心網友回復:
如果您有多種產品,那么有多種選擇。
- 在產品頁面上創建禁用優惠券欄位。將以下代碼放入您的 chid 主題的 functions.php 檔案中。學分
// 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;
}
- 如果要禁用所有優惠券,請添加此代碼。學分
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();
- 如果您使用的是 Yith 禮品卡插件,請使用此代碼。學分
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' );
}
}
}
- 此選項可以隱藏特定產品類別的優惠券欄位。為此使用下面的代碼。學分
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;
}
我希望這將幫助您實作所需的輸出。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/465659.html
標籤:php WordPress woocommerce 钩woocommerce
上一篇:在foreach回圈中顯示WooCommerce產品SKU(ACF關系欄位)
下一篇:為什么PHP函式回傳TRUE?
