【问题标题】:WooCommerce Non-Recurring Subscription FeesWooCommerce 非经常性订阅费
【发布时间】:2018-06-03 09:00:03
【问题描述】:

我在 WooCommerce 中使用 WooCommerce 订阅插件。我正在尝试编写一个函数,当满足以下条件之一时,对 普通产品 执行 10% 的折扣:

  1. 用户有一个有效的订阅
  2. 用户的购物车中有订阅产品
function vip_discount() {

    $woocommerce = WC();
    $items = $woocommerce->cart->get_cart();
    $vip_product_id = get_subscription_product_id();
    $is_vip_in_cart = is_in_cart($vip_product_id);
    $vip_product_price = 0;

    foreach ($items as $item) {
        if( $item['variation_id'] === get_subscription_variation_id('monthly') || $item['variation_id'] === get_subscription_variation_id('quarterly') || $item['variation_id'] === get_subscription_variation_id('annually') ) {
            $vip_product_price = $item['line_total'];
        }
    }

    if ( wcs_user_has_subscription( '', '', 'active' ) || $is_vip_in_cart ) {
        // Make sure that the calculation is a negative number at the end ALWAYS!
        $discount = -( 10 / 100 ) * ( $woocommerce->cart->get_displayed_subtotal() - $vip_product_price);
        print_r($discount);
        $woocommerce->cart->add_fee( 'VIP Discount', $discount );
    }
}
add_action( 'woocommerce_cart_calculate_fees', 'vip_discount' );

问题是这个钩子由于某种原因运行了两次。它也没有应用正确的费用。它应该从负应用的费用中减去经常性项目总数,而不是费用最终作为订阅(经常性)产品价格本身。

感谢任何补充信息或帮助。

【问题讨论】:

    标签: php wordpress woocommerce cart woocommerce-subscriptions


    【解决方案1】:

    首先如果你使用$woocommerce,你首先需要global $woocommerce;。最好使用WC(),因为这是同一件事的实际方式。

    使用 woocommerce_cart_calculate_fees 动作挂钩时,您的挂钩函数中缺少参数 $cart WC_Cart 对象)

    函数get_subscription_product_id() 没有退出,所以它可能是一个自定义函数……我已经用别的东西替换了它。

    您应该需要使用cart_contents_total 而不是显示的小计,因为此挂钩在总计计算之前运行。

    试试这个重新访问的类似代码:

    add_action( 'woocommerce_cart_calculate_fees', 'vip_discount', 10, 1 );
    function vip_discount( $cart ) {
        if ( is_admin() && ! defined('DOING_AJAX') ) return; // Exit
    
        // Here the rate percentage of 10% to be applied
        $rate = .10;
    
        // Initializing variables
        $vip_price = $discount = 0;
        $subscription_in_cart = false;
    
        // Loop through the cart items
        foreach ($cart->get_cart() as $cart_item) {
            if( $cart_item['variation_id'] === get_subscription_variation_id('monthly') || $cart_item['variation_id'] === get_subscription_variation_id('quarterly') || $cart_item['variation_id'] === get_subscription_variation_id('annually') ) {
                $vip_price += $cart_item['line_total'];
            }
    
            // Get an instance of the parent product object (if not parent the product object)
            $product = wc_get_product( $cart_item['product_id']);
    
            // Check for simple or variable "subscription" products
            if( $product->is_type('subscription') || $product->is_type('variable-subscription') ){
                $subscription_in_cart = true;
            }
        }
    
        // If customer has an active subscription or a product subscription in cart
        if ( wcs_user_has_subscription( '', '', 'active' ) || $subscription_in_cart ) {
    
            // The discount calculation
            $discount = ( $cart->cart_contents_total - $vip_product_price ) * $rate;
    
            if( $discount > 0 ){
                // Add a negative fee (a discount)
                $cart->add_fee( __("VIP Discount"), -$discount ); // not taxable here
            }
        }
    }
    

    此代码在您的活动子主题(或主题)的 function.php 文件中或任何插件文件中。

    这应该可行。

    【讨论】:

      猜你喜欢
      • 2017-01-29
      • 2016-10-02
      • 1970-01-01
      • 2017-02-01
      • 2012-10-08
      • 2019-06-30
      • 2016-06-26
      • 1970-01-01
      • 2014-02-01
      相关资源
      最近更新 更多