/** * "pc_shop/cart/calculate_prices" event handler. * * Validates delivery specific form information and calculates delivery price. * * @param array $params An associative array containing cart and order information that should be used for calculation. * - 'data' array: A reference to an associative array representing current cart state that should be filled with calculated data. * - 'order_data' array: An associative array containing information on current order. * - 'coupon_data' array: An associative array containing information on applied discount coupon. * - 'delivery_option_data' array: An associative array containing information on the currently selected delivery method. * - 'delivery_form_data' array: An associative array containing values of fields filled in the form specific to currently selected delivery method. */ function calculateCartPrice($params) { global $cfg, $core, $cache; $data =& $params['data']; if (isset($params['delivery_form_data']['city']) && $params['delivery_form_data']['city']) { /** @var PC_shop_site $shop */ $shop = $core->Get_object('PC_shop_site'); $limit = $this->getCityCODLimit($params['delivery_form_data']['city']); $data['delivery_info']['cod_limit'] = $limit; // print_pre($params['data']['items']); // print_pre($params['data']['products']); $maxX = 0; // package width $maxY = 0; // package length $totalZ = 0; // package height $totalSizeWeight = 0; $totalVolume = 0; $totalVolumeWeight = 0; foreach ($params['data']['items'] as $cartItem) { $product = $shop->products->applyAttributes($params['data']['products'][$cartItem['product_id']], $cartItem['attributes']); unset($product['attributes'], $product['attribute_index'], $product['combinations'], $product['price_combinations'], $product['resources'], $product['text'], $product['description']); if (!$product['weight']) { continue; } /* $measurements = array( 'weight' => $product['weight'], 'width' => $product['width'], 'height' => $product['height'], 'length' => $product['length'], 'volume' => $product['volume'], ); print_pre($measurements); */ if ($product['width'] > 0 && $product['height'] > 0 && $product['length'] > 0) { if ($product['width'] <= $product['height'] && $product['width'] <= $product['length']) { $x = $product['height']; $y = $product['length']; $z = $product['width']; } else { if ($product['height'] <= $product['width'] && $product['height'] <= $product['length']) { $x = $product['width']; $y = $product['length']; $z = $product['height']; } else { $x = $product['width']; $y = $product['height']; $z = $product['length']; } } $maxX = max($maxX, min($x, $y)); $maxY = max($maxY, max($x, $y)); $totalZ += $z * $cartItem['basket_quantity']; $totalSizeWeight += $product['weight'] * $cartItem['basket_quantity']; } else { if ($product['volume']) { $totalVolume += $product['volume'] * $cartItem['basket_quantity']; $totalVolumeWeight += $product['weight'] * $cartItem['basket_quantity']; } } } $senderCity = v($cfg['pc_shop_delivery_sdek']['sdek_sender_city'], 44); $destinationCity = $params['delivery_form_data']['city']; $date = date('Y-m-d'); $tariffId = v($cfg['pc_shop_delivery_sdek']['sdek_tariff_id'], null); $deliveryMode = v($cfg['pc_shop_delivery_sdek']['sdek_delivery_mode'], null); if (!$tariffId && !$deliveryMode) { $deliveryMode = 3; } // by default use warehouse-house $key = md5("{$senderCity}.{$destinationCity}.{$date}.{$tariffId}.{$deliveryMode}.{$totalSizeWeight}.{$maxX}.{$maxY}.{$totalZ}.{$totalVolumeWeight}.{$totalVolume}"); if (($calcData = $cache->get($key)) === null) { try { $calc = new CalculatePriceDeliveryCdek(); if ($cfg['pc_shop_delivery_sdek']['sdek_login']) { $calc->setAuth($cfg['pc_shop_delivery_sdek']['sdek_login'], $cfg['pc_shop_delivery_sdek']['sdek_password']); } $calc->setSenderCityId($senderCity); $calc->setReceiverCityId($destinationCity); $calc->setDateExecute($date); if ($tariffId) { $calc->setTariffId($tariffId); } if ($deliveryMode) { $calc->setModeDeliveryId($deliveryMode); } if ($totalSizeWeight > 0) { $calc->addGoodsItemBySize($totalSizeWeight, $maxY / 10, $maxX / 10, $totalZ / 10); } // divided by 10 because it must be in cm. if ($totalVolumeWeight > 0) { $calc->addGoodsItemByVolume($totalVolumeWeight, $totalVolume); } if ($calc->calculate()) { $calcData = $calc->getResult(); } else { $calcData = $calc->getError(); } $cacheDuration = 3600; } catch (\Exception $ex) { $calcData = array('error' => array(array('text' => strtr($core->Get_plugin_variable('error_internal', 'pc_shop_delivery_sdek'), array('{error}' => $ex->getMessage()))))); $cacheDuration = 600; } $cache->set($key, $calcData, $cacheDuration); } $data['delivery_info']['package'] = array('totalSizeWeight' => $totalSizeWeight, 'dimensions' => array($maxX, $maxY, $totalZ), 'totalVolumeWeight' => $totalVolumeWeight, 'volume' => $totalVolume); if (isset($calcData['result'])) { $result = $calcData['result']; $price = $result['price']; $cod = array_key_exists('cashOnDelivery', $result) ? $result['cashOnDelivery'] : null; $baseCur = $shop->price->get_base_currency(); if ($baseCur != $result['currency']) { $price = $shop->price->get_converted_price_in_base_currency($price, $result['currency'], true); if ($cod !== null) { $cod = $shop->price->get_converted_price_in_base_currency($cod, $result['currency'], true); } } $data['order_delivery_price'] = $shop->price->get_price_in_user_currency($price); if ($cod !== null) { $data['order_cod_price'] = $shop->price->get_price_in_user_currency($cod); } $data['delivery_info']['period_min'] = $result['deliveryPeriodMin']; $data['delivery_info']['period_max'] = $result['deliveryPeriodMax']; $data['delivery_info']['date_min'] = $result['deliveryDateMin']; $data['delivery_info']['date_max'] = $result['deliveryDateMax']; if (!$price && $result['price'] || !$cod && array_key_exists('cashOnDelivery', $result) && $result['cashOnDelivery']) { $data['errors'][] = $core->Get_variable('error_impossible_to_calculate', null, 'pc_shop_delivery_sdek'); } } else { if (isset($calcData['error']) && is_array($calcData['error'])) { foreach ($calcData['error'] as $error) { $data['errors'][] = $error['text']; } } } } else { $data['errors'][] = $core->Get_variable('error_city_required', null, 'pc_shop_delivery_sdek'); } }
//устанавливаем город-отправитель $calc->setSenderCityId($_REQUEST['senderCityId']); //устанавливаем город-получатель $calc->setReceiverCityId($_REQUEST['receiverCityId']); //устанавливаем дату планируемой отправки $calc->setDateExecute($_REQUEST['dateExecute']); //устанавливаем тариф по-умолчанию $calc->setTariffId('137'); //задаём список тарифов с приоритетами // $calc->addTariffPriority($_REQUEST['tariffList1']); // $calc->addTariffPriority($_REQUEST['tariffList2']); //устанавливаем режим доставки $calc->setModeDeliveryId($_REQUEST['modeId']); //добавляем места в отправление $calc->addGoodsItemBySize($_REQUEST['weight1'], $_REQUEST['length1'], $_REQUEST['width1'], $_REQUEST['height1']); $calc->addGoodsItemByVolume($_REQUEST['weight2'], $_REQUEST['volume2']); if ($calc->calculate() === true) { $res = $calc->getResult(); echo 'Цена доставки: ' . $res['result']['price'] . 'руб.<br />'; echo 'Срок доставки: ' . $res['result']['deliveryPeriodMin'] . '-' . $res['result']['deliveryPeriodMax'] . ' дн.<br />'; echo 'Планируемая дата доставки: c ' . $res['result']['deliveryDateMin'] . ' по ' . $res['result']['deliveryDateMax'] . '.<br />'; echo 'id тарифа, по которому произведён расчёт: ' . $res['result']['tariffId'] . '.<br />'; if (array_key_exists('cashOnDelivery', $res['result'])) { echo 'Ограничение оплаты наличными, от (руб): ' . $res['result']['cashOnDelivery'] . '.<br />'; } } else { $err = $calc->getError(); if (isset($err['error']) && !empty($err)) { //var_dump($err); foreach ($err['error'] as $e) { echo 'Код ошибки: ' . $e['code'] . '.<br />';