Advertisement
Guest User

Block/NodeType/Product.php

a guest
Apr 7th, 2020
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.10 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Snowdog\Menu\Block\NodeType;
  4.  
  5. use Magento\Framework\Registry;
  6. use Magento\Framework\UrlInterface;
  7. use Snowdog\Menu\Model\TemplateResolver;
  8. use Magento\Framework\View\Element\Template\Context;
  9. use Snowdog\Menu\Model\NodeType\Product as ModelProduct;
  10. use Magento\Framework\Pricing\Helper\Data as PricingHelper;
  11.  
  12. class Product extends AbstractNode
  13. {
  14.     /**
  15.      * @var string
  16.      */
  17.     protected $defaultTemplate = 'menu/node_type/product.phtml';
  18.  
  19.     /**
  20.      * @var string
  21.      */
  22.     protected $nodeType = 'product';
  23.  
  24.     /**
  25.      * @var array
  26.      */
  27.     protected $nodes;
  28.  
  29.     /**
  30.      * @var array
  31.      */
  32.     protected $productUrls;
  33.  
  34.     /**
  35.      * @var array
  36.      */
  37.     protected $productPrices;
  38.  
  39.     /**
  40.      * @var array
  41.      */
  42.     protected $productImages;
  43.  
  44.     /**
  45.      * @var array
  46.      */
  47.     protected $productTitles;
  48.  
  49.     /**
  50.      * @var Registry
  51.      */
  52.     private $coreRegistry;
  53.  
  54.     /**
  55.      * @var ModelProduct
  56.      */
  57.     private $productModel;
  58.  
  59.     /**
  60.      * @var String
  61.      */
  62.     private $mediaUrl;
  63.  
  64.     /**
  65.      * @var PricingHelper
  66.      */
  67.     private $priceHelper;
  68.  
  69.     public function __construct(
  70.         Context $context,
  71.         Registry $coreRegistry,
  72.         ModelProduct $productModel,
  73.         TemplateResolver $templateResolver,
  74.         PricingHelper $priceHelper,
  75.         array $data = []
  76.     ) {
  77.         parent::__construct($context, $templateResolver, $data);
  78.         $this->coreRegistry = $coreRegistry;
  79.         $this->productModel = $productModel;
  80.         $this->priceHelper = $priceHelper;
  81.     }
  82.  
  83.     /**
  84.      * @return \Magento\Catalog\Model\Product|null
  85.      */
  86.     public function getCurrentProduct()
  87.     {
  88.         return $this->coreRegistry->registry('current_product');
  89.     }
  90.  
  91.     /**
  92.      * @return string
  93.      */
  94.     public function getJsonConfig()
  95.     {
  96.         $data = $this->productModel->fetchConfigData();
  97.  
  98.         return $data;
  99.     }
  100.  
  101.     /**
  102.      * @param array $nodes
  103.      */
  104.     public function fetchData(array $nodes)
  105.     {
  106.         $storeId = $this->_storeManager->getStore()->getId();
  107.  
  108.         list(
  109.             $this->nodes,
  110.             $this->productUrls,
  111.             $this->productPrices,
  112.             $this->productImages,
  113.             $this->productTitles,
  114.             $this->productColours
  115.         ) = $this->productModel->fetchData($nodes, $storeId);
  116.     }
  117.  
  118.     /**
  119.      * @param int $nodeId
  120.      * @return bool
  121.      * @throws \InvalidArgumentException
  122.      */
  123.     public function isCurrentProduct($nodeId)
  124.     {
  125.         if (!isset($this->nodes[$nodeId])) {
  126.             throw new \InvalidArgumentException('Invalid node identifier specified');
  127.         }
  128.  
  129.         $node = $this->nodes[$nodeId];
  130.         $productId = (int)$node->getContent();
  131.         $currentProduct = $this->getCurrentProduct();
  132.  
  133.         return $currentProduct
  134.             ? $currentProduct->getId() == $productId
  135.             : false;
  136.     }
  137.  
  138.     /**
  139.      * @param int $nodeId
  140.      * @param int|null $storeId
  141.      * @return string|false
  142.      * @throws \InvalidArgumentException
  143.      */
  144.     public function getProductUrl($nodeId, $storeId = null)
  145.     {
  146.         $productUrlPath = $this->getProductData($this->productUrls, $nodeId);
  147.  
  148.         if ($productUrlPath) {
  149.             $baseUrl = $this->_storeManager->getStore($storeId)->getBaseUrl();
  150.  
  151.             return $baseUrl . $productUrlPath;
  152.         }
  153.  
  154.         return false;
  155.     }
  156.  
  157.     /**
  158.      * @param int $nodeId
  159.      * @return double|false
  160.      * @throws \InvalidArgumentException
  161.      */
  162.     public function getProductPrice($nodeId)
  163.     {
  164.         return $this->getProductData($this->productPrices, $nodeId);
  165.     }
  166.  
  167.     /**
  168.      * @param int $nodeId
  169.      * @return null|string
  170.      */
  171.     public function getProductImage($nodeId)
  172.     {
  173.         $image = $this->getProductData($this->productImages, $nodeId);
  174.  
  175.         if (!$image) {
  176.             return null;
  177.         }
  178.  
  179.         return $this->getMediaUrl('catalog/product' . $image);
  180.     }
  181.  
  182.     /**
  183.      * @param array $data
  184.      * @param int $nodeId
  185.      * @return false|string|double
  186.      */
  187.     public function getProductData($data, $nodeId)
  188.     {
  189.         if (!isset($this->nodes[$nodeId])) {
  190.             throw new \InvalidArgumentException('Invalid node identifier specified');
  191.         }
  192.  
  193.         $node = $this->nodes[$nodeId];
  194.         $productId = (int)$node->getContent();
  195.  
  196.         if (isset($data[$productId])) {
  197.             return $data[$productId];
  198.         }
  199.  
  200.         return false;
  201.     }
  202.  
  203.     /**
  204.      * @param int $nodeId
  205.      * @param int $level
  206.      * @param int $storeId
  207.      *
  208.      * @return string
  209.      */
  210.     public function getHtml($nodeId, $level, $storeId = null)
  211.     {
  212.         $classes = $level == 0 ? 'level-top' : '';
  213.         $node = $this->nodes[$nodeId];
  214.         $url = $this->getProductUrl($nodeId, $storeId);
  215.         $title = $node->getTitle();
  216.  
  217.         return <<<HTML
  218. <a href="$url" class="$classes" role="menuitem"><span>$title</span></a>
  219. HTML;
  220.     }
  221.  
  222.     /**
  223.      * @param string $path
  224.      * @return string
  225.      */
  226.     private function getMediaUrl($path)
  227.     {
  228.         if (!$this->mediaUrl) {
  229.             $this->mediaUrl = $this->_storeManager->getStore()
  230.                 ->getBaseUrl(UrlInterface::URL_TYPE_MEDIA);
  231.         }
  232.  
  233.         return $this->mediaUrl . $path;
  234.     }
  235.  
  236.     /**
  237.      * @return \Magento\Framework\Phrase
  238.      */
  239.     public function getLabel()
  240.     {
  241.         return __("Product");
  242.     }
  243.  
  244.     /**
  245.      * @param int $nodeId
  246.      * @return false|string
  247.      */
  248.     public function getProductTitle($nodeId)
  249.     {
  250.         return $this->getProductData($this->productTitles, $nodeId);
  251.     }
  252.  
  253.     public function getProductColour($nodeId)
  254.     {
  255.         return $this->getProductData($this->productColours, $nodeId);
  256.     }
  257.  
  258.     /**
  259.      * @param int $nodeId
  260.      * @return float|string
  261.      */
  262.     public function getFormattedProductPrice($nodeId)
  263.     {
  264.         $productPrice = $this->getProductPrice($nodeId);
  265.  
  266.         return $this->priceHelper->currency($productPrice, true, false);
  267.     }
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement