• Resolved afaqahmed

    (@afaqahmed)


    We are trying to change woocommece cart totals ( Including subtotal) programatically using ‘woocommerce_cart_calculate_fees‘ hook. We have updated woocommerce to the latest version. Now the ‘total’ price in the cart not changing according to the changed value of subtotal and shipping, it always shows the default value.
    I have tried ‘woocommerce_after_calculate_totals’, ‘woocommerce_before_calculate_totals’, ‘woocommerce_calculate_totals’hooks.
    Have a look at my code:

    add_action( 'woocommerce_review_order_before_payment', 'bbloomer_checkout_radio_choice' );
      
    function bbloomer_checkout_radio_choice() {
         
       $chosen = WC()->session->get( 'radio_chosen' );
       $chosen = empty( $chosen ) ? WC()->checkout->get_value( 'radio_choice' ) : $chosen;
       $chosen = empty( $chosen ) ? '0' : $chosen;
            
       $args = array(
       'type' => 'radio',
       'class' => array( 'form-row-wide', 'update_totals_on_change' ),
       'required'    => true,
       'options' => array(
          '12' => 'Lámina digital en PDF ($12,00)',
          '22' => 'Lámina impresa + marco ($22,00)',
          '32' => 'Lámina impresa + marco + regalos ($32,00)',
       ),
       'default' => $chosen
       );
         
       echo '<div id="checkout-radio">';
       echo '<h3 style="color:#55c1ca ">Tipo de pack*</h3>';
       woocommerce_form_field( 'radio_choice', $args, $chosen );
       echo '</div>';
         
    }
      
    // Part 2 
    // Add Fee and Calculate Total
       
    add_action( 'woocommerce_cart_calculate_fees', 'bbloomer_checkout_radio_choice_fee', 10 );
      
    function bbloomer_checkout_radio_choice_fee( $cart ) {
       
       if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
        
       $radio = WC()->session->get( 'radio_chosen' );
         
       if ( $radio ) {
          //$cart_object->subtotal='Option Fee';
    	   //WC()->cart->subtotal=$radio;
    	   $cart->cart_contents_total = $radio;
    	   $cart->set_cart_contents_total($radio); 
    	   $cart->set_subtotal($radio);
    	    
    	   //WC()->cart->total=WC()->cart->get_cart_total();
    	   //$cart_total = WC()->cart->get_cart_contents_total();
    	   
    	   //calculate_totals( );
    	   $a=WC()->cart->get_cart_contents_total();
    	   $cart->add_fee( 'Option Fee', $radio );
       }
       
    }
      
    
    // Part 3 
    // Add Radio Choice to Session
      
    add_action( 'woocommerce_checkout_update_order_review', 'bbloomer_checkout_radio_choice_set_session' );
      
    function bbloomer_checkout_radio_choice_set_session( $posted_data ) {
        parse_str( $posted_data, $output );
        if ( isset( $output['radio_choice'] ) ){
            WC()->session->set( 'radio_chosen', $output['radio_choice'] );
        }
    }

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Failed to Programmatically Update Woocommerce Checkout Total’ is closed to new replies.