0

I want to get all products id in magento 2. I tried many times but it is not working. Thanks in Advance

Actually I'm using plugin to hide price and add to cart button now I want to hide the price and add to cart button for specific categories.. like you can see in image enter image description here

now to hide add to cart and price for specific categories my code is

> namespace PME\Hideprice\Plugin

<?php

declare(strict_types=1);

namespace PME\Hideprice\Plugin;

use Magento\Framework\App\Http\Context;
use Magento\Customer\Model\Context as CustomerContext;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;
use PME\Hideprice\Helper\Data as Helper;

/**
 * Class IsSalablePlugin
 *
 * @category Plugin
 * @package  PME\Hideprice\Plugin
 */
class IsSalablePlugin
{
    /**
     * Scope config
     *
     * @var ScopeConfigInterface
     */
    protected $scopeConfig;

    /**
     * HTTP Context
     * Customer session is not initialized yet
     *
     * @var Context
     */
    protected $context;

    protected $_initialValue = false;

    const DISABLE_ADD_TO_CART = 'pme_hideprice/general/catalog_frontend_disable_add_to_cart_for_guest';

    /**
     * SalablePlugin constructor.
     *
     * @param ScopeConfigInterface $scopeConfig ScopeConfigInterface
     * @param Context              $context     Context
     */
    public function __construct(
        ScopeConfigInterface $scopeConfig,
        Context $context,
        Helper $helper
    ) {
        $this->scopeConfig = $scopeConfig;
        $this->context = $context;
        $this->_helper = $helper;
    }

    /**
     * 
     */
    public function afterIsSalable(
        \Magento\Catalog\Model\Product $subject,
        $result
    )
    {

        $categories_main=$this->_helper->arrayData();
        $categories = array_pop($categories_main);
        $categoriess[] = $categories;

        $allowed= $this->_helper->getCurrentCategory();
        $result=array_intersect($categoriess,$allowed);

        if (!empty($result)) {

        $this->initialValue = $result;
        $scope = ScopeInterface::SCOPE_STORE; 
            if($this->_helper->getIsEnable()) {
                if ($this->scopeConfig->getValue(self::DISABLE_ADD_TO_CART, $scope)) {
                    if ($this->_helper->isCustomerLoggedIn()) {
                        return true;
                    }
                    return false;
                } 
                return true; 
            }    
            return $result;   
        }
    }

    public function afterGetIsSalable (
        \Magento\Catalog\Model\Product $subject,
        $result
    ) {

        $categories_main=$this->_helper->arrayData();
        $categories = array_pop($categories_main);
        $categoriess[] = $categories;

        $allowed= $this->_helper->getCurrentCategory();
        $result=array_intersect($categoriess,$allowed);

        if (!empty($result)) {

            $scope = ScopeInterface::SCOPE_STORE; 
            if($this->_helper->getIsEnable()) {
                if ($this->scopeConfig->getValue(self::DISABLE_ADD_TO_CART, $scope)) {
                    if ($this->_helper->isCustomerLoggedIn()) {
                        return true;
                    }
                    return $this->initialValue;
                }
                return true;  
            } 
            return $result;
         }
    }    
}

now for this my Helper is

PME\Hideprice\Helper

<?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
{

    protected $getarray;

    /**
     * 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
     * @param \Magento\Framework\Registry $registry,
     */

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

        $this->_customerSession = $_customerSession;
        $this->scopeConfig = $scopeConfig;
        $this->context = $context;
        $this->_registry = $registry;
        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
        );
    }

    public function arrayData(){

            $getarray = $this->scopeConfig->getValue('pme_hideprice/general/category',
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
            return explode ( ',',$getarray);

    }

    /**
 * Return catalog current category object
 *
 * @return \Magento\Catalog\Model\Category
 */

public function getCurrentCategory()
{
    return $this->_registry->registry('current_category');
}



}

now in this the issue is I'm using registry and the registry only get the current product category.. so its not works properly. enter image description here

maybe after this problem might be logic problem if you can also set it for me I'll be really thankful to you

3
  • Please post your code. Dec 21, 2018 at 0:41
  • @VivekKumar ok buddy I'm going to upload the code... n the purpose for what I want to use it... it will be really glad for me if you solve my problem coz this is now gonna headache for me.
    – Asad Khan
    Dec 21, 2018 at 1:21
  • @VivekKumar buddy I just upload the code I wish that you will solve my Problem Thanks in advance
    – Asad Khan
    Dec 21, 2018 at 1:31

2 Answers 2

2

You can use ProductRepository to get all Products Id in Magento 2.

Maybe this code will useful for you:

class Product extends Action
{
    private $productRepository;
    /**
     * @var SearchCriteriaBuilder
     */
    private $searchCriteriaBuilder;

    public function __construct(
        Context $context,
        ProductRepositoryInterface $productRepository,
        SearchCriteriaBuilder $searchCriteriaBuilder
    ) {
        parent::__construct($context);
        $this->productRepository = $productRepository;
        $this->searchCriteriaBuilder = $searchCriteriaBuilder;
    }

    public function execute()
    {
        $this->getResponse()->setHeader('Content-Type', 'text/plain');
        $criteria = $this->searchCriteriaBuilder->create();
        $products = $this->productRepository->getList($criteria);
        foreach ($products as $product) {
            $this->getResponse()->appendBody(sprintf(
                    "(%d)",
                    $product->getId())
            );
        }
    }
}

Cheers!

1
  • This is overkill as it loads full product models when you only need IDs...
    – Wouter
    Jan 15, 2020 at 12:31
1

The problem is this:

$allowed= $this->_helper->getCurrentCategory();
$result=array_intersect($categoriess,$allowed);

$allowed is not an array. It's an instance of the category model.
you can change to this:

$result=array_intersect($categoriess, [$allowed->getId()]);
1
  • now it gives me the further problem Fatal error: Uncaught Error: Call to a member function getId() on null in C:\xampp\htdocs\magento\app\code\PME\Hideprice\Plugin\IsSalablePlugin.php:71 Stack trace
    – Asad Khan
    Dec 21, 2018 at 10:05

Your Answer

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

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