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 );
.