【问题标题】:Make coupon field mandatory for a product category in WooCommerce使 WooCommerce 中产品类别的优惠券字段为必填项
【发布时间】:2019-01-14 08:53:58
【问题描述】:

我正在尝试使优惠券字段在 Woocommerce 上对于产品类别是强制性的。我曾尝试使用the code from this answer,但它只适用于一组优惠券代码。我需要它才能使用任何有效的优惠券代码。

感谢您的帮助。

【问题讨论】:

  • 在问题中使用一些现有代码时,您应该始终添加答案链接并感谢作者。如果您不自定义此现有答案代码,请不要将其添加到您的问题中,而只需保留答案代码的链接即可。

标签: php wordpress woocommerce custom-taxonomy coupon


【解决方案1】:

当发现特定产品类别的购物车商品时,请尝试以下操作,使优惠券字段成为必填项:

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 文件中。测试和工作

【讨论】:

  • 谢谢,这正是我需要的。
【解决方案2】:

我想我会分享我的版本 - 它添加了一个必填复选框字段,以便您可以指定哪些优惠券有效此消息。使用了此处发布的解决方案和 one.

的组合
// 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' );
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-10
    • 2019-01-12
    • 2017-01-11
    • 2021-11-06
    • 2017-12-29
    • 1970-01-01
    • 2021-01-21
    • 1970-01-01
    相关资源
    最近更新 更多