• Resolved shikha1121

    (@shikha1121)


    Hi,

    I added some code for annual billing & monthly billing by radio button selection. My code is working both on cart & checkout page. But by applying my code , other pages of website are not working
    Please check my code:-

    add_action( 'woocommerce_review_order_before_payment', 'checkout_radio_choice' );
      
    function 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' ),
       'options' => array(
          '0' => 'Monthly Billing',
          '10' => 'Annual Billing',
          
       ),
       'default' => $chosen
       );
         
       echo '<div id="checkout-radio">';
       echo '<h5>Billing Option</h5>';
       woocommerce_form_field( 'radio_choice', $args, $chosen );
       echo '</div>';
         
    }
    
       
    add_action( 'woocommerce_get_price_html', 'checkout_radio_choice_fee', 9999, 2 );
      
    function checkout_radio_choice_fee( $cart ) {
    
       $orig_price = wc_get_price_to_display( $product );
       if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
        
       $radio = WC()->session->get( 'radio_chosen' );
        if ( $radio == "10") {
    $orig_price = wc_get_price_to_display( $product );
    $price_html = wc_price( $orig_price*12 ); 
    
     }
    else{  
    $orig_price = wc_get_price_to_display( $product );
    $price_html = $product->get_price();
     
     }
       return $price_html;
    }
    add_action( 'woocommerce_before_calculate_totals', 'alter_price_cart', 9999 );
     
    function alter_price_cart( $cart ) {
     
        if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
     
        if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;
     
       $radio = WC()->session->get( 'radio_chosen' );
     
      
        foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
     
        if ( $radio == "10") {
            $product = $cart_item['data'];
            $price = $product->get_price();
    
            $cart_item['data']->set_price( $price *12 );}
    else{   $product = $cart_item['data'];
            $price = $product->get_price();
    
            $cart_item['data']->set_price( $price);exit;}
    
    }
    
     
    }
    
      
    add_action( 'woocommerce_checkout_update_order_review', 'checkout_radio_choice_set_session' );
      
    function 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)
  • Mirko P.

    (@rainfallnixfig)

    Hi @shikha1121,

    This forum is intended to support the default functionality of WooCommerce, so there aren’t as many developers on here as other options.

    For this type of questions, you’re more likely to get input by asking in the WooCommerce Developer Slack or Advanced WooCommerce Facebook group. As those communities are both frequented by developers.

    However, I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.

    Thanks.

    Mirko P.

    (@rainfallnixfig)

    Hi there,

    We’ve not seen any activity on this thread for a while, so I’m marking this thread as resolved.

    Hopefully, you were able to find a solution and the above resources for developers were helpful. If you have further questions, please feel free to open a new topic.

    Thanks.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Override Product Price Programmatically’ is closed to new replies.