php - 从 WooCommerce 中的所有优惠券中排除产品

标签 php wordpress woocommerce product coupon

我已经在互联网上搜索了所有内容,但尽管无数人问这个问题,但似乎在任何地方都没有任何答案。

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

我知道你可以在优惠券级别上做到这一点,但这相当困惑,特别是因为很多人都有自动优惠券,几个人创建优惠券等......

TLDR:任何使产品从所有优惠券中排除的方法 在产品级别 .

最佳答案

这是自动化此过程的好方法。

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

custom checkbox in product general settings metabox

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

2) 所选产品将被排除在产品级别的优惠券折扣之外,产品折扣金额将设置为零。

编码:

// 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 文件或任何插件文件中。

经测试,完美运行

关于php - 从 WooCommerce 中的所有优惠券中排除产品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47598528/

相关文章:

php mysql 下拉列表

php - 从 GET 请求中删除等号 "="

php - 如何在php中的数组的父项下显示子项

wordpress - Arrowchat聊天框打开修改

javascript - 如何在 facebook Pixel javascript 代码中使用 WordPress 自定义插件选项的值

php - 使用 WooCommerce 产品搜索搜索特定 post_type 表单 post_type 列时出错

PHP is_readable 失败但 opendir 有效

php - 如何获取我的表中的另一个表列?

php - 在 woocommerce 中使用 ajax 从购物车中删除变量产品

wordpress - 按类别 ID 获取产品