Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top