自定义优惠券类型woocommerce WordPress

时间:2018-10-18 12:39:52

标签: php wordpress woocommerce

我需要创建自定义优惠券类型。因为我对定制优惠券进行了定制计算。外面的人都帮我。

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

因此,优惠券需要使一种产品的购物车总价等于2000

2 个答案:

答案 0 :(得分:1)

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

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')上添加您添加的优惠券名称

工作正常且经过测试。

答案 1 :(得分:0)

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

  1. 注册自定义优惠券类型
  2. 验证优惠券
  3. 计算并应用折扣

注册自定义优惠券类型

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;
}

验证优惠券

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;
}

计算并应用折扣

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;
}

来源:

相关问题