public function testAddProductWithoutSession()
 {
     /** @var $product \Magento\Catalog\Model\Product */
     $product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Catalog\\Model\\Product')->load(1);
     $this->_model->addProduct($product);
     $this->assertFalse($this->_model->hasItems(1, $this->_visitor->getId()));
     $this->assertTrue($this->_model->hasItems(0, $this->_visitor->getId()));
 }
 /**
  * Add Product to Compare Products List action
  *
  * Reset count of compared products cache
  *
  * @param \Magento\Framework\Event\Observer $observer
  * @return void
  */
 public function execute(\Magento\Framework\Event\Observer $observer)
 {
     $productId = $observer->getEvent()->getProduct()->getId();
     $viewData = ['product_id' => $productId];
     if ($this->_customerSession->isLoggedIn()) {
         $viewData['customer_id'] = $this->_customerSession->getCustomerId();
     } else {
         $viewData['visitor_id'] = $this->_customerVisitor->getId();
     }
     $this->_productCompFactory->create()->setData($viewData)->save()->calculate();
     $this->eventSaver->save(\Magento\Reports\Model\Event::EVENT_PRODUCT_COMPARE, $productId);
 }
 /**
  * Customer login action
  *
  * @param \Magento\Framework\Event\Observer $observer
  * @return $this
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 public function execute(\Magento\Framework\Event\Observer $observer)
 {
     if (!$this->_customerSession->isLoggedIn()) {
         return $this;
     }
     $visitorId = $this->_customerVisitor->getId();
     $customerId = $this->_customerSession->getCustomerId();
     $eventModel = $this->_eventFactory->create();
     $eventModel->updateCustomerType($visitorId, $customerId);
     $this->_productCompFactory->create()->updateCustomerFromVisitor()->calculate();
     $this->_productIndexFactory->create()->updateCustomerFromVisitor()->calculate();
     return $this;
 }
 /**
  * Save event
  *
  * @param int $eventTypeId
  * @param int $objectId
  * @param int|null $subjectId
  * @param int $subtype
  * @return void
  */
 public function save($eventTypeId, $objectId, $subjectId = null, $subtype = 0)
 {
     if ($subjectId === null) {
         if ($this->_customerSession->isLoggedIn()) {
             $subjectId = $this->_customerSession->getCustomerId();
         } else {
             $subjectId = $this->_customerVisitor->getId();
             $subtype = 1;
         }
     }
     /** @var \Magento\Reports\Model\Event $eventModel */
     $eventModel = $this->_eventFactory->create();
     $storeId = $this->_storeManager->getStore()->getId();
     $eventModel->setData(['event_type_id' => $eventTypeId, 'object_id' => $objectId, 'subject_id' => $subjectId, 'subtype' => $subtype, 'store_id' => $storeId]);
     $eventModel->save();
 }
Exemple #5
0
 /**
  * Retrieve visitor id
  *
  * if don't exists return current visitor id
  *
  * @return int
  */
 public function getVisitorId()
 {
     if ($this->hasData('visitor_id')) {
         return $this->getData('visitor_id');
     }
     return $this->_customerVisitor->getId();
 }
Exemple #6
0
 /**
  * Retrieve Visitor Id
  *
  * @return int
  */
 public function getVisitorId()
 {
     if (!$this->hasData('visitor_id')) {
         $visitorId = $this->_customerVisitor->getId();
         $this->setData('visitor_id', $visitorId);
     }
     return $this->getData('visitor_id');
 }
 /**
  * Add visitor and customer data to compare item
  *
  * @param \Magento\Catalog\Model\Product\Compare\Item $item
  * @return $this
  */
 protected function _addVisitorToItem($item)
 {
     $item->addVisitorId($this->_customerVisitor->getId());
     if ($this->_customerSession->isLoggedIn()) {
         $item->setCustomerId($this->_customerSession->getCustomerId());
     }
     return $this;
 }
 /**
  * Retrieve Where Condition to Index table
  *
  * @return array
  */
 protected function _getWhereCondition()
 {
     $condition = array();
     if ($this->_customerSession->isLoggedIn()) {
         $condition['customer_id'] = $this->_customerSession->getCustomerId();
     } elseif ($this->_customerId) {
         $condition['customer_id'] = $this->_customerId;
     } else {
         $condition['visitor_id'] = $this->_customerVisitor->getId();
     }
     return $condition;
 }
 /**
  * Retrieve Product Compare items collection
  *
  * @return \Magento\Catalog\Model\ResourceModel\Product\Compare\Item\Collection
  */
 public function getItems()
 {
     if ($this->_items === null) {
         $this->_compareProduct->setAllowUsedFlat(false);
         $this->_items = $this->_itemCollectionFactory->create();
         $this->_items->useProductItem(true)->setStoreId($this->_storeManager->getStore()->getId());
         if ($this->httpContext->getValue(Context::CONTEXT_AUTH)) {
             $this->_items->setCustomerId($this->currentCustomer->getCustomerId());
         } elseif ($this->_customerId) {
             $this->_items->setCustomerId($this->_customerId);
         } else {
             $this->_items->setVisitorId($this->_customerVisitor->getId());
         }
         $this->_items->addAttributeToSelect($this->_catalogConfig->getProductAttributes())->loadComparableAttributes()->addMinimalPrice()->addTaxPercents()->setVisibility($this->_catalogProductVisibility->getVisibleInSiteIds());
     }
     return $this->_items;
 }
Exemple #10
0
 /**
  * Calculate cache product compare collection
  *
  * @param bool $logout
  * @return $this
  */
 public function calculate($logout = false)
 {
     /** @var $collection Collection */
     $collection = $this->_itemCollectionFactory->create()->useProductItem(true);
     if (!$logout && $this->_customerSession->isLoggedIn()) {
         $collection->setCustomerId($this->_customerSession->getCustomerId());
     } elseif ($this->_customerId) {
         $collection->setCustomerId($this->_customerId);
     } else {
         $collection->setVisitorId($this->_customerVisitor->getId());
     }
     /* Price data is added to consider item stock status using price index */
     $collection->addPriceData()->setVisibility($this->_catalogProductVisibility->getVisibleInSiteIds());
     $count = $collection->getSize();
     $this->_catalogSession->setCatalogCompareItemsCount($count);
     return $this;
 }
Exemple #11
0
 /**
  * Generates user ID from Magento's visitor model
  * @return string
  */
 public function getUserIdByVisitor()
 {
     return md5($this->_customerVisitor->getId());
 }