0

I have a multi vendor platform. When someone order 2 products from one seller he has few ( lets say 2 ) options for delivery.

option 1: 10$ option 2: 20$

If the client choose option 1 for first product and option 2 for the second, total fee will be 10+20=30$. This works correct. If he choose option 1 for first and option 1 for second, total fee will be only 10$, because of the same options. This also works fine. But when he choose option 1 for frist, then option 2 for second ( it goes 10+20=30$ ) but then decide to choose for the second ( or the first one option 2. Otherwise to make it equal ) option 1 then the fee must become 10$ only because of the same fees. But it doesn't. It stays 30$. Seems like I can change the fee to higher value but can't lower it. This is my code:

add_action( 'wp_ajax_woo_get_ajax_data', 'bbloomer_checkout_radio_choice_set_session' );
add_action( 'wp_ajax_nopriv_woo_get_ajax_data', 'bbloomer_checkout_radio_choice_set_session' );

function bbloomer_checkout_radio_choice_set_session() {
    if ( isset($_POST['radio']) ){
        global $woocommerce;
        $totalRadio = 0.00;
        $outRadio = [];
        $vendorsIDshippingValue = [];
        $counter = 0;
        foreach ($_POST['radio'] as $key => $value) {
            $value = str_replace(".", "-", $value);
            $radio = sanitize_key( $value );
            $radio = str_replace("-", ".", $radio);
            $nameArray = explode("_", $key);
            $productID = $nameArray[count($nameArray) - 3];
            $vendor_id = get_vendor_from_product( $productID );

            //If the vendor doesn't exist - add to shipping price to total
            if ( ! array_key_exists($vendor_id, $vendorsIDshippingValue) ) {
                $vendorsIDshippingValue[$vendor_id][] = $radio;
            //If vendor does exist
            } elseif ( ! in_array($radio, $vendorsIDshippingValue[$vendor_id]) ) {
                $vendorsIDshippingValue[$vendor_id][] = $radio;
            } else {
                die();
            }
            $totalRadio = $totalRadio + (float)$radio;
            WC()->session->set('radio_chosen'.$productID.'_'.$nameArray[count($nameArray) - 2], $radio );
            $woocommerce->cart->add_fee( __('Option Fee', 'woocommerce'), $totalRadio );        
            echo json_encode( $totalRadio );
        }
        die();
    }
}

$totalRadio is always correct. But this line seems like doesn't want to apply it correct: $woocommerce->cart->add_fee( __('Option Fee', 'woocommerce'), $totalRadio );.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Toma Tomov
  • 1,476
  • 19
  • 55
  • You can't set/update a fee where you are trying to do it… Business Bloomer code is copied/inspired from [this linked answer](https://stackoverflow.com/a/49470265/3730754)… To set a fee to the cart, you need to use `woocommerce_cart_calculate_fees` hook [like in this answer](https://stackoverflow.com/a/49470265/3730754). – LoicTheAztec Sep 15 '19 at 14:39
  • I did it by setting the session variable for the fee to 0 and recalculate it on every radio check. – Toma Tomov Sep 15 '19 at 15:39
  • Your question code and explanations are incomplete and your code not testable … Note that *"Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error **and the shortest code necessary to reproduce it in the question itself**. Questions without **a clear problem statement** are not useful to other readers. See: [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example)"*. – LoicTheAztec Sep 15 '19 at 16:28

1 Answers1

0

Is there a remove_fee function? Perhaps you need to remove any previous fees if they change the shipping options?

Jason Miesionczek
  • 14,268
  • 17
  • 76
  • 108