首页 > 解决方案 > WooCommerce 优惠券从正常价格计算

问题描述

我需要“朋友”客户的功能,他们可以以更优惠的价格获得产品。基于用户角色的解决方案不是我想要的,客户希望它与优惠券一起使用。他们向客户提供优惠券代码,例如,他们可以从某些产品的常规价格中获得 8 欧元的折扣。问题是,默认情况下 WooCommerce 优惠券从最低价格计算。如果设置了销售价格,那么这个计算是错误的,客户得到的价格太便宜了。基本上,我想要优惠券给某些产品固定的“朋友”价格。

我一直在谷歌搜索,找不到现成的解决方案。欢迎任何有关如何解决此问题的建议。

我检查了多个插件,但没有一个能满足我的要求。

标签: wordpresswoocommerce

解决方案


添加到functions.php。基于此代码https://brilliantinfo.net/apply-coupon-discount-on-regular-price-in-woocommerce/

针对固定产品的优惠券类型进行了修改。如果您需要它仅适用于特定优惠券,那么我可以为此进行修改。(就目前而言,这将适用于使用的任何固定产品优惠券)

    add_action( 'woocommerce_before_calculate_totals', 'adjust_cart_coupon', 10, 1);
    function adjust_cart_coupon( $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 == 'fixed_product'){
            $coupon = True;
          }
        }
      }

      if ($coupon == True){
        foreach ( $cart_object->get_cart() as $cart_item ){
          $price = $cart_item['data']->regular_price;
          //sets cart item to use regular price and not sale price
          $cart_item['data']->set_price( $price );
        }
      }
    }

推荐阅读