/**
  * Automation new wishlist program.
  *
  * @param array $wishlist
  *
  * @return $this
  *
  * @throws \Magento\Framework\Exception\LocalizedException
  */
 public function registerWishlist($wishlist)
 {
     try {
         $emailWishlist = $this->wishlistFactory->create();
         $customer = $this->customerFactory->create();
         //if wishlist exist not to save again
         if (!$emailWishlist->getWishlist($wishlist['wishlist_id'])) {
             $customer->load($wishlist['customer_id']);
             $email = $customer->getEmail();
             $wishlistId = $wishlist['wishlist_id'];
             $websiteId = $customer->getWebsiteId();
             $emailWishlist->setWishlistId($wishlistId)->setCustomerId($wishlist['customer_id'])->setStoreId($customer->getStoreId())->save();
             $store = $this->storeManager->getStore($customer->getStoreId());
             $storeName = $store->getName();
             //if api is not enabled
             if (!$this->helper->isEnabled($websiteId)) {
                 return $this;
             }
             $programId = $this->helper->getWebsiteConfig('connector_automation/visitor_automation/wishlist_automation', $websiteId);
             //wishlist program mapped
             if ($programId) {
                 $automation = $this->automationFactory->create();
                 //save automation type
                 $automation->setEmail($email)->setAutomationType(\Dotdigitalgroup\Email\Model\Sync\Automation::AUTOMATION_TYPE_NEW_WISHLIST)->setEnrolmentStatus(\Dotdigitalgroup\Email\Model\Sync\Automation::AUTOMATION_STATUS_PENDING)->setTypeId($wishlistId)->setWebsiteId($websiteId)->setStoreName($storeName)->setProgramId($programId);
                 $automation->save();
             }
         }
     } catch (\Exception $e) {
         $this->helper->error((string) $e, []);
     }
 }
 /**
  * If it's configured to capture on shipment - do this.
  *
  * @param \Magento\Framework\Event\Observer $observer
  *
  * @return $this
  */
 public function execute(\Magento\Framework\Event\Observer $observer)
 {
     $object = $observer->getEvent()->getDataObject();
     $wishlist = $this->wishlist->create()->load($object->getWishlistId());
     $emailWishlist = $this->wishlistFactory->create();
     try {
         if ($object->getWishlistId()) {
             $itemCount = count($wishlist->getItemCollection());
             $item = $emailWishlist->getWishlist($object->getWishlistId());
             if ($item && $item->getId()) {
                 $preSaveItemCount = $item->getItemCount();
                 if ($itemCount != $item->getItemCount()) {
                     $item->setItemCount($itemCount);
                 }
                 if ($itemCount == 1 && $preSaveItemCount == 0) {
                     $item->setWishlistImported(null);
                 } elseif ($item->getWishlistImported()) {
                     $item->setWishlistModified(1);
                 }
                 $item->save();
             }
         }
     } catch (\Exception $e) {
         $this->helper->debug((string) $e, []);
     }
     return $this;
 }
 /**
  * If it's configured to capture on shipment - do this.
  *
  * @param \Magento\Framework\Event\Observer $observer
  *
  * @return $this
  */
 public function execute(\Magento\Framework\Event\Observer $observer)
 {
     $object = $observer->getEvent()->getDataObject();
     $customer = $this->customerFactory->create()->load($object->getCustomerId());
     $website = $this->storeManager->getStore($customer->getStoreId())->getWebsite();
     //sync enabled
     $syncEnabled = $this->helper->getWebsiteConfig(\Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_SYNC_WISHLIST_ENABLED, $website->getId());
     if ($this->helper->isEnabled($website->getId()) && $syncEnabled) {
         //Remove wishlist
         try {
             $item = $this->wishlistFactory->create()->getWishlist($object->getWishlistId());
             if ($item instanceof \Magento\Framework\DataObject && $item->getId()) {
                 //register in queue with importer
                 $this->importerFactory->create()->registerQueue(\Dotdigitalgroup\Email\Model\Importer::IMPORT_TYPE_WISHLIST, [$item->getId()], \Dotdigitalgroup\Email\Model\Importer::MODE_SINGLE_DELETE, $website->getId());
                 $item->delete();
             }
         } catch (\Exception $e) {
             $this->helper->debug((string) $e, []);
         }
     }
 }
 /**
  * @return \Magento\Backend\Model\View\Result\Redirect
  */
 public function execute()
 {
     $ids = $this->getRequest()->getParam('selected');
     if (!is_array($ids)) {
         $this->messageManager->addErrorMessage(__('Please select wishlist.'));
     } else {
         try {
             //@codingStandardsIgnoreStart
             foreach ($ids as $id) {
                 $model = $this->wishlist->create()->setId($id);
                 $model->delete();
             }
             //@codingStandardsIgnoreEnd
             $this->messageManager->addSuccessMessage(__('Total of %1 record(s) were deleted.', count($ids)));
         } catch (\Exception $e) {
             $this->messageManager->addErrorMessage($e->getMessage());
         }
     }
     /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
     $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
     $resultRedirect->setPath('*/*/');
     return $resultRedirect;
 }