2

How to replace a custom text (from backend) for add to cart button and stock availability in product view page in my custom module magento 2

enter image description here

Updated

app\code\Cm\Preorder\view\frontend\layout\catalog_product_view.xml

    <?xml version="1.0"?>
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="product.info.type">
            <block class="Magento\Catalog\Block\Product\View\Type\Simple" name="product.info.simple" as="product_type_data" template="Cm_Preorder::product/view/type/default.phtml"/>
        </referenceContainer>
        <referenceBlock name="product.info.addtocart">
            <action method="setTemplate">
                <argument name="template" xsi:type="string">Cm_Preorder::product/view/addtocart.phtml</argument>
            </action>
        </referenceBlock>
        <referenceBlock name="product.info.addtocart.additional">
            <action method="setTemplate">
                <argument name="template" xsi:type="string">Cm_Preorder::product/view/addtocart.phtml</argument>
            </action>
        </referenceBlock>
    </body>
</page>

app\code\Cm\Preorder\view\frontend\templates\product\view\addtocart.phtml

<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

/** @var $block \Magento\Catalog\Block\Product\View */
?>
<?php $_product = $block->getProduct(); ?>
<?php $buttonTitle = __('Pre order'); ?>
<?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><?php /* @escapeNotVerified */ echo __('Qty') ?></span></label>
            <div class="control">
                <input type="number"
                       name="qty"
                       id="qty"
                       maxlength="12"
                       value="<?php /* @escapeNotVerified */ echo $block->getProductDefaultQty() * 1 ?>"
                       title="<?php /* @escapeNotVerified */ echo __('Qty') ?>" class="input-text qty"
                       data-validate="<?php echo $block->escapeHtml(json_encode($block->getQuantityValidators())) ?>"
                       />
            </div>
        </div>
        <?php endif; ?>
        <div class="actions">
            <button type="submit"
                    title="<?php /* @escapeNotVerified */ echo $buttonTitle ?>"
                    class="action primary tocart"
                    id="product-addtocart-button">
                <span><?php /* @escapeNotVerified */ echo $buttonTitle ?></span>
            </button>
            <?php echo $block->getChildHtml('', true) ?>
        </div>


    </div>
</div>
<?php endif; ?>
<script type="text/x-magento-init">
    {
        "#product_addtocart_form": {
            "Magento_Catalog/product/view/validation": {
                "radioCheckboxClosest": ".nested"
            }
        }
    }
</script>
<?php if (!$block->isRedirectToCartEnabled()) : ?>
<script type="text/x-magento-init">
    {
        "#product_addtocart_form": {
            "catalogAddToCart": {
                "bindSubmit": false
            }
        }
    }
</script>
<?php endif; ?>

module.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Cm_Preorder" setup_version="1.0.1" schema_version="1.0.1">
        <sequence>
            <module name="Magento_Catalog"/>
        </sequence>   
    </module>
</config>

any help me to solve this

5
  • Any Solutions for that? Commented May 17, 2019 at 12:46
  • @Kanhaiya lal answer was working Commented May 17, 2019 at 12:58
  • My problem i can't override the addtocart.phtml file in my custom module so can you advise me? Commented May 17, 2019 at 13:00
  • wait i will my code Commented May 17, 2019 at 13:06
  • above code is working perfectly in my custom module Commented May 17, 2019 at 13:10

2 Answers 2

1

In your custom module add a file for example : app/code/[NAME_SPACE]/[MODULE]/i18n/en_US.csv

in this file

"Add to Cart","New word for add to cart"
"In Stock","New word for in stock"

Then clear the cache

php bin/magento cache:clean 

Note if still not came then in your custom module check the sequence of the module and then add Magento_Catalog as dependent module then do setup upgrade.

php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy // (-f add if you are in developer mode)
php bin/magento cache:clean 

As per updated question fetch add to cart text from admin

create a helper app/code/NAMESPACE/MODULE/Helper/Data.php

