/** * {@inheritdoc} */ public function setCurrent(AddressInterface $address) { foreach ($address->getCustomer()->getAddressesByType($address->getType()) as $custAddress) { if ($custAddress->getCurrent()) { $custAddress->setCurrent(false); $this->save($custAddress); break; } } $address->setCurrent(true); $this->save($address); }
/** * {@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; }
/** * {@inheritdoc} */ public function setBillingAddress(AddressInterface $address = null) { $this->billingAddress = $address; $this->billingAddressId = $address ? $address->getId() : null; }
/** * 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); }
/** * Checks if $address is valid * * @param AddressInterface $address * * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException */ protected function checkAddress(AddressInterface $address = null) { if (null === $address || $address->getCustomer()->getId() !== $this->getCustomer()->getId()) { throw new NotFoundHttpException(); } }
/** * {@inheritdoc} */ public function addAddress(AddressInterface $address) { $address->setCustomer($this); if (count($this->getAddressesByType($address->getType())) === 0) { $address->setCurrent(true); } $this->getAddresses()->add($address); if (null === $this->getFirstname()) { $this->setFirstname($address->getFirstname()); } if (null === $this->getLastname()) { $this->setLastname($address->getLastname()); } if (null === $this->getPhoneNumber()) { $this->setPhoneNumber($address->getPhone()); } }