4

What the best way to change text add to cart only for not registered users?

  1. Documentation recommend change dictionary file. https://devdocs.magento.com/guides/v2.3/frontend-dev-guide/translations/translate_practice.html but this solution works for all users
  2. Create some plugin/observer for Magento\Framework\Phrase\Renderer\Translate.
1

There are many ways to achieve this, but I would recommend it by overriding the template file.

To do so, just copy the file /vendor/magento/module-catalog/view/frontend/templates/product/list.phtml and put it under app/design/frontend/{theme-package}/{theme}/Magento/Catalog/templates/product folder.

Now you can programmatically check whether a customer is logged into the website or not, and based on that you can change the value of __('Add to Cart') to whatever you want.

Since the solution needs some custom code, it is not possible by simply writing the translation in the translation files.

The above solution is the simplest one and it also does not break any Magento standard.

1
  • this might not work with FPC enabled – igrossiter Feb 2 '19 at 12:50
1

You have to override js file from path and all user for translate text this file

vendor/magento/module-catalog/view/frontend/web/js/catalog-add-to-cart.js  

To

app/design/frontend/YourTheme/Packadge/Magento_Catalog/web/js/catalog-add-to-cart.js

You have to changes text which you want to from this file.

0

I think the simple solution to change the "Add to cart" only for not registred users is to do something like this:

Not registred means not logged in

app/design/frontend/{Vendor}{themename}/Magento_Catalog/templates/product/view/addtocart.phtml

/** @var $block \Magento\Catalog\Block\Product\View */
?>
<?php $_product = $block->getProduct(); ?>

/* ***** We do our stuff right here ***** */
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('Magento\Customer\Model\Session');
if($customerSession->isLoggedIn()) {
    $buttonTitle = __('Add to Cart');
} else {
    $buttonTitle = __('Text for not registred user');
}

...

In the case you want to get the registred users:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
$website_id = $storeManager->getStore()->getWebsiteId();
$customer = $objectManager->get('Magento\Customer\Model\Customer');
$customer->setWebsiteId($website_id);
$customer->load('CUSTOMER_ID');

if ( $customer->getId() ) {
      // the customer exist
} else {
      // does't exist
}

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.