<?php
namespace NAMESPACE\MODULE\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;

class Data extends AbstractHelper
{
    /**
     * @var ScopeConfigInterface 
     */ 
    private $scopeConfig;

    /**
     * Data constructor.
     * @param ScopeConfigInterface $scopeConfig
     */
    public function __construct(
        ScopeConfigInterface $scopeConfig

    ) {
        $this->scopeConfig = $scopeConfig;
    }

    public function getAddToCartText()
    {
        return $this->scopeConfig->getValue(
            'general/store_information/add_to_cart', // update as per your backend need 
            ScopeInterface::SCOPE_STORE
        );
    }
}

for add to cart text get from admin override the file in your theme app/design/frontend/NAMESPACE/THEME/Magento_Catalog/templates/product/view/addtocart.phtml

$customHelper = $this->helper(NAMESPACE\MODULE\Helper\Data::class);

<?php $buttonTitle = __($customHelper->getAddToCartText()); ?>

same process for in-stock, same helper add new function and call in respective area.


Override a template inside a custom module instead of theme

Example of overriding addtocart.phtml for simple product

so in custom module create a file app/code/NAMESPACE/MODULE/view/frontend/layout/catalog_product_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">
            <body>
                <referenceBlock  name="product.info.addtocart">
                    <action method="setTemplate">
                        <argument name="template" xsi:type="string">NAMESPACE_MODULE::product/view/addtocart.phtml</argument>
                    </action>
                </referenceBlock>

               <referenceBlock name="product.info.addtocart.additional">
                    <action method="setTemplate">
                        <argument name="template" xsi:type="string">NAMESPACE_MODULE::product/view/addtocart.phtml</argument>
                    </action>
            </referenceBlock>
         </body>
        </page>

product.info.addtocart is used for simple products, and product.info.addtocart.additional is used for configurable products.

then copy the addtocart.phtml file vendor to app/code/NAMESPACE/MODULE/view/frontend/templates/product/view/addtocart.phtml and do some necessary change (as per current requirement add a custom helper and update the button text).

Note : it is mandatory to add module sequence (<module name="Magento_Catalog"/>) is module.xml file.

hope you will get your expected output.

24
  • No i need to change a custom text from backend Commented May 10, 2019 at 6:47
  • @divyasekar i guess you are looking for magento.stackexchange.com/a/78473/52244 (how to get store config data in store front) Commented May 10, 2019 at 6:48
  • No, i already get a config value in frontend. my need is how to replace in frontend in product view page Commented May 10, 2019 at 6:52
  • I m confused with whether i need to override a product view page or customize a stock availablity and add to cart button in frontend Commented May 10, 2019 at 6:54
  • 1
    @divyasekar add some dummy text in addtocart.phtml file at the top and see, might be some condition not allow to execute the code. Commented May 10, 2019 at 13:32
2

Add To Cart Button Text Change :-

Override This File In Your Module :-

vendor/magento/module-catalog/view/frontend/templates/product/view/addtocart.phtml

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

You Can get Your Config Value Using ScopeConfigInterface And Change $buttonTitle with your Value.

"In STOCK" Text Change :-

Override This File :- vendor/magento/module-catalog/view/frontend/templates/product/view/type/default.phtml

    <div class="stock available" title="<?= /* @escapeNotVerified */ __('Availability') ?>">
            <span><?= /* @escapeNotVerified */ __('In stock') ?></span>
     </div>
10
  • No i need to change a text from backend Commented May 10, 2019 at 6:38
  • using backend like to create configration for chnge text ?? Commented May 10, 2019 at 6:43
  • i was already create a configuration in backend and i had doubt in how to bring a custom text in frontend Commented May 10, 2019 at 6:45
  • check my answer Commented May 10, 2019 at 6:54
  • Its looks fine to add to cart text replacement and i need stock availabity also Commented May 10, 2019 at 6:56

Your Answer

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

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