/** * For a given {product, warehouse}, gets the carrier available * * @param JeproshopProductModelProduct $product The id of the product, or an array with at least the package size and weight * @param int $warehouse_id * @param int $address_delivery_id * @param int $shop_id * @param $cart * @return array */ public static function getAvailableCarrierList(JeproshopProductModelProduct $product, $warehouse_id, $address_delivery_id = null, $shop_id = null, $cart = null) { if (is_null($shop_id)) { $shop_id = JeproshopContext::getContext()->shop->shop_id; } if (is_null($cart)) { $cart = JeproshopContext::getContext()->cart; } $address_id = (int) (!is_null($address_delivery_id) && $address_delivery_id != 0 ? $address_delivery_id : $cart->address_delivery_id); if ($address_id) { $address = new JeproshopAddressModelAddress($address_id); $zone_id = JeproshopAddressModelAddress::getZoneIdByAddressId($address->address_id); // Check the country of the address is activated if (!JeproshopAddressModelAddress::isCountryActiveById($address->address_id)) { return array(); } } else { $country = new JeproshopCountryModelCountry(JeproshopSettingModelSetting::getValue('default_country')); $izone_id = $country->zone_id; } // Does the product is linked with carriers? $query = new DbQuery(); $query->select('id_carrier'); $query->from('product_carrier', 'pc'); $query->innerJoin('carrier', 'c', 'c.id_reference = pc.id_carrier_reference AND c.deleted = 0'); $query->where('pc.id_product = ' . (int) $product->product_id); $query->where('pc.id_shop = ' . (int) $shop_id); $cache_id = 'Carrier::getAvailableCarrierList_' . (int) $product->id . '-' . (int) $id_shop; if (!Cache::isStored($cache_id)) { $carriers_for_product = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query); Cache::store($cache_id, $carriers_for_product); } $carriers_for_product = Cache::retrieve($cache_id); $carrier_list = array(); if (!empty($carriers_for_product)) { //the product is linked with carriers foreach ($carriers_for_product as $carrier) { //check if the linked carriers are available in current zone if (Carrier::checkCarrierZone($carrier['id_carrier'], $id_zone)) { $carrier_list[] = $carrier['id_carrier']; } } if (empty($carrier_list)) { return array(); } //no linked carrier are available for this zone } // The product is not directly linked with a carrier // Get all the carriers linked to a warehouse if ($warehouse_id) { $warehouse = new JeproshopWarehouseModelWarehouse($warehouse_id); $warehouse_carrier_list = $warehouse->getCarriers(); } $available_carrier_list = array(); $customer = new JeproshopCustomerModelCustomer($cart->customer_id); $carriers = JeproshopCarrierModelCarrier::getCarriersForOrder($zone_id, $customer->getGroups(), $cart); foreach ($carriers as $carrier) { $available_carrier_list[] = $carrier->carrier_id; } if ($carrier_list) { $carrier_list = array_intersect($available_carrier_list, $carrier_list); } else { $carrier_list = $available_carrier_list; } if (isset($warehouse_carrier_list)) { $carrier_list = array_intersect($carrier_list, $warehouse_carrier_list); } if ($product->width > 0 || $product->height > 0 || $product->depth > 0 || $product->weight > 0) { foreach ($carrier_list as $key => $carrier_id) { $carrier = new JeproshopCarrierModelCarrier($carrier_id); if ($carrier->max_width > 0 && $carrier->max_width < $product->width || $carrier->max_height > 0 && $carrier->max_height < $product->height || $carrier->max_depth > 0 && $carrier->max_depth < $product->depth || $carrier->max_weight > 0 && $carrier->max_weight < $product->weight) { unset($carrier_list[$key]); } } } return $carrier_list; }