在 WooCommerce 中将优惠券字段设为产品类别的必填项

Make coupon field mandatory for a product category in WooCommerce

我正在努力使优惠券字段在 Woocommerce 上对于产品类别是强制性的。我试过使用 但它只适用于一组优惠券代码。我需要它与任何有效的优惠券代码一起使用。

感谢您的帮助。

尝试以下方法,当找到特定产品类别的购物车商品时,优惠券字段必填:

add_action( 'woocommerce_check_cart_items', 'mandatory_coupon_code' );
function mandatory_coupon_code() {
     // Set your product categories in the array (can be term IDs, slugs or names)
    $product_categories = array( 'clothing' );

    $found = false;

    // Loop through cart items to check for the product categories
    foreach ( WC()->cart->get_cart() as $cart_item ){
        if( has_term( $product_categories, 'product_cat', $cart_item['product_id'] ) ){
            $found = true; // cart item from the product category is found
            break; // We can stop the loop
        }
    }

    // If not found we exit
    if( ! $found ) return; // exit

    $applied_coupons = WC()->cart->get_applied_coupons();

    // Coupon not applied and product category found
    if( is_array($applied_coupons) && sizeof($applied_coupons) == 0 ) {
        // Display an error notice preventing checkout
        $message = __( 'Please enter a coupon code to be able to checkout.', 'woocommerce' );
        wc_add_notice( $message, 'error' );
    }
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。测试和工作

我想分享我的版本 - 它添加了一个必填的复选框字段,因此您可以指定激活此消息的优惠券。结合使用此处发布的解决方案和此

// Add a custom checkbox to Admin coupon settings pages
add_action( 'woocommerce_coupon_options', 'add_coupon_option_checkbox', 10 );
function add_coupon_option_checkbox() {
    woocommerce_wp_checkbox( array(
        'id'            => 'items_mandatory',
        'label'         => __( 'Force specific items', 'woocommerce' ),
        'description'   => __( 'Make this coupon mandatory for specific items.', 'woocommerce' ),
        'desc_tip'      => false,
    ) );
}


// Save the custom checkbox value from Admin coupon settings pages
add_action( 'woocommerce_coupon_options_save', 'save_coupon_option_checkbox', 10, 2 );
function save_coupon_option_checkbox( $post_id, $coupon ) {
    update_post_meta( $post_id, 'items_mandatory', isset( $_POST['items_mandatory'] ) ? 'yes' : 'no' );
}


// Force Coupon codes for Woocommerce
add_action( 'woocommerce_check_cart_items', 'mandatory_coupon_code' );
function mandatory_coupon_code() {
     // Set your product categories in the array (can be term IDs, slugs or names)
    $product_categories = array( '58');
    $applied_coupons = WC()->cart->get_applied_coupons();
    $found = false;
    
    if( sizeof($applied_coupons) > 0 ) {
        // Loop through applied coupons
        foreach( $applied_coupons as $coupon_code ) {
            $coupon = new WC_Coupon( $coupon_code );
            if( $coupon->get_meta('items_mandatory') === 'yes' ) {
                $coupon_applied = true;
                break;
            }
        }
    }
    // Loop through cart items to check for the product categories
    foreach ( WC()->cart->get_cart() as $cart_item ){
        if( has_term( $product_categories, 'product_cat', $cart_item['product_id'] ) ){
            $found = true; // cart item from the product category is found
            break; // We can stop the loop
        }
    }

    // If not found we exit
    if( ! $found ) return; // exit
    
    // Coupon not applied and product category found
    
    if( ! $coupon_applied) {
        // Display an error notice preventing checkout
        $message = __( 'The product "%s" requires a coupon for checkout.' );
        wc_add_notice( sprintf($message, $cart_item['data']->get_name() ), 'error' );
    }
}