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

3
  • What error you're getting ? Or can you please tell me what is the path of this file ? catalog_product_view.xml Commented Feb 3, 2020 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
    Commented Feb 3, 2020 at 7:48
  • Please check my answer @Pramod Commented Feb 3, 2020 at 7:48

2 Answers 2

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!

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

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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