1

i am changing the price of products in cart like this

   add_action('woocommerce_before_calculate_totals', 'set_custom_price',1000,1);
    function set_custom_price($cart_obj) {
        foreach ($cart_obj->get_cart() as $key => $value) {

        if($value['alredy_have_number'] == true) {
             $value['data']->set_price(0.90);
        }
      }
   }

It works fine for recurring price but I wants to change signup fee for subscription products. what function or hook can I use for that ?

1
add_action( 'woocommerce_before_calculate_totals', 'change_subscription_signup_fee', 1000, 1 );
function change_subscription_signup_fee( $cart ) {
    if (is_admin() && !defined('DOING_AJAX'))
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item ) {

        // Check that product custom cart item data "alredy_have_number" exist and is true
        if( isset($cart_item['alredy_have_number']) && $cart_item['alredy_have_number'] ) {


            // Check if subscription products
            if ( in_array( $cart_item['data']->get_type(), ['subscription', 'subscription_variation']) ) {
                // Change subscription Sign up fee
                $cart_item['data']->update_meta_data('_subscription_sign_up_fee', 0.90);
            }
        }
    }
}
2
  • 1
    Thanks for your contribution. Please note, that not all code is easy to understand for everyone. Please take your time to explain in plain text what your code is doing, which goal it achieves and why it solves the OPs problem. Thanks.
    – kaiser
    May 2 '20 at 0:42
  • thanks man... this line saved me $cart_item['data']->update_meta_data('_subscription_sign_up_fee', 3.40);
    – moneeb
    May 2 '20 at 19:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.