3

This is how I tried but its not working Bodak/DisableAddToCart/etc/adminhtml/system.xml

    <?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        <section id="catalog">
            <group id="frontend">
                <field id="catalog_frontend_disable_add_to_cart_for_guest" translate="label comment" type="select" sortOrder="250" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Disable Add to cart button for Guests</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
            </group>
        </section>
    </system>
</config>

Then in Bodak/DisableAddToCart/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Model\Product">
        <plugin name="Bodak_IsSalablePlugin::after" type="Bodak\DisableAddToCart\Plugin\IsSalablePlugin"/>
    </type>
</config>

Then after that Bodak/DisableAddToCart/Plugin/IsSalablePlugin.php

<?php

declare(strict_types=1);

namespace Bodak\DisableAddToCart\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;

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

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

    const DISABLE_ADD_TO_CART = 'catalog/frontend/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
    ) {
        $this->scopeConfig = $scopeConfig;
        $this->context = $context;
    }

    /**
     * Check if is disable add to cart and if customer is logged in
     *
     * @return bool
     */
    public function afterIsSalable(): bool
    {
        $scope = ScopeInterface::SCOPE_STORE;

        if ($this->scopeConfig->getValue(self::DISABLE_ADD_TO_CART, $scope)) {
            if ($this->context->getValue(CustomerContext::CONTEXT_AUTH)) {
                return true;
            }
            return false;
        }
        return true;
    }
}
4
2

Follow below steps to hide Add to Cart button for Guest user.

File path: magento/app/code/Guest/AddtoCartHideGuest/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Guest_AddtoCartHideGuest',
    __DIR__
);

File path: magento/app/code/Guest/AddtoCartHideGuest/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Guest_AddtoCartHideGuest" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Catalog"/>
        </sequence>
    </module>
</config>

File path: magento/app/code/Guest/AddtoCartHideGuest/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Model\Product">
        <plugin name="Guest_IsSalablePlugin::after" type="Guest\AddtoCartHideGuest\Plugin\IsSalablePlugin"/>
    </type>
</config>

File path: magento/app/code/Guest/AddtoCartHideGuest/etc/adminhtml/system.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        <tab id="guest" translate="label" sortOrder="1">
            <label>Guest Add to Cart</label>
        </tab>
        <section id="guest_addtocart" translate="label" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>Add to Cart for Guest</label>
            <tab>guest</tab>
            <resource>Guest_AddtoCartHideGuest::system_config</resource>
            <group id="guest" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Disable Add to Cart Button For Guest</label>
                <field id="enable" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Enable</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
             </group>
        </section>
    </system>
</config>

Go to admin and set Disable Add to Cart Button For Guest >> Enable: Yes

Admin path: Stores >> Settings >> Configuration >> GUEST ADD TO CART >> Add to Cart for Guest >> Disable Add to Cart Button For Guest >> Enable: Yes

Let in know if any issue.

Hope it help!

1
0

Hide Add To Cart

app\code\Vendorename\Modulename\etc

di.xml

<?xml version="1.0" encoding="UTF-8"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Model\Product">
        <plugin name="hidecartbutton" type="Vendorename\Modulename\Plugin\Hidecartbutton" sortOrder="1"/>
    </type>
</config>

app\code\Vendorename\Modulename\Plugin

Hidecartbutton.php

<?php

namespace Vendorename\Vendorename\Plugin;

use Magento\Catalog\Model\Product;

class Hidecartbutton
{
    protected $_customerSessionFactory;

    public function __construct(
        \Magento\Customer\Model\SessionFactory $customerSessionFactory
    ) {
        $this->_customerSessionFactory = $customerSessionFactory;
    }
    public function afterIsSaleable(Product $product, $isSaleable)
    {
        // check customer login or not
        $cust_data = $this->_customerSessionFactory->create();
        if($customerSession->isLoggedIn()) {
            return $product->isSalable();
        } else {
            return false;
        }
    }
}

I Hope This Helps You.

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.