php 优惠券折扣的产品正常价格(不是销售价格)

gev0vcfq  于 2023-06-04  发布在  PHP
关注(0)|答案(2)|浏览(177)

在WooCommerce中,我有常规价格为X,销售价格为Y的产品。我想添加一个优惠券的代码为$45.00折扣要采取从常规价格X.
我想优惠券无视销售价格,所以我得到X-45美元不是Y-45美元。但是当不使用优惠券时,使用价格Y。
我发现下面的工程百分比折扣,但我似乎不能使它的工作为一个固定的产品折扣价格。

add_filter('woocommerce_coupon_get_discount_amount', 'woocommerce_coupon_get_discount_amount', 10, 5 );
function woocommerce_coupon_get_discount_amount( $discount, $discounting_amount, $cart_item, $single, $coupon ) {
    if ($coupon->type == 'percent_product' || $coupon->type == 'percent') {
        global $woocommerce;
        $cart_total = 0;

        foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $cart_item ) {

         $variable_product1= new WC_Product_Variation( $cart_item["variation_id"] );

         $cart_total += $variable_product1 ->regular_price * $cart_item['quantity'];
        } 
        $discount = round( ( $cart_total / 100 ) * $coupon->amount, $woocommerce->cart->dp );
        return $discount;
    }
    return $discount;
}
w41d8nur

w41d8nur1#

这似乎是检查优惠券,然后在应用优惠券之前将购物车价格更改为正常价格而不是折扣价格(从本应用程序中的woocommerce动态定价获得)。放置在functions.php中

add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 10, 1);
function add_custom_price( $cart_object) {

    global $woocommerce;

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $coupon = False;

    if ($coupons = WC()->cart->get_applied_coupons()  == False ) 
      $coupon = False;
    else {
        foreach ( WC()->cart->get_applied_coupons() as $code ) {
          $coupons1 = new WC_Coupon( $code );
          if ($coupons1->type == 'percent_product' || $coupons1->type == 'percent')
            $coupon = True;
        }
    }

    if ($coupon == True)
        foreach ( $cart_object->get_cart() as $cart_item ) 
        {
            $price = $cart_item['data']->regular_price;
            $cart_item['data']->set_price( $price );
        }
}
f87krz0w

f87krz0w2#

这里有一个更完整的答案的基础上JSE的检查,如果每个产品的资格,然后恢复价格到非销售之一。因此,如果优惠券仅限于特定的产品或猫,或被排除在任何,它将采取相应的行动。对于没有限制的优惠券,它会像预期的那样穿过整个购物车。

function regular_price_for_coupon_items($cart_object){
    if (!function_exists('WC') || !isset(WC()->cart)){
        return;
    }

    if (is_admin() && !defined('DOING_AJAX')){
        return;
    }

    // Check if cart has coupon.
    if (!WC()->cart->has_discount()){
        return;
    }

    // Get coupons.
    $coupons = WC()->cart->get_applied_coupons();

    // Loop through coupons.
    foreach ($coupons as $coupon){
        // Create coupon object.
        $coupon = new WC_Coupon($coupon);

        foreach ($cart_object->get_cart() as $cart_item){
            // Check if products are not resticted from this coupon
            $included_products = true;
            $included_cats = true;

            if (count($product_ids = $coupon->get_product_ids()) > 0){
                if (!in_array($cart_item['product_id'], $product_ids, false)){
                    $included_products = false;
                }
            }
            if (count($exluded_product_ids = $coupon->get_excluded_product_ids()) > 0){
                if (in_array($cart_item['product_id'], $exluded_product_ids, false)){
                    $included_products = false;
                }
            }

            if (count($product_cats = $coupon->get_product_categories()) > 0){
                if (!has_term($product_cats, 'product_cat', $cart_item['product_id'])){
                    $included_cats = false;
                }
            }
            if (count($excluded_product_cats = $coupon->get_excluded_product_categories()) > 0){
                if (has_term($excluded_product_cats, 'product_cat', $cart_item['product_id'])){
                    $included_cats = false;
                }
            }

            if ($included_products && $included_cats){
                $price = $cart_item['data']->get_regular_price();
                $cart_item['data']->set_price($price);
            }
        }
    }
}
add_action('woocommerce_before_calculate_totals', 'regular_price_for_coupon_items', PHP_INT_MAX, 1);

如果你想排除特定类型的优惠券,你可以在优惠券foreach循环的if语句中使用$coupon->get_discount_type(),就在优惠券对象被示例化之后,如果它不是你想操作的优惠券类型,“继续”迭代到下一个。
WooCommerce应该涵盖这个功能没有的基础。例如将其设置为固定购物车折扣,其中至少有一个项目不符合基于每个优惠券的限制的其他场景。如果我错过了什么,请让我知道!

相关问题