0

I need to hide product price in product list page. By below code, I can hide price but I need some product data for condition. I have read that $block variable is available in block but I am using container because I don't want to edit/change whole page.

Please help.

My layout xml file is : catalog_category_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">
  <head>
    <css src="Prince_Callforprice::css/fancybox/jquery.fancybox.css"/>
  </head>
  <body>
    <referenceContainer name="content">
         <block class="Prince\Callforprice\Block\Show" name="remove_price_and_addtocart_search_list" template="Prince_Callforprice::product/list/remove_price.phtml" cacheable="false" after="-"/>
    </referenceContainer>
  </body>
</page>

My phtml file is : remove_price.phtml

<?php

$_productCollection = $block->getLoadedProductCollection(); // error comes here

foreach ($_productCollection as $_product){ ?>
 <!-- product data -->
<?php } ?>

<?php  if($block->isCallforpriceEnable()){ 
      $btnLabel = 'Call For Price';
?>
 <script>
 var label = '<?php echo $btnLabel; ?>';
   require([
        "jquery",
   ], function($){
        var closeTags = $('.solcfpspan');
        $.each(closeTags,function(index,value){
          var closeButton = '<a href="#" class="fancybox action primary">'+label+'</a>';
            if($(this).parent().find('.product-item-inner .actions-primary')){ 
                $(this).parent().find('.product-item-inner .actions-primary').html(closeButton);
            }
            if($(this).parent().find('.product-item-actions .actions-primary')){
                $(this).parent().find('.product-item-actions .actions-primary').html(closeButton);
            }
        });
    });
 </script>
<?php
}
?>

[EDIT]

My Show.php file is:

<?php

namespace Prince\Callforprice\Block;

use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Catalog\Model\Category;
use Magento\Catalog\Model\Product;
use Magento\Eav\Model\Entity\Collection\AbstractCollection;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\DataObject\IdentityInterface;

class Show extends \Magento\Framework\View\Element\Template
{

protected $_helper;

public function __construct(
    \Magento\Catalog\Block\Product\Context $context,
    \Magento\Framework\Data\Helper\PostHelper $postDataHelper,
    \Magento\Catalog\Model\Layer\Resolver $layerResolver,
    CategoryRepositoryInterface $categoryRepository,
    \Magento\Framework\Url\Helper\Data $urlHelper,
    \Prince\Callforprice\Helper\Data $helper,
    array $data = []
) {
    $this->_catalogLayer = $layerResolver->get();
    $this->_postDataHelper = $postDataHelper;
    $this->categoryRepository = $categoryRepository;
    $this->urlHelper = $urlHelper;
    $this->_helper = $helper;
    parent::__construct(
        $context,
        $postDataHelper,
        $layerResolver,
        $categoryRepository,
        $urlHelper,
        $data
    );
}    

public function getLabel()
{
    return $this->_helper->getConfigValue('callforpricesection/callforpricegroup/callforpricebuttontext');
}

public function isCallforpriceEnable()
{
    return $this->_helper->getConfigValue('callforpricesection/callforpricegroup/active');
}   
}

It gives fatat error: "Object DOMDocument should be created.";i:1;s:13450:"#0 /214fresh/vendor/magento/framework/View/Element/UiComponent/Config/Reader.php(95): Magento\Framework\View\Element\UiComponent\Config\DomMerger->getDom()

1

your block class should be like this:

<?php
namespace Prince\Callforprice\Block;

use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Catalog\Model\Category;
use Magento\Catalog\Model\Product;
use Magento\Eav\Model\Entity\Collection\AbstractCollection;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\DataObject\IdentityInterface;

class Show extends \Magento\Catalog\Block\Product\ListProduct
{

    public function __construct(
            \Magento\Catalog\Block\Product\Context $context,
            \Magento\Framework\Data\Helper\PostHelper $postDataHelper,
            \Magento\Catalog\Model\Layer\Resolver $layerResolver,
            CategoryRepositoryInterface $categoryRepository,
            \Magento\Framework\Url\Helper\Data $urlHelper,
            array $data = []
        ) {
            $this->_catalogLayer = $layerResolver->get();
            $this->_postDataHelper = $postDataHelper;
            $this->categoryRepository = $categoryRepository;
            $this->urlHelper = $urlHelper;
            parent::__construct(
                $context,
                $postDataHelper,
                $layerResolver,
                $categoryRepository,
                $urlHelper,
                $data
            );
        }
    public function isCallforpriceEnable(){
        //your code
    }
}
  • @Sarafaraz, I have updated my question. After your code, it give me fatal error. – Prince May 1 '17 at 12:26
  • You have extended : \Magento\Framework\View\Element\Template Please extend this: \Magento\Catalog\Block\Product\ListProduct – Sarfaraz bheda May 1 '17 at 12:28
  • also declare remaining protected variables – Sarfaraz bheda May 1 '17 at 12:29
  • @Sarfarazbheda please help link1 , link2 , link3 – css beginner May 2 '17 at 10:42

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.