/** * Test resolve name. * * @dataProvider dataResolveName */ public function testResolveNameWithName(PurchasableNameResolverInterface $resolver) { $variant = $this->prophesize('Elcodi\\Component\\Product\\Entity\\Interfaces\\VariantInterface'); $variant->getName()->willReturn('Variant name'); $variant = $variant->reveal(); $this->assertEquals('Variant name', $resolver->resolveName($variant)); $this->assertEquals('Variant name', $resolver->resolveName($variant, ' # ')); }
/** * Buildform function * * @param FormBuilderInterface $builder the formBuilder * @param array $options the options for this form */ public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('name', 'text', ['required' => true, 'constraints' => [new Constraints\Length(['max' => 65])]])->add('slug', 'text', ['required' => true, 'constraints' => [new Constraints\Length(['max' => 65])]])->add('description', 'textarea', ['required' => true])->add('showInHome', 'checkbox', ['required' => false])->add('stock', 'hidden', ['required' => true])->add('sku', 'text', ['required' => false])->add('price', 'money_object', ['required' => true, 'constraints' => [new MinimumMoney(['value' => 0])]])->add('reducedPrice', 'money_object', ['required' => false, 'constraints' => [new MinimumMoney(['value' => 0])]])->add('imagesSort', 'text', ['required' => false])->add('enabled', 'checkbox', ['required' => false])->add('height', 'number', ['required' => false])->add('width', 'number', ['required' => false])->add('depth', 'number', ['required' => false])->add('weight', 'number', ['required' => false])->add('metaTitle', 'text', ['required' => false])->add('metaDescription', 'text', ['required' => false, 'constraints' => [new Constraints\Length(['max' => 159])]])->add('metaKeywords', 'text', ['required' => false])->add('stockType', 'choice', ['choices' => ['admin.purchasable_pack.field.stock_type.specific_stock' => ElcodiProductStock::SPECIFIC_STOCK, 'admin.purchasable_pack.field.stock_type.inherit_stock' => ElcodiProductStock::INHERIT_STOCK], 'choices_as_values' => true, 'required' => true])->add('stock', 'number', ['required' => false])->add('manufacturer', 'entity', ['class' => $this->manufacturerNamespace, 'required' => false, 'multiple' => false])->add('principalCategory', 'entity', ['class' => $this->categoryNamespace, 'required' => true, 'multiple' => false])->add('purchasables', 'entity', ['class' => $this->purchasableNamespace, 'required' => false, 'choice_label' => function (PurchasableInterface $purchasable) { return $this->purchasableNameResolver->resolveName($purchasable); }, 'multiple' => true])->add('images', 'entity', ['class' => $this->imageNamespace, 'required' => false, 'property' => 'id', 'multiple' => true, 'expanded' => true]); $builder->addEventSubscriber($this->getEntityTranslatorFormEventListener()); }
/** * Test resolve name. * * @dataProvider dataResolveName */ public function testResolveName(PurchasableNameResolverInterface $resolver) { $product = $this->prophesize('Elcodi\\Component\\Product\\Entity\\Interfaces\\ProductInterface'); $variant = $this->prophesize('Elcodi\\Component\\Product\\Entity\\Interfaces\\VariantInterface'); $product->getName()->willReturn('Product Name'); $variant->getProduct()->willReturn($product->reveal()); $attribute1 = $this->prophesize('Elcodi\\Component\\Attribute\\Entity\\Interfaces\\AttributeInterface'); $attribute1->getName()->willReturn('attribute1'); $value1 = $this->prophesize('Elcodi\\Component\\Attribute\\Entity\\Interfaces\\ValueInterface'); $value1->getAttribute()->willReturn($attribute1->reveal()); $value1->getValue()->willReturn('value1'); $attribute2 = $this->prophesize('Elcodi\\Component\\Attribute\\Entity\\Interfaces\\AttributeInterface'); $attribute2->getName()->willReturn('attribute2'); $value2 = $this->prophesize('Elcodi\\Component\\Attribute\\Entity\\Interfaces\\ValueInterface'); $value2->getAttribute()->willReturn($attribute2->reveal()); $value2->getValue()->willReturn('value2'); $variant->getOptions()->willReturn(new ArrayCollection([$value1->reveal(), $value2->reveal()])); $variant = $variant->reveal(); $this->assertEquals('Product Name - attribute1 value1 - attribute2 value2', $resolver->resolveName($variant)); $this->assertEquals('Product Name # attribute1 value1 # attribute2 value2', $resolver->resolveName($variant, ' # ')); }
/** * Get extra data * * Returns the order lines as array in the following form * * [ * 1 => [ 'item' => 'Item 1', 'amount' => 1234, 'currency_code' => 'EUR ], * 2 => [ 'item_name' => 'Item 2', 'item_amount' => 2345, 'item_currency_code' => 'EUR ], * ] * * @return array */ public function getExtraData() { $extraData = []; $orderDescription = []; if ($this->order instanceof Order) { $currency = $this->order->getAmount()->getCurrency(); /** * @var OrderLine $orderLine * * Line prices must be converted to the currency * of the Cart when they are defined in another * currency */ foreach ($this->order->getOrderLines() as $orderLine) { $orderLineArray = []; $purchasable = $orderLine->getPurchasable(); $orderLineName = $this->purchasableNameResolver->resolveName($purchasable); $orderLineArray['item_name'] = $orderLineName; $lineAmount = $orderLine->getPurchasableAmount(); /* * We need to convert any price to match * current order currency */ $convertedAmount = $this->currencyConverter->convertMoney($lineAmount, $currency); $orderLineArray['amount'] = $convertedAmount->getAmount(); /** * Line items currency should always match * the one from the order */ $orderLineArray['item_currency_code'] = $this->getCurrency(); $orderDescription[] = $orderLineName; $orderLineArray['quantity'] = $orderLine->getQuantity(); $extraData['items'][$orderLine->getId()] = $orderLineArray; } // We add the shipping costs as a new "shadow" line in the extraData structure. $shippingAmount = $this->order->getShippingAmount(); if ($shippingAmount->isGreaterThan(Money::create(0, $shippingAmount->getCurrency()))) { $extraData['items'][] = ['item_name' => 'shipping', 'item_currency_code' => $shippingAmount->getCurrency(), 'quantity' => 1, 'amount' => $shippingAmount->getAmount()]; } // We add the coupon discounts as a new "shadow" line in the extraData structure. $couponAmount = $this->order->getCouponAmount(); if ($couponAmount->isGreaterThan(Money::create(0, $couponAmount->getCurrency()))) { $extraData['discount_amount_cart'] = $couponAmount->getAmount(); } $extraData['order_description'] = implode(" - ", $orderDescription); } return $extraData; }
/** * Test resolve name. * * @dataProvider dataResolveName */ public function testResolveName(PurchasableNameResolverInterface $resolver) { $product = $this->prophesize('Elcodi\\Component\\Product\\Entity\\Interfaces\\ProductInterface'); $product->getName()->willReturn('My Product'); $this->assertEquals('My Product', $resolver->resolveName($product->reveal())); }