public getProduct ( string $code ) : |
||
$code | string | |
return |
/** * 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)); } }
public function testPool() { $productProvider = $this->getMock('Sonata\\Component\\Product\\ProductProviderInterface'); $productManager1 = $this->getMock('Sonata\\Component\\Product\\ProductManagerInterface'); $productManager2 = $this->getMock('Sonata\\Component\\Product\\ProductManagerInterface'); // we need products from different objects to test ProductPool $product1 = $this->getMock('Sonata\\Component\\Product\\ProductInterface'); $product2 = new Product(); $productManager1->expects($this->any())->method('getClass')->will($this->returnValue($product1)); $productManager2->expects($this->any())->method('getClass')->will($this->returnValue($product2)); $definition1 = new ProductDefinition($productProvider, $productManager1); $definition2 = new ProductDefinition($productProvider, $productManager2); $productPool = new Pool(); $productPool->addProduct('product1', $definition1); $productPool->addProduct('product2', $definition2); $this->assertFalse($productPool->hasProvider('grou')); $this->assertTrue($productPool->hasProvider('product1')); $this->assertTrue($productPool->hasProvider('product2')); $this->assertEquals($productPool->getProduct('product1'), $definition1); $this->assertEquals($productPool->getProduct('product2'), $definition2); $this->assertEquals($productPool->getProductCode($product1), 'product1'); $this->assertEquals($productPool->getProductCode($product2), 'product2'); }