Product.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace Dotdigitalgroup\Email\Block\Recommended;
  3. /**
  4. * Product block
  5. *
  6. * @api
  7. */
  8. class Product extends \Magento\Catalog\Block\Product\AbstractProduct
  9. {
  10. /**
  11. * @var \Dotdigitalgroup\Email\Helper\Data
  12. */
  13. public $helper;
  14. /**
  15. * @var \Dotdigitalgroup\Email\Helper\Recommended
  16. */
  17. public $recommendedHelper;
  18. /**
  19. * @var \Magento\Sales\Model\OrderFactory
  20. */
  21. public $orderFactory;
  22. /**
  23. * @var \Magento\Sales\Model\ResourceModel\Order
  24. */
  25. private $orderResource;
  26. /**
  27. * Product constructor.
  28. *
  29. * @param \Magento\Catalog\Block\Product\Context $context
  30. * @param \Magento\Sales\Model\ResourceModel\Order $orderResource
  31. * @param \Magento\Sales\Model\OrderFactory $orderFactory
  32. * @param \Dotdigitalgroup\Email\Helper\Recommended $recommended
  33. * @param \Dotdigitalgroup\Email\Helper\Data $helper
  34. * @param array $data
  35. */
  36. public function __construct(
  37. \Magento\Catalog\Block\Product\Context $context,
  38. \Magento\Sales\Model\ResourceModel\Order $orderResource,
  39. \Magento\Sales\Model\OrderFactory $orderFactory,
  40. \Dotdigitalgroup\Email\Helper\Recommended $recommended,
  41. \Dotdigitalgroup\Email\Helper\Data $helper,
  42. array $data = []
  43. ) {
  44. parent::__construct($context, $data);
  45. $this->orderFactory = $orderFactory;
  46. $this->recommendedHelper = $recommended;
  47. $this->helper = $helper;
  48. $this->orderResource = $orderResource;
  49. }
  50. /**
  51. * Get the products to display for recommendation.
  52. *
  53. * @return array
  54. */
  55. public function getLoadedProductCollection()
  56. {
  57. $params = $this->getRequest()->getParams();
  58. //check for param code and id
  59. if (! isset($params['order_id']) ||
  60. ! isset($params['code']) ||
  61. ! $this->helper->isCodeValid($params['code'])
  62. ) {
  63. $this->helper->log('Product recommendation for this order not found or invalid code');
  64. return [];
  65. }
  66. //products to be displayed for recommended pages
  67. $orderId = (int) $this->getRequest()->getParam('order_id');
  68. //display mode based on the action name
  69. $mode = $this->getRequest()->getActionName();
  70. $orderModel = $this->orderFactory->create();
  71. $this->orderResource->load($orderModel, $orderId);
  72. //number of product items to be displayed
  73. $limit = $this->recommendedHelper
  74. ->getDisplayLimitByMode($mode);
  75. $orderItems = $orderModel->getAllItems();
  76. $numItems = count($orderItems);
  77. //no product found to display
  78. if ($numItems == 0 || !$limit) {
  79. return [];
  80. } elseif ($numItems > $limit) {
  81. $maxPerChild = 1;
  82. } else {
  83. $maxPerChild = number_format($limit / $numItems);
  84. }
  85. $this->helper->log(
  86. 'DYNAMIC PRODUCTS : limit ' . $limit . ' products : '
  87. . $numItems . ', max per child : ' . $maxPerChild
  88. );
  89. $productsToDisplayCounter = 0;
  90. $productsToDisplay = $this->recommendedHelper->getProductsToDisplay(
  91. $orderItems,
  92. $mode,
  93. $productsToDisplayCounter,
  94. $limit,
  95. $maxPerChild
  96. );
  97. //check for more space to fill up the table with fallback products
  98. if ($productsToDisplayCounter < $limit) {
  99. $productsToDisplay = $this->recommendedHelper->fillProductsToDisplay(
  100. $productsToDisplay,
  101. $productsToDisplayCounter,
  102. $limit
  103. );
  104. }
  105. $this->helper->log('loaded product to display ' . count($productsToDisplay));
  106. return $productsToDisplay;
  107. }
  108. /**
  109. * Diplay mode type.
  110. *
  111. * @return mixed|string
  112. */
  113. public function getMode()
  114. {
  115. return $this->recommendedHelper->getDisplayType();
  116. }
  117. /**
  118. * Number of the columns.
  119. *
  120. * @return int|mixed
  121. */
  122. public function getColumnCount()
  123. {
  124. return $this->recommendedHelper->getDisplayLimitByMode(
  125. $this->getRequest()
  126. ->getActionName()
  127. );
  128. }
  129. /**
  130. * @param null|string|bool|int|\Magento\Store\Api\Data\StoreInterface $store
  131. *
  132. * @return string
  133. */
  134. public function getTextForUrl($store)
  135. {
  136. $store = $this->_storeManager->getStore($store);
  137. return $store->getConfig(
  138. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_DYNAMIC_CONTENT_LINK_TEXT
  139. );
  140. }
  141. }