0

I try to load the current product in gallery.phtml

Helper (app\code\Company\Base\Helper\Data.php):

...

use \Magento\Catalog\Helper\Data as CatalogHelper;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{

    ...

    /**
     * @var \Magento\Catalog\Model\Product
     */
    protected $_product;

    protected $_catalogHelper;

    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Framework\View\Layout\ProcessorFactory $layoutProcessorFactory,
        \Magento\Framework\View\Asset\Repository $assetRepo,
        \Magento\Cms\Model\BlockFactory $blockFactory,
        \Magento\Cms\Api\BlockRepositoryInterface $blockRepository,
        \Magento\Cms\Block\Block $cmsblock,
        \Magento\Catalog\Model\CategoryRepository $categoryRepository,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Checkout\Model\Session $checkoutSession,
        \Magento\Directory\Model\CurrencyFactory $currencyFactory, 
        \Magento\Catalog\Model\Product $product,
        \Magento\Framework\App\Http\Context $httpContext,
        \Magento\Framework\ObjectManagerInterface $interface,
        \Magento\Cms\Model\Template\FilterProvider $filterProvider,
        \Magento\Framework\Filesystem\DirectoryList $dir,
        \Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface,
        \Magento\Customer\Model\Customer $customer,
        \Magento\Customer\Model\Address $address,
        \Magento\Framework\App\Request\Http $request,
        CatalogHelper $catalogHelper,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig

    ) {
        $this->_layoutProcessorFactory = $layoutProcessorFactory;
        $this->_assetRepo = $assetRepo;
        $this->blockFactory = $blockFactory;
        $this->_blockRepository = $blockRepository;
        $this->cmsblock = $cmsblock;
        $this->categoryRepository = $categoryRepository;
        $this->storeManager = $storeManager;
        $this->_customerSession = $customerSession;
        $this->_checkoutSession = $checkoutSession;
        $this->_product = $product;
        $this->_httpContext = $httpContext;
        $this->_object = $interface;
        $this->_currencyCode = $currencyFactory->create();
        $this->_scopeConfig = $scopeConfig;
        $this->_customer = $customer;
        $this->_address = $address;
        $this->_filterProvider = $filterProvider;
        $this->_dir = $dir;
        $this->_request = $request;
        $this->_customerRepositoryInterface = $customerRepositoryInterface;

        $this->_store = $storeManager->getStore();
        $this->_catalogHelper = $catalogHelper;
    }

    /**
     * @return Product
     */
    public function getProduct()
    {
        if (is_null($this->_product)) {
            $this->_product = $this->_catalogHelper->getProduct();
        }

        // DEBUG
        var_dump($this->_product->getData()); exit();    // Output: array(0) { }


        return $this->_product;
    }

    public function getProductName()
    {
        return $this->getProduct()->getName();
    }

    ...

}

Layout (THEME\Magento_Catalog\layout\override\base\catalog_product_view.xml):

<container name="product.info.media" htmlTag="div" htmlClass="product media" after="product.info.main">
    <block class="Magento\Catalog\Block\Product\View\Gallery"
           name="product.info.media.image"
           template="Magento_Catalog::product/view/gallery.phtml">
        <arguments>
            <argument name="gallery_options" xsi:type="object">Magento\Catalog\Block\Product\View\GalleryOptions</argument>
            <argument name="imageHelper" xsi:type="object">Magento\Catalog\Helper\Image</argument>

            <argument name="product_name" xsi:type="helper" helper="Company\Base\Helper\Data::getProductName" />
        </arguments>
    </block>
</container>

gallery.phtml (THEME\Magento_Catalog\templates\product\view\gallery.phtml):

$productName = $block->getProductName();
exit("'".$productName."'");

Output:

array(0) { }
2
0

I solved it by removing lines from the constructor

public function __construct(
    \Magento\Framework\App\Helper\Context $context,
    \Magento\Framework\View\Layout\ProcessorFactory $layoutProcessorFactory,
    \Magento\Framework\View\Asset\Repository $assetRepo,
    \Magento\Cms\Model\BlockFactory $blockFactory,
    \Magento\Cms\Api\BlockRepositoryInterface $blockRepository,
    \Magento\Cms\Block\Block $cmsblock,
    \Magento\Catalog\Model\CategoryRepository $categoryRepository,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    \Magento\Customer\Model\Session $customerSession,
    \Magento\Checkout\Model\Session $checkoutSession,
    \Magento\Directory\Model\CurrencyFactory $currencyFactory, 
    // \Magento\Catalog\Model\Product $product,                <--- not needed anymore
    \Magento\Framework\App\Http\Context $httpContext,
    \Magento\Framework\ObjectManagerInterface $interface,
    \Magento\Cms\Model\Template\FilterProvider $filterProvider,
    \Magento\Framework\Filesystem\DirectoryList $dir,
    \Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface,
    \Magento\Customer\Model\Customer $customer,
    \Magento\Customer\Model\Address $address,
    \Magento\Framework\App\Request\Http $request,
    CatalogHelper $catalogHelper,
    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig

) {
    $this->_layoutProcessorFactory = $layoutProcessorFactory;
    $this->_assetRepo = $assetRepo;
    $this->blockFactory = $blockFactory;
    $this->_blockRepository = $blockRepository;
    $this->cmsblock = $cmsblock;
    $this->categoryRepository = $categoryRepository;
    $this->storeManager = $storeManager;
    $this->_customerSession = $customerSession;
    $this->_checkoutSession = $checkoutSession;
    // $this->_product = $product;            <--- I had to remove this
    $this->_httpContext = $httpContext;
    $this->_object = $interface;
    $this->_currencyCode = $currencyFactory->create();
    $this->_scopeConfig = $scopeConfig;
    $this->_customer = $customer;
    $this->_address = $address;
    $this->_filterProvider = $filterProvider;
    $this->_dir = $dir;
    $this->_request = $request;
    $this->_customerRepositoryInterface = $customerRepositoryInterface;

    $this->_store = $storeManager->getStore();
    $this->_catalogHelper = $catalogHelper;
}
0

try this code

protected $registry;

public function __construct(
    ......................................
    \Magento\Framework\Registry $registry,
    ......................................
) {
    ......................................
    $this->registry = $registry;
    ......................................
}

public function getCurrentProduct()
{
    return $this->registry->registry('current_product');
}

And in you phtml file

$product_data = $block->getCurrentProduct(); // here this is all data of current product 

$product_entityid = $product_data->getEntityId(); // get product id

$product_sku = $product_data->getSku(); // get sku

$product_name = $product_data->getName(); // get name 

exit("'".$product_name."'");

I Hope This Helps You.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.