/** * {@inheritdoc} */ public function getAvailableMethods(BasketInterface $basket = null, AddressInterface $deliveryAddress = null) { $instances = array(); if (!$basket) { return $instances; } // STEP 1 : We get product's delivery methods /** @var $basketElement \Sonata\Component\Basket\BasketElementInterface */ foreach ($basket->getBasketElements() as $basketElement) { $product = $basketElement->getProduct(); if (!$product) { $this->log(sprintf('[sonata::getAvailableDeliveryMethods] product.id: %d does not exist', $basketElement->getProductId())); return $instances; } /** @var $productDelivery \Sonata\Component\Product\DeliveryInterface */ foreach ($product->getDeliveries() as $productDelivery) { // delivery method already selected if (isset($instances[$productDelivery->getCode()])) { $this->log(sprintf('[sonata::getAvailableDeliveryMethods] product.id: %d - code : %s already selected', $basketElement->getProductId(), $productDelivery->getCode())); continue; } $deliveryMethod = $this->getDeliveryPool()->getMethod($productDelivery->getCode()); if (!$deliveryMethod) { $this->log(sprintf('[sonata::getAvailableDeliveryMethods] product.id: %d - code: %s does not exist', $basketElement->getProductId(), $productDelivery->getCode())); continue; } // product delivery not enable if (!$deliveryMethod->getEnabled()) { $this->log(sprintf('[sonata::getAvailableDeliveryMethods] product.id: %d - code : %s is not enabled', $basketElement->getProductId(), $productDelivery->getCode())); continue; } // Address is required but none have been provided if ($deliveryMethod->isAddressRequired() && !$deliveryAddress) { $this->log(sprintf('[sonata::getAvailableDeliveryMethods] product.id: %d - code : %s requires an address but none have been provided', $basketElement->getProductId(), $productDelivery->getCode())); continue; } // the product is not deliverable at the $shippingAddress if ($deliveryMethod->isAddressRequired() && $deliveryAddress->getCountryCode() != $productDelivery->getCountryCode()) { $this->log(sprintf('[sonata::getAvailableDeliveryMethods] product.id: %d - code : %s the country code does not match (%s != %s)', $basketElement->getProductId(), $productDelivery->getCode(), $deliveryAddress->getCountryCode(), $productDelivery->getCountryCode())); continue; } $this->log(sprintf('[sonata::getAvailableDeliveryMethods] product.id: %d - code : %s selected', $basketElement->getProductId(), $productDelivery->getCode())); $instances[$deliveryMethod->getCode()] = $deliveryMethod; } } // STEP 2 : We select the delivery methods with the highest priority $instances = array_values($instances); usort($instances, array('Sonata\\Component\\Delivery\\Selector', 'sort')); return $instances; }
/** * Constructor * * @param AddressInterface $address * @param int $code * @param Exception $previous */ public function __construct(AddressInterface $address, $code = 0, Exception $previous = null) { $this->address = $address; $message = sprintf("Some elements in your basket cannot be delivered in country '%s'.", $address->getCountryCode()); parent::__construct($message, $code, $previous); }