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

Divya Sekar
  • 1,473
  • 1
  • 22
  • 78

2 Answers2

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>
Ronak Rathod
  • 6,322
  • 17
  • 42
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.

Kanhaiya lal
  • 1,548
  • 12
  • 24
  • No i need to change a custom text from backend – Divya Sekar May 10 '19 at 06:47
  • @divyasekar i guess you are looking for https://magento.stackexchange.com/a/78473/52244 (how to get store config data in store front) – Kanhaiya lal May 10 '19 at 06:48
  • No, i already get a config value in frontend. my need is how to replace in frontend in product view page – Divya Sekar May 10 '19 at 06: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 – Divya Sekar May 10 '19 at 06:54
  • @divyasekar Need to override the template example for add to cart vendor\magento\module-catalog\view\frontend\templates\product\view\addtocart.phtml to in your theme and via helper you can replace the button text and same step need to do for In stock – Kanhaiya lal May 10 '19 at 06:54
  • @divyasekar i updated my answer as per need in question like fetch data from admin so i create a helper code and call in addtocart.phtml, please refer the code. more help will be appreciated. – Kanhaiya lal May 10 '19 at 07:11
  • is there any chances to override a addtocart.phtml file in custom module from custom template – Divya Sekar May 10 '19 at 07:24
  • @divyasekar see the update answer – Kanhaiya lal May 10 '19 at 08:08
  • its not override – Divya Sekar May 10 '19 at 08:51
  • @divyasekar,1. just want to know which type of product are you working,2. if simple then this code will work, 3. can you please update the code of module.xml file so i can see the sequence, and also update the path of catalog_product_view.xml path of your module in question and also the path of template phtml – Kanhaiya lal May 10 '19 at 08:59
  • i want to both configurable and simple product – Divya Sekar May 10 '19 at 09:04
  • i will post my code wait – Divya Sekar May 10 '19 at 09:04
  • Please check a edit question – Divya Sekar May 10 '19 at 09:14
  • @divyasekar see the updated xml code – Kanhaiya lal May 10 '19 at 09:39
  • not yet nothing happens – Divya Sekar May 10 '19 at 10:37
  • @divyasekar i created a custom module to override a template file in custom module instead of theme. https://github.com/kanhaiya5590/M2-template-override-in-custom-module-instead-of-theme and same i tested for simple and configurable product. if this work you will get output test instead of addtocart button so need to update content for vendor and update as per your need, please don't forgot th share the updated feedback – Kanhaiya lal May 10 '19 at 12:36
  • now only i noted. layout file not called in my module – Divya Sekar May 10 '19 at 12:49
  • I dont know hoe to slove this – Divya Sekar May 10 '19 at 12:50
  • In my xml file i willl mention a wrong path but not showing a error its will run fine but i m giving a wrong block but it runs fine my xml file not called – Divya Sekar May 10 '19 at 12:50
  • @divyasekar might be the magento mode is not set to developer mode so you not get direct error, try to check log or change the mode to developer mode via php bin/magento deploy:mode:set developer, can you confirm shared custom module working – Kanhaiya lal May 10 '19 at 13:00
  • when i create a normal custom block it will appear https://prnt.sc/nmtq2u but i used a above xml layout is not working – Divya Sekar May 10 '19 at 13:15
  • normal custom block were created https://prnt.sc/nmts6t it will appear prnt.sc/nmtq2u but i was try to addtocart it was not called – Divya Sekar May 10 '19 at 13:19
  • 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. – Kanhaiya lal May 10 '19 at 13:32
  • ok i will test it and update you – Divya Sekar May 10 '19 at 13:48