Example #1
0
 /**
  * @param Product $product
  * @param Store $store
  * @return \NostoProduct
  */
 public function build(Product $product, Store $store)
 {
     $nostoProduct = new \NostoProduct();
     try {
         $nostoProduct->setUrl($this->buildUrl($product, $store));
         $nostoProduct->setProductId($product->getId());
         $nostoProduct->setName($product->getName());
         $nostoProduct->setImageUrl($this->buildImageUrl($product, $store));
         $price = $this->_priceHelper->getProductFinalPriceInclTax($product);
         $nostoProduct->setPrice(new \NostoPrice($price));
         $listPrice = $this->_priceHelper->getProductPriceInclTax($product);
         $nostoProduct->setListPrice(new \NostoPrice($listPrice));
         $nostoProduct->setCurrency(new \NostoCurrencyCode($store->getBaseCurrencyCode()));
         $nostoProduct->setAvailability(new \NostoProductAvailability($product->isAvailable() ? \NostoProductAvailability::IN_STOCK : \NostoProductAvailability::OUT_OF_STOCK));
         $nostoProduct->setCategories($this->buildCategories($product));
         // Optional properties.
         $descriptions = array();
         if ($product->hasData('short_description')) {
             $descriptions[] = $product->getData('short_description');
         }
         if ($product->hasData('description')) {
             $descriptions[] = $product->getData('description');
         }
         if (count($descriptions) > 0) {
             $nostoProduct->setDescription(implode(' ', $descriptions));
         }
         if ($product->hasData('manufacturer')) {
             $nostoProduct->setBrand($product->getAttributeText('manufacturer'));
         }
         if (($tags = $this->buildTags($product)) !== []) {
             $nostoProduct->setTag1($tags);
         }
         if ($product->hasData('created_at')) {
             if ($timestamp = strtotime($product->getData('created_at'))) {
                 $nostoProduct->setDatePublished(new \NostoDate($timestamp));
             }
         }
     } catch (\NostoException $e) {
         $this->_logger->error($e, ['exception' => $e]);
     }
     $this->_eventManager->dispatch('nosto_product_load_after', ['product' => $nostoProduct]);
     return $nostoProduct;
 }