<?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="Magenticians_Mymodule" setup_version="1.0.0"></module> </config>Create registration.php file in app/code/Magenticians/Mymodule and add the following code in it:
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Magenticians_Mymodule', __DIR__ );
<?php namespace Magenticians\Mymodule\Setup; use Magento\Eav\Setup\EavSetup; use Magento\Eav\Setup\EavSetupFactory; use Magento\Framework\Setup\InstallDataInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Catalog\Setup\CategorySetupFactory; use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory; class InstallData implements InstallDataInterface { private $eavSetupFactory; private $attributeSetFactory; private $attributeSet; private $categorySetupFactory; public function __construct(EavSetupFactory $eavSetupFactory, AttributeSetFactory $attributeSetFactory, CategorySetupFactory $categorySetupFactory ) { $this->eavSetupFactory = $eavSetupFactory; $this->attributeSetFactory = $attributeSetFactory; $this->categorySetupFactory = $categorySetupFactory; } public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { $setup->startSetup(); // CREATE ATTRIBUTE SET $categorySetup = $this->categorySetupFactory->create(['setup' => $setup]); $attributeSet = $this->attributeSetFactory->create(); $entityTypeId = $categorySetup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY); $attributeSetId = $categorySetup->getDefaultAttributeSetId($entityTypeId); $data = [ 'attribute_set_name' => 'DisableAddToCart', 'entity_type_id' => $entityTypeId, 'sort_order' => 200, ]; $attributeSet->setData($data); $attributeSet->validate(); $attributeSet->save(); $attributeSet->initFromSkeleton($attributeSetId); $attributeSet->save(); // CREATE PRODUCT ATTRIBUTE $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]); $eavSetup->addAttribute( \Magento\Catalog\Model\Product::ENTITY, 'text_add_to_cart', [ 'type' => 'varchar', 'label' => 'Add to Cart Text', 'backend' => '', 'input' => 'text', 'wysiwyg_enabled' => false, 'source' => '', 'required' => false, 'sort_order' => 5, 'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_STORE, 'used_in_product_listing' => true, 'visible_on_front' => fasle, 'attribute_set' => 'DisableAddToCart', ] ); $setup->endSetup(); } } ?>In the above code I have created the attribute set ‘DisableAddToCart’ and attributed it with code ‘text_add_to_cart’ and label ‘Add to Cart Text’. I have also assigned this attribute in the created attribute set.
<?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> <referenceBlock name="product.info.addtocart"> <action method="setTemplate"> <argument name="template" xsi:type="string">Magenticians_Mymodule::catalog/product/view/addtocart.phtml</argument> </action> </referenceBlock> </body> </page>The purpose of above file is to override the addtocart file with our newly created addtocart file (app/code/Magenticians/Mymodule/view/frontend/templates/catalog/product/view/ addtocart.phtml)
<?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $product = $objectManager->get('Magento\Framework\Registry')->registry('current_product'); $attributeSet = $objectManager->create('Magento\Eav\Api\AttributeSetRepositoryInterface'); $attributeSetRepository = $attributeSet->get($product->getAttributeSetId()); $attribute_set_name = $attributeSetRepository->getAttributeSetName(); ?> <?php if ($attribute_set_name=="DisableAddToCart"):?> <div class="box-tocart"> <div class="fieldset"> <div class="actions disablebtn"> <button type="submit" class="action primary tocart" id="product-addtocart-button" disabled> <?php echo $_product->getData('text_add_to_cart'); ?> </button> <?= $block->getChildHtml('', true) ?> </div> </div> </div> <?php else : ?>And at the end of the addtocart file code, just add <?php endif; ?> . Your final addtocart.phtml file will look like this:
<?php /** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ // @codingStandardsIgnoreFile /** @var $block \Magento\Catalog\Block\Product\View */?> <?php $_product = $block->getProduct(); ?> <?php $buttonTitle = __('Add to Cart'); ?> <?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $product = $objectManager->get('Magento\Framework\Registry')->registry('current_product'); $attributeSet = $objectManager->create('Magento\Eav\Api\AttributeSetRepositoryInterface'); $attributeSetRepository = $attributeSet->get($product->getAttributeSetId()); $attribute_set_name = $attributeSetRepository->getAttributeSetName(); ?> <?php if ($attribute_set_name=="DisableAddToCart"):?> <div class="box-tocart"> <div class="fieldset"> <div class="actions disablebtn"> <button type="submit" class="action primary tocart" id="product-addtocart-button" disabled> <?php echo $_product->getData('text_add_to_cart'); ?> </button> <?= $block->getChildHtml('', true) ?> </div> </div> </div> <?php else : ?> <?php if ($_product->isSaleable()): ?> <div class="box-tocart"> <div class="fieldset"> <?php if ($block->shouldRenderQuantity()): ?> <div class="field qty"> <label class="label" for="qty"><span><?= /* @escapeNotVerified */ __('Qty') ?></span></label> <div class="control"> <input type="number" name="qty" id="qty" value="<?= /* @escapeNotVerified */ $block->getProductDefaultQty() * 1 ?>" title="<?= /* @escapeNotVerified */ __('Qty') ?>" class="input-text qty" data-validate="<?= $block->escapeHtml(json_encode($block->getQuantityValidators())) ?>" /> </div> </div> <?php endif; ?> <div class="actions"> <button type="submit" title="<?= /* @escapeNotVerified */ $buttonTitle ?>" class="action primary tocart" id="product-addtocart-button"> <span><?= /* @escapeNotVerified */ $buttonTitle ?></span> </button> <?= $block->getChildHtml('', true) ?> </div> </div> </div> <?php endif; ?> <?php if ($block->isRedirectToCartEnabled()) : ?> <script type="text/x-magento-init"> { "#product_addtocart_form": { "Magento_Catalog/product/view/validation": { "radioCheckboxClosest": ".nested" } } } </script> <?php else : ?> <script type="text/x-magento-init"> { "#product_addtocart_form": { "Magento_Catalog/js/validate-product": {} } } </script> <?php endif; ?> <?php endif; ?>Now it’s time to create a CSS file. Go to app/code/Magenticians/Mymodule/view/frontend/web/css and create disableaddtocart.css file. Add the following code in it:
.disablebtn { cursor: not-allowed; }Now just update the catalog_product_view.xml by going to app/code/Magenticians/Mymodule/view/frontend/layout/catalog_product_view.xml in order to integrate CSS file. Final catalog_product_view.xml will look like this:
<?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="Magenticians_Mymodule::css/disableaddtocart.css"/> </head> <body> <referenceBlock name="product.info.addtocart"> <action method="setTemplate"> <argument name="template" xsi:type="string">Magenticians_Mymodule::catalog/product/view/addtocart.phtml</argument> </action> </referenceBlock> </body> </page>
php bin/magento module:enable Magenticians_Mymodule php bin/magento setup:upgrade php bin/magento setup:di:compile php bin/magento setup:static-content:deploy php bin/magento cache:clean php bin/magento cache:flushNow open the admin panel of your store and go to STORES → Attribute Set. You will see new attribute DisableAddToCart:
Make Unlimited Customizations to Your Ecommerce Store.
With Cloudways, Deploy Your Magento Store in Minutes and Add as many Customizations to it as you want.
Your greatest possible competitive advantage can be your clients and the interactions they have with… Read More
Digital marketing KPIs are measurable values that marketing teams use to track progress toward desired… Read More
In today's digital age, fraud poses a significant threat to businesses of all sizes. As… Read More
Financial crimes continue to evolve and proliferate in our increasingly digital, global economy. From complex… Read More
In the highly competitive modern workplace, trust, and employee loyalty are crucial factors for long-term… Read More
In the ever-evolving world of small business developing and implementing effective marketing strategies is critical to… Read More
View Comments
Hi just wanna say thanks for your website easy to use :)
but i have a slight issue when doing the de compile i get error with this file InstallData.php parse error: synax error, unexpected ' privarte' (t_string) , EXPECTING FUNCTION (T_FUNCTION) IN InstallData.php LINE 15