return BasketElements.
public getBasketElements ( ) : Sonata\Component\Basket\BasketElementInterface[] | ||
Résultat | Sonata\Component\Basket\BasketElementInterface[] |
/** * Returns basket elements delivery countries * * @return array */ protected function getBasketDeliveryCountries() { $countries = array(); foreach ($this->basket->getBasketElements() as $basketElement) { $product = $basketElement->getProduct(); foreach ($product->getDeliveries() as $delivery) { $code = $delivery->getCountryCode(); if (!isset($countries[$code])) { $countries[$code] = Intl::getRegionBundle()->getCountryName($code); } } } return $countries; }
/** * Build a basket * * @param \Sonata\Component\Basket\BasketInterface $basket * * @throws \RuntimeException */ public function build(BasketInterface $basket) { $basket->setProductPool($this->productPool); foreach ($basket->getBasketElements() as $basketElement) { if ($basketElement->getProduct() === null) { // restore information if ($basketElement->getProductCode() == null) { throw new \RuntimeException('The product code is empty'); } $productDefinition = $this->productPool->getProduct($basketElement->getProductCode()); $basketElement->setProductDefinition($productDefinition); } } // load the delivery address $deliveryAddressId = $basket->getDeliveryAddressId(); if ($deliveryAddressId) { $address = $this->addressManager->findOneBy(array('id' => $deliveryAddressId)); $basket->setDeliveryAddress($address); } $deliveryMethodCode = $basket->getDeliveryMethodCode(); if ($deliveryMethodCode) { $basket->setDeliveryMethod($this->deliveryPool->getMethod($deliveryMethodCode)); } // load the payment address $billingAddressId = $basket->getBillingAddressId(); if ($billingAddressId) { $address = $this->addressManager->findOneBy(array('id' => $billingAddressId)); $basket->setBillingAddress($address); } // load the payment method $paymentMethodCode = $basket->getPaymentMethodCode(); if ($paymentMethodCode) { $basket->setPaymentMethod($this->paymentPool->getMethod($paymentMethodCode)); } }
/** * The validator asks each product repository to validate the related basket element. * * @param BasketInterface $basket * @param Constraint $constraint */ public function validate($basket, Constraint $constraint) { foreach ($basket->getBasketElements() as $pos => $basketElement) { // create a new ErrorElement object $errorElement = new ErrorElement($basket, $this->constraintValidatorFactory, $this->context, $this->context->getGroup()); $errorElement->with('basketElements[' . $pos . ']'); // validate the basket element through the related service provider $this->productPool->getProvider($basketElement->getProductCode())->validateFormBasketElement($errorElement, $basketElement, $basket); } if (count($this->context->getViolations()) > 0) { $this->context->addViolationAt('basketElements', $constraint->message); } }
/** * {@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; }
/** * transform a basket into order. * * @throws \RuntimeException * * @param null|\Sonata\Component\Basket\BasketInterface $basket * * @return null|\Sonata\Component\Order\OrderInterface */ public function transformIntoOrder(BasketInterface $basket) { $event = new BasketTransformEvent($basket); $this->eventDispatcher->dispatch(TransformerEvents::PRE_BASKET_TO_ORDER_TRANSFORM, $event); // Customer $customer = $basket->getCustomer(); if (!$customer instanceof CustomerInterface) { if ($this->getLogger()) { $this->getLogger()->emergency('[Sonata\\Component\\Payment\\Transform\\Basket::transform] the customer is not valid'); } throw new \RuntimeException('Invalid customer'); } // Billing $billingAddress = $basket->getBillingAddress(); if (!$billingAddress instanceof AddressInterface) { if ($this->getLogger()) { $this->getLogger()->emergency('[Sonata\\Component\\Payment\\Transform\\Basket::transform] the billing address is not valid'); } throw new \RuntimeException('Invalid billing address'); } $paymentMethod = $basket->getPaymentMethod(); if (!$paymentMethod instanceof PaymentInterface) { if ($this->getLogger()) { $this->getLogger()->emergency('[Sonata\\Component\\Payment\\PaymentInterface::transform] the payment method is not valid'); } throw new \RuntimeException('Invalid payment method'); } // Shipping $deliveryMethod = $basket->getDeliveryMethod(); if (!$deliveryMethod instanceof ServiceDeliveryInterface) { if ($this->getLogger()) { $this->getLogger()->emergency('[Sonata\\Component\\Delivery\\ServiceDeliveryInterface::transform] the delivery method is not valid'); } throw new \RuntimeException('Invalid delivery method'); } $deliveryAddress = $basket->getDeliveryAddress(); if ($deliveryMethod->isAddressRequired() && !$deliveryAddress instanceof AddressInterface) { if ($this->getLogger()) { $this->getLogger()->emergency('[Sonata\\Component\\Delivery\\ServiceDeliveryInterface::transform] the shipping address is not valid'); } throw new \RuntimeException('Invalid delivery address'); } // add a custom class_instance for testing purpose. $order = $this->orderManager->create(); $order->setCustomer($customer); $order->setUsername($customer->getFullname()); $order->setLocale($customer->getLocale()); if ($deliveryMethod->isAddressRequired()) { $order->setShippingAddress1($deliveryAddress->getAddress1()); $order->setShippingAddress2($deliveryAddress->getAddress2()); $order->setShippingAddress3($deliveryAddress->getAddress3()); $order->setShippingPostcode($deliveryAddress->getPostcode()); $order->setShippingCity($deliveryAddress->getCity()); $order->setShippingCountryCode($deliveryAddress->getCountryCode()); $order->setShippingName($deliveryAddress->getFirstname() . ' ' . $deliveryAddress->getLastname()); $order->setShippingPhone($deliveryAddress->getPhone()); } $order->setBillingAddress1($billingAddress->getAddress1()); $order->setBillingAddress2($billingAddress->getAddress2()); $order->setBillingAddress3($billingAddress->getAddress3()); $order->setBillingPostcode($billingAddress->getPostcode()); $order->setBillingCity($billingAddress->getCity()); $order->setBillingCountryCode($billingAddress->getCountryCode()); $order->setBillingName($billingAddress->getFirstname() . ' ' . $billingAddress->getLastname()); $order->setBillingPhone($billingAddress->getPhone()); $order->setTotalExcl($basket->getTotal()); $order->setTotalInc($basket->getTotal(true)); $order->setDeliveryVat($basket->getDeliveryVat()); $order->setDeliveryCost($basket->getDeliveryPrice(true)); $order->setDeliveryMethod($basket->getDeliveryMethod()->getCode()); $order->setDeliveryStatus(ServiceDeliveryInterface::STATUS_OPEN); $order->setCreatedAt(new \DateTime()); $order->setCurrency($basket->getCurrency()); $order->setStatus(OrderInterface::STATUS_OPEN); $order->setPaymentStatus(TransactionInterface::STATUS_OPEN); $order->setPaymentMethod($basket->getPaymentMethod()->getCode()); foreach ($basket->getBasketElements() as $basketElement) { $orderElement = $basketElement->getProductProvider()->createOrderElement($basketElement); if (!$orderElement instanceof OrderElementInterface) { continue; } $order->addOrderElement($orderElement); } $event = new OrderTransformEvent($order); $this->eventDispatcher->dispatch(TransformerEvents::POST_BASKET_TO_ORDER_TRANSFORM, $event); return $order; }
/** * @param FormEvent $event */ public function preBind(FormEvent $event) { $form = $event->getForm(); $data = $event->getData(); $this->buildBasketElements($form, $this->basket->getBasketElements()); }
/** * {@inheritdoc} */ public function isBasketValid(BasketInterface $basket) { if ($basket->countBasketElements() == 0) { return false; } foreach ($basket->getBasketElements() as $element) { $product = $element->getProduct(); if ($product->isRecurrentPayment() === true) { return false; } } return true; }