Automattic / woocommerce-subscriptions-core

Subscriptions core package for WooCommerce
Other
86 stars 32 forks source link

Please add a filter to maybe_add_fees #276

Open danieleperilli opened 1 year ago

danieleperilli commented 1 year ago

Description

Can you please add a filter (or transform the action to a filter) to the maybe_add_fees method of WCS_Cart_Renewal class? It would be helpful for our automatic discount plugin.

Current Code

public function maybe_add_fees( $cart ) {
     if ( $cart_item = $this->cart_contains() ) {

          [...]

          do_action( 'woocommerce_adjust_order_fees_for_setup_cart_for_' . $this->cart_item_key, $order, $cart );

          [...]
     }
}

Proposed Code

public function maybe_add_fees( $cart ) {
     if ( $cart_item = $this->cart_contains() ) {

          [...]

          $fees = apply_filters( 'woocommerce_adjust_order_fees_for_setup_cart_for_' . $this->cart_item_key, $order->get_fees(), $order, $cart);
          if ($fees) {

               [...]

          }
     }
}

Product impact

haszari commented 1 year ago

It would be helpful for our automatic discount plugin

Could you link to the plugin? Is it in on the WooCommerce Marketplace?

If you could provide more details about what you're trying to achieve that would help us understand the wider context. I'm assuming this is related to allowing merchants to automatically apply discounts to subscription products. If you could outline one or more use cases that will help illustrate the need. Thank you!

danieleperilli commented 1 year ago

Here is my scenario:

Both these plugin are developed internally, but in the future they could be publicly released.

Example:

  1. A user creates an order with ProductA x 5 and SubscriptionB x 1
  2. A volume discount of 10% is automatically applied to the cart (as negative fee)
  3. The user choose BACS as payment method - note that we always provide the pay-for-order URL in the order email, so if the user changes his mind later he can pay with CC without contacting us
  4. The user opens the pay-for-order link and, as the order contains a subscription, WCS loads the order fees (through maybe_add_fees) which is, in our case, only the volume discount
  5. The user changes the quantity of ProductA to 2 which no longer qualifies for the volume discount
  6. At this point we have a problem, since our plugin assign discounts automatically in the woocommerce_calculate_fees, but the stored volume discount has been already assigned by WCS's maybe_add_fees. So we need to remove all the fees and re-assign them one by one if they don't match the automatic discount that we want to remove. Note that we don't want to remove these fees from the order until is paid. For this reason, an additional filter would be helpful.