Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Neom\CategoryLanding\Block\Widget\Bestsellers;
  4.  
  5. use Magento\Catalog\Block\Product\AbstractProduct;
  6. use Magento\Catalog\Block\Product\Context;
  7. use Magento\Catalog\Block\Product\ImageBlockBuilder;
  8. use Magento\Catalog\Model\Product;
  9. use Magento\Catalog\Block\Widget\Link as CatalogLink;
  10. use Neom\CategoryLanding\Provider\Widget\Bestsellers\Product\CollectionProvider;
  11.  
  12. /**
  13. * @author Diego Cabrejas <diego@wearejh.com>
  14. */
  15. class ProductList extends AbstractProduct
  16. {
  17. /**
  18. * @var CollectionProvider
  19. */
  20. private $collectionProvider;
  21. /**
  22. * @var ProductLink
  23. */
  24. private $productLink;
  25.  
  26. public function __construct(
  27. Context $context,
  28. CollectionProvider $collectionProvider,
  29. CatalogLink $productLink,
  30. array $data = [],
  31. ImageBlockBuilder $imageBlockBuilder = null,
  32. string $var = ''
  33. ) {
  34. $this->collectionProvider = $collectionProvider;
  35. parent::__construct($context, $data, $imageBlockBuilder);
  36. $this->setTemplate('widget/bestsellers/product-list.phtml');
  37. $this->productLink = $productLink;
  38. }
  39.  
  40. public function getProducts() : array
  41. {
  42. $products = $this->collectionProvider->getCollection($this->getProductIds())->getItems();
  43. usort($products, [$this, 'sortProducts']);
  44.  
  45. //mark first item as Nicola's pick
  46. if (count($products)) {
  47. reset($products)->setIsNicolasPick(true);
  48. }
  49.  
  50. return $products;
  51. }
  52.  
  53. private function getProductIds() : array
  54. {
  55. $ids = [];
  56.  
  57. foreach ($this->getProductIdPaths() as $idPath) {
  58. $ids[] = $this->getProductIdByPath($idPath);
  59. }
  60.  
  61. return $ids;
  62. }
  63.  
  64. private function getProductIdPaths() : array
  65. {
  66. return $this->getData('product_id_paths') ?? [];
  67. }
  68.  
  69. private function getProductIdByPath(string $idPath) : string
  70. {
  71. $parts = explode('/', $idPath);
  72.  
  73. if (!isset($parts[0]) || !isset($parts[1])) {
  74. throw new \RuntimeException('Wrong id_path structure.');
  75. }
  76.  
  77. return $parts[1];
  78. }
  79.  
  80. private function getProductPathByProductId(int $id) : string
  81. {
  82. foreach ($this->getProductIdPaths() as $idPath) {
  83. $productId = $this->getProductIdByPath($idPath);
  84. if ($productId === $id) {
  85. return $idPath;
  86. }
  87. }
  88.  
  89. return '';
  90. }
  91.  
  92. private function sortProducts(Product $productA, Product $productB)
  93. {
  94. $productIds = $this->getProductIds();
  95. return array_search($productA->getId(), $productIds) <=> array_search($productB->getId(), $productIds);
  96. }
  97.  
  98. public function getProductLink(Product $product) : string
  99. {
  100. $idPath = $this->getProductPathByProductId($product->getId());
  101. $this->productLink->setData('id_path', $idPath);
  102. return $this->productLink->getHref();
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement