Добрый день!
$location = fn_get_customer_location($auth, $cart);
$product_groups = Shippings::groupProductsList($cart_products, $location);
// Check whether shipping must be calculated
$is_shipping_calculation_forced = $calculate_shipping === 'A';
$is_shipping_calculation_required_for_selected = $calculate_shipping === 'E';
$is_shipping_selected = !empty($cart['chosen_shipping'])
&& count($cart['chosen_shipping']) === count($product_groups);
$cart['calculate_shipping'] = $cart['shipping_required']
&& (
$is_shipping_calculation_forced
|| (
$is_shipping_calculation_required_for_selected
&& $is_shipping_selected
)
);
if ($cart['calculate_shipping'] || empty($cart['product_groups'])) {
$shippings = [];
$shippings_groups = [];
foreach ($product_groups as $key_group => $group) {
if ($cart['shipping_required'] === false) {
$product_groups[$key_group]['free_shipping'] = true;
$product_groups[$key_group]['shipping_no_required'] = true;
}
$product_groups[$key_group]['shippings'] = [];
$shippings_group = Shippings::getShippingsList($group, $lang_code, $area, ['get_images' => true]);
foreach ($shippings_group as $shipping_id => $shipping) {
$shippings_groups[$shipping_id] = $shipping;
$product_group = $group;
if (!empty($shipping['service_params']['max_weight_of_box'])) {
$product_group = Shippings::repackProductsByWeight($group, $shipping['service_params']['max_weight_of_box']);
}
$shippings[] = array_merge($shipping, [
'package_info' => $product_group['package_info'],
'package_info_full' => $product_group['package_info_full'],
'keys' => [
'group_key' => $key_group,
'shipping_id' => $shipping_id,
'company_id' => $product_group['company_id']
],
]);
$shipping['group_key'] = $key_group;
$shipping['rate'] = 0;
// shipping is free when obtained via promotions, or group has free shipping and shipping method is suitable for free shipping
$shipping['free_shipping'] = in_array($shipping_id, $cart['free_shipping']) ||
$group['free_shipping'] && Shippings::isFreeShipping($shipping);
$product_groups[$key_group]['shippings'][$shipping_id] = $shipping;
// Adding a shipping method from the created order, if the shipping is not yet in the list.
if (!empty($cart['chosen_shipping']) && !empty($cart['shipping']) && !empty($cart['order_id'])) {
foreach ($cart['shipping'] as $cart_shipping) {
if (!isset($shippings_group[$cart_shipping['shipping_id']])) {
$shippings_group[$cart_shipping['shipping_id']] = $cart_shipping;
}
}
}
}
}
if ($cart['calculate_shipping']) {
$cart['product_groups'] = $product_groups;
$rates = Shippings::calculateRates($shippings);
Registry::set($shipping_cache_key, $rates);
foreach ($rates as $rate) {
$group_key = $rate['keys']['group_key'];
$shipping_id = $rate['keys']['shipping_id'];
$company_id = $rate['keys']['company_id'];
if (!Shippings::isFreeShipping($shippings_groups[$shipping_id])) {
$package_info = $product_groups[$group_key]['package_info_full'];
} else {
$package_info = $product_groups[$group_key]['package_info'];
}
if (
$company_id
&& Shippings::isSentByMarketplace($shippings_groups[$shipping_id])
) {
$rate['price'] = 0;
}
if (!empty($product_groups[$group_key]['shippings'][$shipping_id]['rate_info'])) {
if ($rate['price'] !== false) {
$rate['price'] += !empty($package_info['shipping_freight'])
? $package_info['shipping_freight']
: 0;
$product_groups[$group_key]['shippings'][$shipping_id]['rate'] = empty($product_groups[$group_key]['shippings'][$shipping_id]['free_shipping'])
? $rate['price']
: 0;
} else {
unset($product_groups[$group_key]['shippings'][$shipping_id]);
}
if (!empty($rate['service_delivery_time'])) {
$product_groups[$group_key]['shippings'][$shipping_id]['service_delivery_time'] = $rate['service_delivery_time'];
}
} else {
unset($product_groups[$group_key]['shippings'][$shipping_id]);
}
}
}
$cart['product_groups'] = $product_groups;
}
$product_groups = &$cart['product_groups'];
Хотелось бы узнать зачем $product_groups менять с корзины если наверху генерируется новый и при чем с корзины мы достаем не правильный product_groups?