示例#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;
 }
示例#2
0
 /**
  * Loads the order info from a Magento order model.
  *
  * @param Order $order the order model.
  * @return \NostoOrder
  */
 public function build(Order $order)
 {
     $nostoOrder = new \NostoOrder();
     try {
         $nostoCurrency = new NostoCurrencyCode($order->getOrderCurrencyCode());
         $nostoOrder->setOrderNumber($order->getId());
         $nostoOrder->setExternalRef($order->getRealOrderId());
         $nostoOrder->setCreatedDate(new NostoDate(strtotime($order->getCreatedAt())));
         $nostoOrder->setPaymentProvider(new NostoOrderPaymentProvider($order->getPayment()->getMethod()));
         if ($order->getStatus()) {
             $nostoStatus = new NostoOrderStatus();
             $nostoStatus->setCode($order->getStatus());
             $nostoStatus->setLabel($order->getStatusLabel());
             $nostoOrder->setStatus($nostoStatus);
         }
         foreach ($order->getAllStatusHistory() as $item) {
             if ($item->getStatus()) {
                 $nostoStatus = new NostoOrderStatus();
                 $nostoStatus->setCode($item->getStatus());
                 $nostoStatus->setLabel($item->getStatusLabel());
                 $nostoStatus->setCreatedAt(new NostoDate(strtotime($item->getCreatedAt())));
                 $nostoOrder->addHistoryStatus($nostoStatus);
             }
         }
         // Set the buyer information
         $nostoBuyer = new NostoOrderBuyer();
         $nostoBuyer->setFirstName($order->getCustomerFirstname());
         $nostoBuyer->setLastName($order->getCustomerLastname());
         $nostoBuyer->setEmail($order->getCustomerEmail());
         $nostoOrder->setBuyer($nostoBuyer);
         // Add each ordered item as a line item
         /** @var Item $item */
         foreach ($order->getAllVisibleItems() as $item) {
             $nostoItem = new NostoOrderItem();
             $nostoItem->setItemId((int) $this->buildItemProductId($item));
             $nostoItem->setQuantity((int) $item->getQtyOrdered());
             $nostoItem->setName($this->buildItemName($item));
             try {
                 $nostoItem->setUnitPrice(new NostoPrice($this->_priceHelper->getItemFinalPriceInclTax($item)));
             } catch (\NostoInvalidArgumentException $E) {
                 $nostoItem->setUnitPrice(new NostoPrice(0));
             }
             $nostoItem->setCurrency($nostoCurrency);
             $nostoOrder->addItem($nostoItem);
         }
         // Add discounts as a pseudo line item
         if (($discount = $order->getDiscountAmount()) < 0) {
             $nostoItem = new NostoOrderItem();
             $nostoItem->setItemId(-1);
             $nostoItem->setQuantity(1);
             $nostoItem->setName($this->buildDiscountRuleDescription($order));
             $nostoItem->setUnitPrice(new NostoPrice($discount));
             $nostoItem->setCurrency($nostoCurrency);
             $nostoOrder->addItem($nostoItem);
         }
         // Add shipping and handling as a pseudo line item
         if (($shippingInclTax = $order->getShippingInclTax()) > 0) {
             $nostoItem = new NostoOrderItem();
             $nostoItem->setItemId(-1);
             $nostoItem->setQuantity(1);
             $nostoItem->setName('Shipping and handling');
             $nostoItem->setUnitPrice(new NostoPrice($shippingInclTax));
             $nostoItem->setCurrency($nostoCurrency);
             $nostoOrder->addItem($nostoItem);
         }
     } catch (Exception $e) {
         $this->_logger->error($e, ['exception' => $e]);
     }
     return $nostoOrder;
 }