首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从WooCommerce的所有优惠券中排除产品

问从WooCommerce的所有优惠券中排除产品
EN

Stack Overflow用户
提问于 2017-12-01 17:29:47
回答 1查看 6.7K关注 0票数 4

我在互联网上到处搜索,但似乎没有任何答案,尽管有无数人问了这个问题。

有没有办法从所有优惠券中排除特定的产品?

我知道你可以在优惠券上做这件事,但这很麻烦,特别是很多人都有自动优惠券,有几个人在制作优惠券等…。

TLDR::任何将产品排除在产品级别的优惠券之外的方法。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-02 01:50:32

这里有一个很好的方法来自动化这个过程。

1)在“产品一般设置”metabox中添加自定义复选框,以禁用当前产品的优惠券功能。因此,您将在后端编辑产品页面中获得此信息:

​

​

所有选定的产品将保存在一个数组中,并将在以下…中使用

( 2)本产品将不包括在产品层面的优惠券折扣,产品折扣额将设置为零。

守则:

代码语言:javascript
复制
// 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 class="product_custom_field">';

    // 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.php文件中,或者在任何插件文件中。

经过测试和完美的工作

票数 26
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47598528

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档