/** * 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; }
/** * 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; }
/** * 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(); }