2

Actually I hide the price using Plugin but now I want to hide price only for specific categories like I only want to hide the Price for bags. I want to do this thing by using Admin configuration like if I enable the extension then there is a option to select where I want to hide the price.n

di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">


        <type name="Magento\Catalog\Pricing\Render\FinalPriceBox">
            <plugin name="price_hide" type="PME\Hideprice\Plugin\Price"/>        
        </type>

</config>

helper\data.php

<?php
namespace PME\Hideprice\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Http\Context;
use Magento\Customer\Model\Context as CustomerContext;

class Data extends AbstractHelper
{

    /**
     * Scope config
     *
     * @var ScopeConfigInterface
     */
    protected $scopeConfig;

    /**
     * HTTP Context
     * Customer session is not initialized yet
     *
     * @var Context
     */

    protected $context;

     /**
     * SalablePlugin constructor.
     *
     * @param ScopeConfigInterface $scopeConfig ScopeConfigInterface
     * @param Context              $context     Context
     */

    public function __construct(
        ScopeConfigInterface $scopeConfig,
        Context $context,
        \Magento\Framework\App\Helper\Context $helpercontext,
        \Magento\Customer\Model\Session $_customerSession
    ) {

        $this->_customerSession = $_customerSession;
        $this->scopeConfig = $scopeConfig;
        $this->context = $context;
        parent::__construct($helpercontext);
    }

    public function isCustomerLoggedIn() {

        return $this->context->getValue(CustomerContext::CONTEXT_AUTH);
        //to check login mean that to check that the customer is logged in or not
    }

    public function getIsEnable(){
        return $this->scopeConfig->getValue('pme_hideprice/general/enabled', 
                                            \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
    }

    public function isEnabled($scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT)
    {
        return $this->scopeConfig->isSetFlag(
            'pme_hideprice/general/enabled',
            $scope
        );
    }
}

Plugin\Price.php

<?php

namespace PME\Hideprice\Plugin;

use PME\Hideprice\Helper\Data as Helper;

    class Price
    {

        public function __construct (
            Helper $helper,
            array $data = []
        ) {
                $this->helper = $helper;
        }

        function afterToHtml(\Magento\Catalog\Pricing\Render\FinalPriceBox $subject, $result) {

            // echo $this->helper->isCustomerLoggedIn(); exit;
            if($this->helper->getIsEnable())
            {
                if(!$this->helper->isCustomerLoggedIn())
                {


                    return '';
                }
            }         
            return $result;
        }

    }
  • What is the plugin you have written? Can you post your plugin file and di.xml which you have created? – Sukumar Gorai Dec 17 '18 at 11:38
  • @SukumarGorai yeah sure – Asad Khan Dec 17 '18 at 11:41
  • @SukumarGorai buddy now u can see my code... – Asad Khan Dec 17 '18 at 11:48
  • 1
    You can check the current category by adding registry to your Helper function and also if the current page category page then you need to use that code otherwise use default code. – Sukumar Gorai Dec 17 '18 at 11:58

Your Answer

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

Browse other questions tagged or ask your own question.