1

I am currently to add a new tab just above add to cart button that will check the pin-code option but i failed to achieve this here is my code:-

catalog_product_view.xml

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>

        <referenceContainer name="product.info.main">
            <block class="Learn\Module\Block\Catalog\Product\View\CheckDelivery" name="textblock" as="textblock" after="-" template="catalog/product/view/zipcode.phtml" />
        </referenceContainer>

    </body>
</page>

vendor/module/Block/product/view/CheckDelivery.php

<?php

namespace Learn\Module\Block\Product\View;

use Magento\Framework\Registry;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;

class CheckDelivery extends Template
{
    protected $_registry;

    public function __construct(
        Context $context,
        Registry $registry,
        array $data = []
    ) {
        parent::__construct($context, $data);
        $this->_registry = $registry;
    }

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

vendor/module/view/frontend/templates/product/view/zipcode.phtml

<?php?>
<?php $_product = $block->getProduct() ?>
<h3><?php echo 'My Product Name Is: ' . $_product->getName(); ?></h3>

but this not displaying on frontend PDP any help on this

  • What error you're getting ? Or can you please tell me what is the path of this file ? catalog_product_view.xml – Kishan Savaliya Feb 3 at 7:38
  • vendor/module/view/frontend/layout/catalog_product_view.xml and am not getting any error rather it not visible on frotnend – Pramod Feb 3 at 7:48
  • Please check my answer @Pramod – Kishan Savaliya Feb 3 at 7:48
2

Please update this code in your catalog_product_view.xml file here in your custom module

app/code/Learn/Module/view/frontend/layout/catalog_product_view.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="product.info.main">
            <block class="Learn\Module\Block\Product\View\CheckDelivery" name="textblock" as="textblock" after="-" template="Learn_Module::catalog/product/view/zipcode.phtml" />
        </referenceContainer>
    </body>
</page>

Please check your Block file's path is correct in above XML file's block class. There you've added Learn\Module\Block\Catalog\Product\View\CheckDelivery and you've created that block file here Learn\Module\Block\Product\View\CheckDelivery. So please correct that and add your Block file here..

app/code/Learn/Module/Block/Product/View/CheckDelivery.php

Content for this file is..

<?php

namespace Learn\Module\Block\Product\View;

use Magento\Framework\Registry;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;

class CheckDelivery extends Template
{
    protected $_registry;

    public function __construct(
        Context $context,
        Registry $registry,
        array $data = []
    ) {
        parent::__construct($context, $data);
        $this->_registry = $registry;
    }

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

Now add your phtml file here in your custom module

app/code/Learn/Module/view/frontend/templates/catalog/product/view/zipcode.phtml

Content for this file is..

<?php $_product = $block->getCurrentProduct() ?>
<h3><?php echo 'My Product Name Is: ' . $_product->getName(); ?></h3>

In above file you've called $block->getProduct() this function and you've to call this one $block->getCurrentProduct(), because you've created getCurrentProduct() function in custom block file.

Output :

enter image description here

Hope this will help you!

|improve this answer|||||
  • tell me one thing i dnt helper for this thing to be displayed on frontend right?? – Pramod Feb 3 at 7:50
  • Means, what you're asking I do not understand. – Kishan Savaliya Feb 3 at 7:51
  • thank a lot the path was incorrect i didnt saw that – Pramod Feb 3 at 7:54
  • You're welcome Buddy :) You can accept answer if it helps. Thanks :-) – Kishan Savaliya Feb 3 at 7:54
0

You can review GitHub module for Product available on specific zipcode.

|improve this answer|||||
  • that would be complex i wana make my own for he better understanding – Pramod Feb 3 at 7:33
  • Ok. On github is the module I have tried yesterday. You can also try. – Vrajesh Patel Feb 3 at 7:38
  • can u provide me the link – Pramod Feb 3 at 7:46
  • I have already provided in my answer. – Vrajesh Patel Feb 3 at 7:50

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.