레지스트리를 사용하지 않고 현재 제품 가져오기 - Magento 2
4166 단어 magento2
파보다/카탈로그/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();
}
}
Reference
이 문제에 관하여(레지스트리를 사용하지 않고 현재 제품 가져오기 - Magento 2), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/pabodah/get-current-product-without-using-registry-magento-2-2pfl텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)