Example #1
0
 /**
  * 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));
     }
 }
Example #2
0
 public function testAvailableMethods()
 {
     $deliveryMethod_low = new ServiceDelivery();
     $deliveryMethod_low->setCode('ups_low');
     $deliveryMethod_low->setEnabled(true);
     $deliveryMethod_low->setPriority(1);
     $deliveryMethod_high = new ServiceDelivery();
     $deliveryMethod_high->setCode('ups_high');
     $deliveryMethod_high->setEnabled(true);
     $deliveryMethod_high->setPriority(2);
     $deliveryMethod_high_bis = new ServiceDelivery();
     $deliveryMethod_high_bis->setCode('ups_high_bis');
     $deliveryMethod_high_bis->setEnabled(true);
     $deliveryMethod_high_bis->setPriority(2);
     $deliveryPool = new DeliveryPool();
     $deliveryPool->addMethod($deliveryMethod_low);
     $deliveryPool->addMethod($deliveryMethod_high);
     $deliveryPool->addMethod($deliveryMethod_high_bis);
     $productPool = new ProductPool();
     $productDelivery_low = $this->getMock('Sonata\\Component\\Product\\DeliveryInterface');
     $productDelivery_low->expects($this->any())->method('getCode')->will($this->returnValue('ups_low'));
     $productDelivery_high = $this->getMock('Sonata\\Component\\Product\\DeliveryInterface');
     $productDelivery_high->expects($this->any())->method('getCode')->will($this->returnValue('ups_high'));
     $productDelivery_high_bis = $this->getMock('Sonata\\Component\\Product\\DeliveryInterface');
     $productDelivery_high_bis->expects($this->any())->method('getCode')->will($this->returnValue('ups_high_bis'));
     $product = $this->getMock('Sonata\\Component\\Product\\ProductInterface');
     $product->expects($this->once())->method('getDeliveries')->will($this->returnValue(array($productDelivery_low, $productDelivery_high, $productDelivery_high_bis)));
     $basketElement = $this->getMock('Sonata\\Component\\Basket\\BasketElementInterface');
     $basketElement->expects($this->once())->method('getProduct')->will($this->returnValue($product));
     $basket = $this->getMock('Sonata\\Component\\Basket\\BasketInterface');
     $basket->expects($this->once())->method('getBasketElements')->will($this->returnValue(array($basketElement)));
     $address = $this->getMock('Sonata\\Component\\Customer\\AddressInterface');
     $selector = new Selector($deliveryPool, $productPool);
     $selector->setLogger($this->getMock('Symfony\\Component\\HttpKernel\\Log\\LoggerInterface'));
     $instances = $selector->getAvailableMethods($basket, $address);
     $this->assertCount(3, $instances);
     $this->assertEquals($instances[0]->getCode(), 'ups_high_bis');
     $this->assertEquals($instances[1]->getCode(), 'ups_high');
     $this->assertEquals($instances[2]->getCode(), 'ups_low');
 }
 /**
  * Adds the delivery information from $order to $invoice
  *
  * @param InvoiceInterface $invoice
  * @param OrderInterface   $order
  */
 protected function addDelivery(InvoiceInterface $invoice, OrderInterface $order)
 {
     /** @var InvoiceElementInterface $invoiceElement */
     $invoiceElement = $this->invoiceElementManager->create();
     $invoiceElement->setQuantity(1);
     $invoiceElement->setPrice($order->getDeliveryCost());
     $invoiceElement->setUnitPrice($order->getDeliveryCost());
     $invoiceElement->setTotal($order->getDeliveryCost());
     $invoiceElement->setVatRate(0);
     $invoiceElement->setDesignation($this->deliveryPool->getMethod($order->getDeliveryMethod())->getName());
     $invoiceElement->setDescription($this->deliveryPool->getMethod($order->getDeliveryMethod())->getName());
     $invoiceElement->setInvoice($invoice);
     $invoice->addInvoiceElement($invoiceElement);
 }
Example #4
0
 public function testPool()
 {
     $pool = new Pool();
     $delivery = new FreeDelivery(true);
     $delivery->setCode('free_1');
     $pool->addMethod($delivery);
     $delivery = new FreeDelivery(true);
     $delivery->setCode('free_2');
     $pool->addMethod($delivery);
     $delivery = new FreeDelivery(true);
     $delivery->setCode('free_2');
     // same code
     $pool->addMethod($delivery);
     $this->assertEquals(2, count($pool->getMethods()), 'Pool return 2 elements');
     $this->assertInstanceOf('Sonata\\Component\\Delivery\\FreeDelivery', $pool->getMethod('free_2'), 'Pool return an FreeDelivery Instance');
 }
 /**
  * {@inheritdoc}
  */
 public function reverseTransform($value)
 {
     return $this->deliveryPool->getMethod($value);
 }