首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自定义优惠券类型woocommerce wordpress

自定义优惠券类型woocommerce wordpress
EN

Stack Overflow用户
提问于 2018-10-18 20:39:52
回答 2查看 2K关注 0票数 2

我需要创建自定义优惠券类型。因为我有自定义优惠券的自定义计算。外面的任何人都能帮我。

优惠券计算:购物车有一个产品,值为5000,然后应用自定义优惠券代码。购物车总数需要更改2000。同样,购物车有2个产品,价值为7000。如果应用自定义优惠券代码,则购物车总数需要为4000。

因此,优惠券需要使购物车总数为一个产品的平价2000

EN

回答 2

Stack Overflow用户

发布于 2018-10-18 21:16:13

请在活动主题的function.php中使用以下代码

代码语言:javascript
复制
function custom_discount_type( $discount_types ) {
    $discount_types['custom_discount'] =__( 'custom discount', 'woocommerce' );
    return $discount_types;
    }

// add the hooks
add_filter( 'woocommerce_coupon_discount_types', 'custom_discount_type',10, 1);

//function to get coupon amount for "custom_discount"
function woocommerce_coupon_get_discount_amount($discount, $discounting_amount, $cart_item, $single, $coupon) {

        if ($coupon->code == 'custom'){
        //echo "yes custom discount"; //if $coupon->type == 'fixed_cart' or 'percent' or 'fixed_product' or 'percent_product' The code Works
            $discount = $cart_item['quantity'] * 2000;
            return $discount;
            } else {
             return $discount;
            }
        }
//add hook to coupon amount hook
add_filter('woocommerce_coupon_get_discount_amount', 'woocommerce_coupon_get_discount_amount', 10, 5);

注:请在此行if ($coupon->code == 'your_added_coupon_here')上添加您添加的优惠券名称

工作正常且经过测试。

票数 2
EN

Stack Overflow用户

发布于 2020-11-25 13:13:41

在较新版本的WooCommerce中,您需要

请注册自定义优惠券类型并验证coupon

  • Calculate并应用

注册自定义优惠券类型

代码语言:javascript
复制
add_filter( 'woocommerce_coupon_discount_types', 'custom_coupon_type',10, 1);
function custom_coupon_type( $discount_types ) {        
    $discount_types['my_type'] =__( 'My New Coupon Type', 'woocommerce' );  
    return $discount_types;
}

验证优惠券

代码语言:javascript
复制
add_filter('woocommerce_coupon_is_valid_for_product', 'validate_custom_coupon', 10, 4);
function validate_custom_coupon($valid, $product, $coupon, $values){
    if ( ! $coupon->is_type( array( 'my_type' ) ) ) {
        return $valid;
    }

    $product_cats = wp_get_post_terms( $product->id, 'product_cat', array( "fields" => "ids" ) );
    
    // SPECIFIC PRODUCTS ARE DISCOUNTED
    if ( sizeof( $coupon->product_ids ) > 0 ) {
        if ( in_array( $product->id, $coupon->product_ids ) || ( isset( $product->variation_id ) && in_array( $product->variation_id, $coupon->product_ids ) ) || in_array( $product->get_parent(), $coupon->product_ids ) ) {
            $valid = true;
        }
    }

    // CATEGORY DISCOUNTS
    if ( sizeof( $coupon->product_categories ) > 0 ) {
        if ( sizeof( array_intersect( $product_cats, $coupon->product_categories ) ) > 0 ) {
            $valid = true;
        }
    }

    // IF ALL ITEMS ARE DISCOUNTED
    if ( ! sizeof( $coupon->product_ids ) && ! sizeof( $coupon->product_categories ) ) {            
        $valid = true;
    }
    
    // SPECIFIC PRODUCT IDs EXLCUDED FROM DISCOUNT
    if ( sizeof( $coupon->exclude_product_ids ) > 0 ) {
        if ( in_array( $product->id, $coupon->exclude_product_ids ) || ( isset( $product->variation_id ) && in_array( $product->variation_id, $coupon->exclude_product_ids ) ) || in_array( $product->get_parent(), $coupon->exclude_product_ids ) ) {
            $valid = false;
        }
    }
    
    // SPECIFIC CATEGORIES EXLCUDED FROM THE DISCOUNT
    if ( sizeof( $coupon->exclude_product_categories ) > 0 ) {
        if ( sizeof( array_intersect( $product_cats, $coupon->exclude_product_categories ) ) > 0 ) {
            $valid = false;
        }
    }

    // SALE ITEMS EXCLUDED FROM DISCOUNT
    if ( $coupon->exclude_sale_items == 'yes' ) {
        $product_ids_on_sale = wc_get_product_ids_on_sale();

        if ( isset( $product->variation_id ) ) {
            if ( in_array( $product->variation_id, $product_ids_on_sale, true ) ) {
                $valid = false;
            }
        } elseif ( in_array( $product->id, $product_ids_on_sale, true ) ) {
            $valid = false;
        }
    }

    return $valid;
}

计算并应用折扣

代码语言:javascript
复制
add_filter('woocommerce_coupon_get_discount_amount', 'wc_cpn_disc', 10, 5);
function wc_cpn_disc($discount, $discounting_amount, $cart_item, $single, $coupon) {
    // IF TYPE MATCHES PERFORM CUSTOM CALCULATION
    if ($coupon->type == 'my_type')
        $discount = $cart_item['quantity'] * 2000;        
    
    return $discount;
}

资料来源:

this exact question

的应答

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

https://stackoverflow.com/questions/52874269

复制
相关文章

相似问题

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