Ejemplo n.º 1
0
 /**
  * Returns an array of unique available options for a Product.
  *
  * Returned Options belong to Variants available for purchase
  *
  * @param ProductInterface   $product   Product
  * @param AttributeInterface $attribute Attribute
  *
  * @return ArrayCollection
  */
 public function getAvailableOptions(ProductInterface $product, AttributeInterface $attribute)
 {
     $availableOptions = new ArrayCollection();
     foreach ($product->getVariants() as $variant) {
         /**
          * @var VariantInterface $variant
          */
         if (!$variant->isEnabled() || $variant->getStock() <= 0) {
             continue;
         }
         foreach ($variant->getOptions() as $option) {
             /**
              * @var ValueInterface $option
              */
             if ($option->getAttribute() == $attribute && !$availableOptions->contains($option)) {
                 $availableOptions->add($option);
             }
         }
     }
     return $availableOptions;
 }
Ejemplo n.º 2
0
 /**
  * Given a Product and an array of integer representing the IDs of a Value Entity,
  * returns the Variant that is associated with the options matching the IDs, if any
  *
  * @param ProductInterface $product            to compare Variants from
  * @param array            $optionsSearchedIds array containing IDs of the options to match
  *
  * @return VariantInterface|null Variant if found, or null if not
  */
 public function findByOptionIds(ProductInterface $product, array $optionsSearchedIds = [])
 {
     sort($optionsSearchedIds);
     foreach ($product->getVariants() as $variant) {
         /**
          * @var VariantInterface $variant
          */
         $optionsConfiguredIds = array_map(function (ValueInterface $option) {
             return $option->getId();
         }, $variant->getOptions()->toArray());
         sort($optionsConfiguredIds);
         if ($optionsSearchedIds == $optionsConfiguredIds) {
             /**
              * Options match, we found the product Variant
              */
             return $variant;
         }
     }
     /**
      * No match, probable option misconfiguration in Variants
      */
     return null;
 }