/** * Set up. */ public function setUp() { parent::setUp(); $this->cart = $this->get('elcodi.factory.cart')->create(); $this->purchasable = $this->createPurchasable(); $this->cartLine = $this->get('elcodi.factory.cart_line')->create()->setPurchasable($this->purchasable)->setPurchasableAmount($this->purchasable->getPrice())->setAmount($this->purchasable->getPrice())->setQuantity(1); $this->get('elcodi.event_dispatcher.cart')->dispatchCartLoadEvents($this->cart); }
/** * Steps necessary to store an image * * @param PurchasableInterface $purchasable Purchasable * @param string $imageName Image name * * @return $this Self object */ protected function storePurchasableImage(PurchasableInterface $purchasable, $imageName) { $imagePath = realpath(__DIR__ . '/../images/' . $imageName); $image = $this->storeImage($imagePath); $purchasable->addImage($image); $purchasable->setPrincipalImage($image); return $this; }
/** * Make a simple validation of a Purchasable instance. * * @param PurchasableInterface $purchasable Purchasable * @param int $stockRequired Stock required * @param bool $useStock Use stock * * @return bool|int Is valid or the number of elements that can be used */ public function isValidUsingSimplePurchasableValidation(PurchasableInterface $purchasable, $stockRequired, $useStock) { if (!$purchasable->isEnabled() || $stockRequired <= 0 || $useStock && $purchasable->getStock() <= 0) { return false; } if ($purchasable->getStock() < $stockRequired) { return $purchasable->getStock(); } return true; }
/** * Given a purchasable, resolve the name. * * @param PurchasableInterface $purchasable Purchasable * @param string $separator Separator * * @return false|string Name resolved or false if invalid object */ public function resolveName(PurchasableInterface $purchasable, $separator = null) { $namespace = $this->getPurchasableNamespace(); if (!$purchasable instanceof $namespace) { return false; } /** * @var $purchasable ProductInterface */ return $purchasable->getName(); }
/** * Update stock. * * @param PurchasableInterface $purchasable Purchasable * @param int $stockToDecrease Stock to decrease * * @return false|int Real decreased stock or false if error */ public function updateSimplePurchasableStock(PurchasableInterface $purchasable, $stockToDecrease) { $stock = $purchasable->getStock(); if ($stock === ElcodiProductStock::INFINITE_STOCK || $stockToDecrease <= 0 || $stock <= 0) { return false; } $realStockToDecrease = min($stock, $stockToDecrease); $resultingStock = $stock - $realStockToDecrease; $purchasable->setStock($resultingStock); return $realStockToDecrease; }
/** * Returns a human readable name for a purchasable, whether Product or * Variant is, with all needed information. This value is unique per each * type of purchasable element. * * @param PurchasableInterface $purchasable Purchasable to get name from * @param string $separator Separator string for product variant options * * @return string Purchasable name */ public function getPurchasableName(PurchasableInterface $purchasable, $separator = self::DEFAULT_SEPARATOR) { if ($purchasable instanceof ProductInterface) { return $purchasable->getName(); } /** * @var VariantInterface $purchasable */ $productName = $purchasable->getProduct()->getName(); foreach ($purchasable->getOptions() as $option) { /** * @var ValueInterface $option */ $productName .= $separator . $option->getAttribute()->getName() . ' ' . $option->getValue(); } return $productName; }
/** * Set the purchasable object. This method is responsible for choosing how * this object must be stored depending on the type. * * @param PurchasableInterface $purchasable Purchasable object * * @return $this Self object */ public function setPurchasable(PurchasableInterface $purchasable) { if ($purchasable instanceof ProductInterface) { $this->product = $purchasable; return $this; } if ($purchasable instanceof VariantInterface) { $this->variant = $purchasable; $product = $purchasable->getProduct(); $this->product = $product; return $this; } if ($purchasable instanceof PackInterface) { $this->pack = $purchasable; } return $this; }
/** * Given a purchasable, resolve the name. * * @param PurchasableInterface $purchasable Purchasable * @param string $separator Separator * * @return string Name resolved */ public function resolveName(PurchasableInterface $purchasable, $separator = self::DEFAULT_SEPARATOR) { $namespace = $this->getPurchasableNamespace(); if (!$purchasable instanceof $namespace) { return false; } /** * @var $purchasable VariantInterface */ $variantName = $purchasable->getProduct()->getName(); foreach ($purchasable->getOptions() as $option) { /** * @var ValueInterface $option */ $variantName .= $separator . $option->getAttribute()->getName() . ' ' . $option->getValue(); } return $variantName; }
/** * Update stock. * * @param PurchasableInterface $purchasable Purchasable * @param int $stockToDecrease Stock to decrease * * @return false|int Real decreased stock or false if error */ public function updateStock(PurchasableInterface $purchasable, $stockToDecrease) { $namespace = $this->getPurchasableNamespace(); if (!$purchasable instanceof $namespace) { return false; } /** * @var $purchasable PackInterface */ $stockType = $purchasable->getStockType(); if ($stockType === ElcodiProductStock::INHERIT_STOCK) { return $this->updateStockOfPurchasablesByLoadedStockUpdaters($purchasable->getPurchasables(), $stockToDecrease); } $decreasedStock = $this->updateSimplePurchasableStock($purchasable, $stockToDecrease); if ($decreasedStock) { $this->packObjectManager->flush($purchasable); } return $decreasedStock; }
/** * Returns a homan readable name for a purchasable, whether Product or Variant * * @param PurchasableInterface $purchasable Purchasable to get name from * @param string $separator Separator string for product variant options * * @return string */ public function getPurchasableName(PurchasableInterface $purchasable, $separator = ' - ') { if ($purchasable instanceof ProductInterface) { /** * @var ProductInterface $purchasable */ $productName = $purchasable->getName(); } else { /** * @var VariantInterface $purchasable */ $productName = $purchasable->getProduct()->getName(); foreach ($purchasable->getOptions() as $option) { /** * @var ValueInterface $option */ $productName .= $separator . $option->getAttribute()->getName() . ' ' . $option->getName(); } } return $productName; }
/** * Gets purchasable validation. * * @param PurchasableInterface $purchasable Purchasable * @param int $stockRequired Stock required * @param bool $useStock Use stock * * @return bool|int Is valid or the number of elements that can be used */ public function isStockAvailable(PurchasableInterface $purchasable, $stockRequired, $useStock) { $namespace = $this->getPurchasableNamespace(); if (!$purchasable instanceof $namespace) { return false; } /** * @var PackInterface $purchasable */ $isInheritStock = ElcodiProductStock::INHERIT_STOCK === $purchasable->getStockType(); $isInheritanceValid = $this->areValidByLoadedValidators($purchasable->getPurchasables(), $stockRequired, $isInheritStock); /** * Happens when their related elements do not allow this pack to be used. */ if (false === $isInheritanceValid) { return false; } $isPackValid = $this->isValidUsingSimplePurchasableValidation($purchasable, $stockRequired, !$isInheritStock); /** * Happens when the pack itself is not valid. */ if (false === $isPackValid) { return false; } /** * At this point we need to check both values for determining the result * value of the whole validation. In this case, both results allow the * pack to be used. */ if (true === $isInheritanceValid && true === $isPackValid) { return true; } /** * Some of them have returned an int value. There is a problem with * stock, so we need to check which one is valid. */ return $isInheritStock ? $isInheritanceValid : $isPackValid; }
/** * Given a purchasable, get it's main product. This purchasable must be an * implementation of ProductInterface or VariantInterface. Otherwise the * method will return false * * @param PurchasableInterface $purchasable Purchasable * * @return ProductInterface|false Product instance or false */ private function getProductByPurchasable(PurchasableInterface $purchasable) { if (!$purchasable instanceof ProductInterface && !$purchasable instanceof VariantInterface) { return false; } return $purchasable instanceof ProductInterface ? $purchasable : $purchasable->getProduct(); }
/** * Resolve view given the purchasable instance * * @param PurchasableInterface $purchasable Purchasable * * @return string template name */ private function resolveTemplateName(PurchasableInterface $purchasable) { if ($purchasable instanceof ProductInterface) { return $purchasable->hasVariants() ? 'Pages:product-view-variant.html.twig' : 'Pages:product-view-item.html.twig'; } if ($purchasable instanceof PackInterface) { return 'Pages:purchasable-pack-view.html.twig'; } return ''; }
/** * Add a Purchasable to Cart as a new CartLine * * This method creates a new CartLine and set item quantity * correspondingly. * * If the Purchasable is already in the Cart, it just increments * item quantity by $quantity * * @param CartInterface $cart Cart * @param PurchasableInterface $purchasable Product or Variant to add * @param integer $quantity Number of units to set or increase * * @return $this self Object */ public function addProduct(CartInterface $cart, PurchasableInterface $purchasable, $quantity) { /** * If quantity is not a number or is 0 or less, product is not added * into cart */ if (!is_int($quantity) || $quantity <= 0) { return $this; } foreach ($cart->getCartLines() as $cartLine) { /** * @var CartLineInterface $cartLine */ if ($cartLine->getPurchasable()->getId() == $purchasable->getId()) { /** * Product already in the Cart, increase quantity */ return $this->increaseCartLineQuantity($cartLine, $quantity); } } $cartLine = $this->cartLineFactory->create(); $cartLine->setPurchasable($purchasable)->setQuantity($quantity); $this->addLine($cart, $cartLine); return $this; }