레지스트리를 사용하지 않고 현재 제품 가져오기 - Magento 2

4166 단어 magento2
우리는 현재 제품을 얻기 위해 Magento 레지스트리 방법을 사용하고 있습니다. 그러나 레지스트리 방법은 더 이상 사용되지 않습니다. 따라서 다음은 해결 방법입니다.

파보다/카탈로그/etc/frontend/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="catalog_controller_product_init_after">
        <observer name="current_product"
                  instance="Paboda\Catalog\Observer\RegisterCurrentProductObserver"/>
    </event>
</config>


파보다/카탈로그/레지스트리/CurrentProduct.php

<?php
namespace Paboda\Catalog\Registry;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\Data\ProductInterfaceFactory;

/**
 * Class CurrentProduct
 *
 * Get current product without registry
 */
class CurrentProduct
{
    /**
     * @var ProductInterface
     */
    private $product;

    /**
     * @var ProductInterfaceFactory
     */
    private $productFactory;

    /**
     * CurrentProduct constructor.
     *
     * @param ProductInterfaceFactory $productFactory
     */
    public function __construct(
        ProductInterfaceFactory $productFactory
    ) {
        $this->productFactory = $productFactory;
    }

    /**
     * Setter
     *
     * @param ProductInterface $product
     */
    public function set(ProductInterface $product): void
    {
        $this->product = $product;
    }

    /**
     * Getter
     *
     * @return ProductInterface
     */
    public function get(): ProductInterface
    {
        return $this->product ?? $this->createProduct();
    }

    /**
     * Product factory
     *
     * @return ProductInterface
     */
    private function createProduct(): ProductInterface
    {
        return $this->productFactory->create();
    }
}


파보다/카탈로그/관찰자/RegisterCurrentProductObserver.php

<?php
namespace Paboda\Catalog\Observer;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Framework\Event\Observer as Event;
use Magento\Framework\Event\ObserverInterface;
use Paboda/Catalog\Registry\CurrentProduct;

/**
 * Class RegisterCurrentProductObserver
 *
 * Current product observer
 */
class RegisterCurrentProductObserver implements ObserverInterface
{
    /**
     * @var CurrentProduct
     */
    private $currentProduct;

    /**
     * RegisterCurrentProductObserver constructor.
     *
     * @param CurrentProduct $currentProduct
     */
    public function __construct(
        CurrentProduct $currentProduct
    ) {
        $this->currentProduct = $currentProduct;
    }

    /**
     * Trigger event
     *
     * @param Event $event
     */
    public function execute(Event $event)
    {
        /** @var ProductInterface $product */
        $product = $event->getData('product');
        $this->currentProduct->set($product);
    }
}


모델 또는 블록에서 다음 방법을 사용하여 현재 제품을 호출할 수 있습니다.

예: 블록:

Paboda/Catalog/Block/Product.php

<?php
namespace Paboda\Catalog\Block;

use Magento\Framework\View\Element\Template;
use Paboda\Catalog\Registry\CurrentProduct;

class Product extends Template
{
    /**
     * @var CurrentProduct
     */
    protected $currentProduct;

    private $product;

    /**
     * Attachments constructor.
     *
     * @param Template\Context $context
     * @param CurrentProduct $currentProduct
     * @param array $data
     */
    public function __construct(
        Template\Context $context,
        CurrentProduct $currentProduct,
        array $data = []
    ) {
        $this->currentProduct = $currentProduct;
        parent::__construct($context, $data);
    }

    /**
     * Get product
     *
     * @return array
     * @throws \Magento\Framework\Exception\NoSuchEntityException
     */
    public function getCurrentProduct()
    {
        return $this->currentProduct->get();
    }
}

좋은 웹페이지 즐겨찾기