Site icon Hip-Hop Website Design and Development

change a WooCommerce Subscription Deposit and Month-to-month Cost? [closed]

A buyer needs some particular/bespoke reductions to the subscription service they promote. I can not discover a plugin to do what they need, so thought I’d code it myself.

However I can not discover how one can edit the preliminary cost merchandise within the cart for subscriptions.

Utilizing woocommerce_before_calculate_totals I can edit the month-to-month quantity. However the preliminary cost doesn’t change. Though it’s looping its value under as a result of I can output it to the display screen, so I believe $cart_item[‘data’]->set_price( $new_price ) shouldn’t be altering the worth of the preliminary cost.

I can change it by way of woocommerce_cart_subtotal, however I cant see how one can get the unique value in that perform to make the adjustment. However then that doesn’t go on to vary tax and whole anyway.

For information, the code under doesn’t use $cart_item[‘data’]->get_price() as a result of woocommerce_before_calculate_totals appears to be referred to as twice which spoils my calculations.

perform set_new_customer_discount( $cart )
{
    // Loop By way of cart objects
    foreach ( $cart->get_cart() as $key => $cart_item )
    {
        //Hook appears to be firing twice for some motive... Get the worth from the unique product knowledge, not from the cart...
        $item_id = $cart_item['data']->id;
        $product = wc_get_product( $item_id );
        $original_price = $product->get_price();
        $new_price = $original_price/2;
        $cart_item['data']->set_price( $new_price );
    }
}
add_action('woocommerce_before_calculate_totals', 'set_new_customer_discount', 20, 1 );

The basket appears to be like like this and I’m attempting to take for instance 10% off each the “To pay now” strains and the “Recurring Totals” strains:

Basket totals

To pay now

Subtotal    £400.00
VAT £80.00
Whole   £480.00

Recurring Totals

Subtotal    £577.80 / month for five months
VAT £115.56 / month for five months
Recurring Whole £693.36 / month for five months
First cost: ninth Could 2019