• Resolved jazzu

    (@jazzu)


    Hello there!

    I friend recently helped me and wrote this code for me, and it works great, however, something’s not right.

    We added 2 checkboxes to checkout, so a customer can choose if they want us to do the assembly or if they want to do it themselves. The checkboxes do exactly that.

    BUT, the problem is if there are more than 1 products. The assembly fee is fixed and it doesn’t matter if I have 1 product or 5 products – the price is still the same.
    What I need is the assembly fee to be multiplied by each product.

    This is the code that I have:

    // Part 1
    // Display Radio Buttons
    add_action( 'woocommerce_review_order_before_payment', 'ahirwp_checkout_radio_choice' );
    function ahirwp_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' => 'Brez monta?e',
          '170' => 'Monta?a - 170 €',
       ),
       'default' => $chosen
       );
       echo '<div id="checkout-radio">';
       echo '<h3>Monta?a v 14 dneh (prostor mora biti predpripravljen!)</h3>';
       woocommerce_form_field( 'radio_choice', $args, $chosen );
       echo '</div>';
    }
    // Part 2
    // Add Fee and Calculate Total
    add_action( 'woocommerce_cart_calculate_fees', 'ahirwp_checkout_radio_choice_fee', 20, 1 );
    function ahirwp_checkout_radio_choice_fee( $cart ) {
       if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
       $radio = WC()->session->get( 'radio_chosen' );
       if ( $radio ) {
          $cart->add_fee( 'Monta?a', $radio );
       }
    }
    // Part 3
    // Add Radio Choice to Session
    add_action( 'woocommerce_checkout_update_order_review', 'ahirwp_checkout_radio_choice_set_session' );
    function ahirwp_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'] );
        }
    }

    Please help me out!

    Kind regards

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

Viewing 5 replies - 1 through 5 (of 5 total)
  • Jazzu –

    I assume you are using radio buttons, not “checkboxes” as you describe in your post here. If that is in fact the case, why do you need 2 radio buttons? Can’t you simply have one checkbox called “Assemble it for me” (or something to that effect)?

    Also, buy “more than 1 product” do you mean more than 1 quantity of the same product, or for instance, 5 different products in the same cart?

    More details of your setup needed to understand what you are trying to do.

    Cheers,
    Jeff
    –––––––

    Hi @jazzu

    Just to add to the previous reply, have you considered using a plugin like Product Add-Ons? You can allow customers to add assembly service to each product before adding to cart.

    https://woocommerce.com/document/product-add-ons/

    Hmmm, now that I think about it, doesn’t the WC core include product variations? If so, why not make two products: one including assembly, and one without?

    You may also want to check Configurable Products. Here’s a case study.

    It’s been a while since we heard from you, so I’m marking this thread resolved. Hopefully, the above info was helpful.

    If you have further questions, please feel free to open a new topic.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Editing a function, so it displays PRODUCTS * FEE instead of PRODUCTS + FEE’ is closed to new replies.