Recommended.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. <?php
  2. namespace Dotdigitalgroup\Email\Helper;
  3. /**
  4. * Dynamic content configuration data and recommendation values.
  5. */
  6. class Recommended extends \Magento\Framework\App\Helper\AbstractHelper
  7. {
  8. const XML_PATH_RELATED_PRODUCTS_TYPE = 'connector_dynamic_content/products/related_display_type';
  9. const XML_PATH_UPSELL_PRODUCTS_TYPE = 'connector_dynamic_content/products/upsell_display_type';
  10. const XML_PATH_CROSSSELL_PRODUCTS_TYPE = 'connector_dynamic_content/products/crosssell_display_type';
  11. const XML_PATH_BESTSELLER_PRODUCT_TYPE = 'connector_dynamic_content/products/bestsellers_display_type';
  12. const XML_PATH_MOSTVIEWED_PRODUCT_TYPE = 'connector_dynamic_content/products/most_viewed_display_type';
  13. const XML_PATH_RECENTLYVIEWED_PRODUCT_TYPE = 'connector_dynamic_content/products/recently_viewed_display_type';
  14. const XML_PATH_PRODUCTPUSH_TYPE = 'connector_dynamic_content/manual_product_push/display_type';
  15. const XML_PATH_RELATED_PRODUCTS_ITEMS = 'connector_dynamic_content/products/related_items_to_display';
  16. const XML_PATH_UPSELL_PRODUCTS_ITEMS = 'connector_dynamic_content/products/upsell_items_to_display';
  17. const XML_PATH_CROSSSELL_PRODUCTS_ITEMS = 'connector_dynamic_content/products/crosssell_items_to_display';
  18. const XML_PATH_BESTSELLER_PRODUCT_ITEMS = 'connector_dynamic_content/products/bestsellers_items_to_display';
  19. const XML_PATH_MOSTVIEWED_PRODUCT_ITEMS = 'connector_dynamic_content/products/most_viewed_items_to_display';
  20. const XML_PATH_RECENTLYVIEWED_PRODUCT_ITEMS = 'connector_dynamic_content/products/recently_viewed_items_to_display';
  21. const XML_PATH_PRODUCTPUSH_DISPLAY_ITEMS = 'connector_dynamic_content/manual_product_push/items_to_display';
  22. const XML_PATH_BESTSELLER_TIME_PERIOD = 'connector_dynamic_content/products/bestsellers_time_period';
  23. const XML_PATH_MOSTVIEWED_TIME_PERIOD = 'connector_dynamic_content/products/most_viewed_time_period';
  24. const XML_PATH_PRODUCTPUSH_ITEMS = 'connector_dynamic_content/manual_product_push/products_push_items';
  25. const XML_PATH_FALLBACK_PRODUCTS_ITEMS = 'connector_dynamic_content/fallback_products/product_ids';
  26. /**
  27. * @var \Magento\Framework\App\Helper\Context
  28. */
  29. private $context;
  30. /**
  31. * @var Data
  32. */
  33. private $helper;
  34. /**
  35. * @var \Magento\Store\Model\StoreManagerInterface
  36. */
  37. private $storeManager;
  38. /**
  39. * @var \Magento\Framework\App\ResourceConnection
  40. */
  41. private $adapter;
  42. /**
  43. * @var \Zend_Date
  44. */
  45. private $date;
  46. /**
  47. * @var \Dotdigitalgroup\Email\Model\ResourceModel\Catalog
  48. */
  49. public $catalog;
  50. /**
  51. * Recommended constructor.
  52. *
  53. * @param \Magento\Framework\App\ResourceConnection $adapter
  54. * @param \Dotdigitalgroup\Email\Helper\Data $data
  55. * @param \Magento\Framework\App\Helper\Context $context
  56. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  57. * @param \Zend_Date $date
  58. */
  59. public function __construct(
  60. \Magento\Framework\App\ResourceConnection $adapter,
  61. \Dotdigitalgroup\Email\Helper\Data $data,
  62. \Magento\Framework\App\Helper\Context $context,
  63. \Magento\Store\Model\StoreManagerInterface $storeManager,
  64. \Zend_Date $date,
  65. \Dotdigitalgroup\Email\Model\ResourceModel\Catalog $catalog
  66. ) {
  67. $this->adapter = $adapter;
  68. $this->helper = $data;
  69. $this->context = $context;
  70. $this->storeManager = $storeManager;
  71. $this->date = $date;
  72. $this->catalog = $catalog;
  73. parent::__construct($context);
  74. }
  75. /**
  76. * Dispay type.
  77. *
  78. * @return string grid:list
  79. */
  80. public function getDisplayType()
  81. {
  82. $mode = $this->context->getRequest()->getActionName();
  83. $type = '';
  84. switch ($mode) {
  85. case 'related':
  86. $type = $this->getRelatedProductsType();
  87. break;
  88. case 'upsell':
  89. $type = $this->getUpsellProductsType();
  90. break;
  91. case 'crosssell':
  92. $type = $this->getCrosssellProductsType();
  93. break;
  94. case 'bestsellers':
  95. $type = $this->getBestSellerProductsType();
  96. break;
  97. case 'mostviewed':
  98. $type = $this->getMostViewedProductsType();
  99. break;
  100. case 'recentlyviewed':
  101. $type = $this->getRecentlyviewedProductsType();
  102. break;
  103. case 'push':
  104. $type = $this->getProductpushProductsType();
  105. }
  106. return $type;
  107. }
  108. /**
  109. * Get related product type.
  110. *
  111. * @return boolean|string
  112. */
  113. private function getRelatedProductsType()
  114. {
  115. return $this->scopeConfig->getValue(
  116. self::XML_PATH_RELATED_PRODUCTS_TYPE
  117. );
  118. }
  119. /**
  120. * Get upsell product type.
  121. *
  122. * @return boolean|string
  123. */
  124. private function getUpsellProductsType()
  125. {
  126. return $this->scopeConfig->getValue(
  127. self::XML_PATH_UPSELL_PRODUCTS_TYPE
  128. );
  129. }
  130. /**
  131. * Get crosssell product type.
  132. *
  133. * @return boolean|string
  134. */
  135. private function getCrosssellProductsType()
  136. {
  137. return $this->scopeConfig->getValue(
  138. self::XML_PATH_CROSSSELL_PRODUCTS_TYPE
  139. );
  140. }
  141. /**
  142. * Get bestseller product type.
  143. *
  144. * @return boolean|string
  145. */
  146. private function getBestSellerProductsType()
  147. {
  148. return $this->scopeConfig->getValue(
  149. self::XML_PATH_BESTSELLER_PRODUCT_TYPE
  150. );
  151. }
  152. /**
  153. * Get most viewed product type.
  154. *
  155. * @return boolean|string
  156. */
  157. private function getMostViewedProductsType()
  158. {
  159. return $this->scopeConfig->getValue(
  160. self::XML_PATH_MOSTVIEWED_PRODUCT_TYPE
  161. );
  162. }
  163. /**
  164. * Get recently viewed product type.
  165. *
  166. * @return boolean|string
  167. */
  168. private function getRecentlyviewedProductsType()
  169. {
  170. return $this->scopeConfig->getValue(
  171. self::XML_PATH_RECENTLYVIEWED_PRODUCT_TYPE
  172. );
  173. }
  174. /**
  175. * Get product push product type.
  176. *
  177. * @return boolean|string
  178. */
  179. private function getProductpushProductsType()
  180. {
  181. return $this->scopeConfig->getValue(self::XML_PATH_PRODUCTPUSH_TYPE);
  182. }
  183. /**
  184. * Limit of products displayed.
  185. *
  186. * @param string $mode
  187. *
  188. * @return int|mixed
  189. */
  190. public function getDisplayLimitByMode($mode)
  191. {
  192. $result = 0;
  193. switch ($mode) {
  194. case 'related':
  195. $result = $this->scopeConfig->getValue(
  196. self::XML_PATH_RELATED_PRODUCTS_ITEMS
  197. );
  198. break;
  199. case 'upsell':
  200. $result = $this->scopeConfig->getValue(
  201. self::XML_PATH_UPSELL_PRODUCTS_ITEMS
  202. );
  203. break;
  204. case 'crosssell':
  205. $result = $this->scopeConfig->getValue(
  206. self::XML_PATH_CROSSSELL_PRODUCTS_ITEMS
  207. );
  208. break;
  209. case 'bestsellers':
  210. $result = $this->scopeConfig->getValue(
  211. self::XML_PATH_BESTSELLER_PRODUCT_ITEMS
  212. );
  213. break;
  214. case 'mostviewed':
  215. $result = $this->scopeConfig->getValue(
  216. self::XML_PATH_MOSTVIEWED_PRODUCT_ITEMS
  217. );
  218. break;
  219. case 'recentlyviewed':
  220. $result = $this->scopeConfig->getValue(
  221. self::XML_PATH_RECENTLYVIEWED_PRODUCT_ITEMS
  222. );
  223. break;
  224. case 'push':
  225. $result = $this->scopeConfig->getValue(
  226. self::XML_PATH_PRODUCTPUSH_DISPLAY_ITEMS
  227. );
  228. }
  229. return $result;
  230. }
  231. /**
  232. * Fallback product ids.
  233. *
  234. * @return array
  235. */
  236. public function getFallbackIds()
  237. {
  238. $fallbackIds = $this->scopeConfig->getValue(
  239. self::XML_PATH_FALLBACK_PRODUCTS_ITEMS
  240. );
  241. if ($fallbackIds) {
  242. return explode(
  243. ',',
  244. $this->scopeConfig->getValue(
  245. self::XML_PATH_FALLBACK_PRODUCTS_ITEMS
  246. )
  247. );
  248. }
  249. return [];
  250. }
  251. /**
  252. * Get time period from config.
  253. *
  254. * @param string $config
  255. *
  256. * @return string
  257. */
  258. public function getTimeFromConfig($config)
  259. {
  260. $now = $this->date;
  261. $period = $this->processConfig($config);
  262. $sub = $this->processPeriod($period);
  263. if (isset($sub)) {
  264. $period = $now->sub(1, $sub);
  265. }
  266. return $period->toString(\Zend_Date::ISO_8601);
  267. }
  268. /**
  269. * @param string $config
  270. *
  271. * @return boolean|string
  272. */
  273. private function processConfig($config)
  274. {
  275. $period = null;
  276. if ($config == 'mostviewed') {
  277. $period = $this->scopeConfig->getValue(
  278. self::XML_PATH_MOSTVIEWED_TIME_PERIOD
  279. );
  280. } elseif ($config == 'bestsellers') {
  281. $period = $this->scopeConfig->getValue(
  282. self::XML_PATH_BESTSELLER_TIME_PERIOD
  283. );
  284. } elseif ($config == 'recentlyviewed') {
  285. $period = $this->scopeConfig->getValue(
  286. self::XML_PATH_MOSTVIEWED_TIME_PERIOD
  287. );
  288. }
  289. return $period;
  290. }
  291. /**
  292. * @param string $period
  293. *
  294. * @return null|string
  295. */
  296. private function processPeriod($period)
  297. {
  298. $sub = null;
  299. if ($period == 'week' || $period == 'W') {
  300. $sub = \Zend_Date::WEEK;
  301. } elseif ($period == 'month' || $period == 'M') {
  302. $sub = \Zend_Date::MONTH;
  303. } elseif ($period == 'year') {
  304. $sub = \Zend_Date::YEAR;
  305. } elseif ($period == 'D') {
  306. $sub = \Zend_Date::DAY;
  307. }
  308. return $sub;
  309. }
  310. /**
  311. * Get product push product ids.
  312. *
  313. * @return array
  314. */
  315. public function getProductPushIds()
  316. {
  317. $productIds = $this->scopeConfig->getValue(
  318. self::XML_PATH_PRODUCTPUSH_ITEMS
  319. );
  320. return explode(',', $productIds);
  321. }
  322. /**
  323. * Get products to display
  324. *
  325. * @param array $items
  326. * @param string $mode
  327. * @param int $productsToDisplayCounter
  328. * @param int $limit
  329. * @param int $maxPerChild
  330. *
  331. * @return array
  332. */
  333. public function getProductsToDisplay($items, $mode, &$productsToDisplayCounter, $limit, $maxPerChild)
  334. {
  335. $productsToDisplay = [];
  336. foreach ($items as $item) {
  337. //parent product
  338. $productModel = $item->getProduct();
  339. //check for product exists
  340. if ($productModel->getId()) {
  341. //get single product for current mode
  342. $recommendedProductIds = $this->getRecommendedProduct($productModel, $mode);
  343. $recommendedProducts = $this->catalog->getProductCollectionFromIds($recommendedProductIds);
  344. $this->addRecommendedProducts(
  345. $productsToDisplayCounter,
  346. $limit,
  347. $maxPerChild,
  348. $recommendedProducts,
  349. $productsToDisplay
  350. );
  351. }
  352. //have reached the limit don't loop for more
  353. if ($productsToDisplayCounter == $limit) {
  354. break;
  355. }
  356. }
  357. return $productsToDisplay;
  358. }
  359. /**
  360. * Add recommended products
  361. *
  362. * @param int $productsToDisplayCounter
  363. * @param int $limit
  364. * @param int $maxPerChild
  365. * @param array $recommendedProducts
  366. * @param array $productsToDisplay
  367. *
  368. * @return null
  369. */
  370. private function addRecommendedProducts(
  371. &$productsToDisplayCounter,
  372. $limit,
  373. $maxPerChild,
  374. $recommendedProducts,
  375. &$productsToDisplay
  376. ) {
  377. $i = 0;
  378. foreach ($recommendedProducts as $product) {
  379. //check if still exists
  380. if ($product->getId() && $productsToDisplayCounter < $limit
  381. && $i <= $maxPerChild
  382. && $product->isSaleable()
  383. && !$product->getParentId()
  384. ) {
  385. //we have a product to display
  386. $productsToDisplay[$product->getId()] = $product;
  387. $i++;
  388. $productsToDisplayCounter++;
  389. }
  390. }
  391. }
  392. /**
  393. * Fill products to display
  394. *
  395. * @param array $productsToDisplay
  396. * @param array $productsToDisplayCounter
  397. * @param int $limit
  398. *
  399. * @return array
  400. */
  401. public function fillProductsToDisplay($productsToDisplay, &$productsToDisplayCounter, $limit)
  402. {
  403. $fallbackIds = $this->getFallbackIds();
  404. $productCollection = $this->catalog->getProductCollectionFromIds($fallbackIds);
  405. foreach ($productCollection as $product) {
  406. if ($product->isSaleable()) {
  407. $productsToDisplay[$product->getId()] = $product;
  408. $productsToDisplayCounter++;
  409. }
  410. //the limit was reached
  411. if ($productsToDisplayCounter == $limit) {
  412. break;
  413. }
  414. }
  415. return $productsToDisplay;
  416. }
  417. /**
  418. * Product related items.
  419. *
  420. * @param \Magento\Catalog\Model\Product $productModel
  421. * @param string $mode
  422. *
  423. * @return array
  424. */
  425. private function getRecommendedProduct($productModel, $mode)
  426. {
  427. //array of products to display
  428. $products = [];
  429. switch ($mode) {
  430. case 'related':
  431. $products = $productModel->getRelatedProductIds();
  432. break;
  433. case 'upsell':
  434. $products = $productModel->getUpSellProductIds();
  435. break;
  436. case 'crosssell':
  437. $products = $productModel->getCrossSellProductIds();
  438. break;
  439. }
  440. return $products;
  441. }
  442. }