0

How to rename the product detail tab title, and display as the last tab? This is what i added, but didn't make any effect. the file is at the following location:

/app/design/frontend/Infortis/ultimo/Amasty_Faq/layout/catalog_product_view.xml

I added:

<action method="setTitle">
   <argument name="title" xsi:type="string">Q&gt;A</argument>
</action>

and

<move element="amasty_faq_product_tab" destination="product.info.details" after="-" />

to the original xml

  <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <head>
            <script src="Amasty_Faq::js/section/product-url.js"/>
        </head>
        <body>
            <referenceBlock name="product.info.details">
                <block class="Amasty\Faq\Block\Catalog\Product\Tab" name="amasty_faq_product_tab" ifconfig="amastyfaq/product_page/show_tab" as="amfaq_product" template="Amasty_Faq::product/questions_tab.phtml" group="detailed_info">
                    <action method="setTitle">
                        <argument name="title" xsi:type="string">Q&gt;A</argument>
                    </action>                   
                    <block class="Amasty\Faq\Block\Lists\QuestionsList" name="amasty_faq_questions" template="Amasty_Faq::lists/questions.phtml" after="-">
                        <block class="Amasty\Faq\Block\Rating\Rating" ifconfig="amastyfaq/rating/enabled" name="amasty_faq_rating" template="Amasty_Faq::rating/rating.phtml">
                            <block class="Magento\Framework\View\Element\Template" name="amasty_faq_rating_item" template="Amasty_Faq::rating/rating_item.phtml"/>
                        </block>
                        <block class="Amasty\Faq\Block\Lists\Pager" name="amasty_faq_pager"/>
                    </block>
                    <block class="Amasty\Faq\Block\Forms\AskQuestion" ifconfig="amastyfaq/product_page/show_link" name="amasty_faq_ask_question_form" template="Amasty_Faq::forms/askquestion.phtml"/>
                </block>
            </referenceBlock>
            <move element="amasty_faq_product_tab" destination="product.info.details" after="-" />
        </body>
    </page>

enter image description here

update: I found out the tab title is set in this file. how do i override this block .php?

/vendor/amasty/module-faq-product-questions/Block/Catalog/Product/Tab.php

I tried to extends the Tab.php by creating a module, but didnt' work

/app/code/Kw/Renametab/Block/Catalog/Product/Tab.php

<?php
    /**
     * @author Amasty Team
     * @copyright Copyright (c) 2019 Amasty (https://www.amasty.com)
     * @package Amasty_Faq
     */


    namespace Kw\Renametab\Block\Catalog\Product;
    use Magento\Framework\View\Element\Template;

    class Tab extends \Amasty\Faq\Block\Catalog\Product\Tab
    {
    /**
     * Render block HTML
     *
     * @return string
     */
    protected function _toHtml()
    {
        $count = $this->getChildBlock('amasty_faq_questions')->getCollection()->count();

        $this->setTitle(__('Q&A') . (($count) ? ' (' . $count .')' : ''));

        return parent::_toHtml();
    }

}

Amasty_Faq::product/questions_tab.phtml

looks like this

<?php
/**
 * @var \Amasty\Faq\Block\Catalog\Product\Tab $block
 */
?>
<div class="amfaq-product-tab">

    <div class="block-title">
        <!--<strong><?//= $block->escapeHtml(__('Question')); ?></strong>-->
    </div>

    <?php
    echo $block->getChildHtml('amasty_faq_questions');
    if ($block->showAskQuestionForm()) {
        echo $block->getChildHtml('amasty_faq_ask_question_form');
    }
    ?>
</div>

The

/vendor/amasty/module-faq-product-questions/Block/Catalog/Product/Tab.php

looks like this, i'm not sure how to override the function, so i edit the original file for the mean time

<?php
/**
 * @author Amasty Team
 * @copyright Copyright (c) 2019 Amasty (https://www.amasty.com)
 * @package Amasty_Faq
 */


namespace Amasty\Faq\Block\Catalog\Product;

use Amasty\Faq\Model\ConfigProvider;
use Magento\Framework\View\Element\Template;

class Tab extends \Amasty\Faq\Block\AbstractBlock implements \Magento\Framework\DataObject\IdentityInterface
{
    /**
     * @var ConfigProvider
     */
    private $configProvider;

    /**
     * Tab constructor.
     *
     * @param Template\Context                 $context
     * @param ConfigProvider                   $configProvider
     * @param array                            $data
     */
    public function __construct(
        Template\Context $context,
        ConfigProvider $configProvider,
        array $data = []
    ) {
        parent::__construct($context, $data);
        $this->configProvider = $configProvider;
    }

    /**
     * @return int
     */
    public function getShortAnswerBehavior()
    {
        return (int)$this->configProvider->getProductPageShortAnswerBehavior();
    }

    /**
     * @return bool
     */
    public function showAskQuestionForm()
    {
        return $this->configProvider->isShowAskQuestionOnProductPage();
    }

    /**
     * Render block HTML
     *
     * @return string
     */
    protected function _toHtml()
    {
        $count = $this->getChildBlock('amasty_faq_questions')->getCollection()->count();

        $this->setTitle(__('Product Questions') . (($count) ? ' (' . $count .')' : ''));

        return parent::_toHtml();
    }

    /**
     * Return identifiers for produced content
     *
     * @return array
     */
    public function getIdentities()
    {
        return [\Amasty\Faq\Model\ResourceModel\Question\Collection::CACHE_TAG];
    }
}

/app/design/frontend/Infortis/ultimo/Magento_Catalog/templates/product/view/details.phtml

look like this

<?php
// @codingStandardsIgnoreFile
?>
<?php
    $theme = $this->helper('Infortis\Base\Helper\Data');
    $innerContainerClasses = '';
    $jsScriptInit = '';

    $mode = $theme->getCfg('product_page/tabs');
    $openedTab = $theme->getCfg('product_page/opened_tab');
    $jsOpenedTab = '';
    if ($openedTab !== null)
    {
        $jsOpenedTab = ', "active": ' . $openedTab;
    }
    else
    {
        $jsOpenedTab = ', "active": null';
    }

    switch ($mode)
    {
        case 1:
            $innerContainerClasses = 'product data items ' . $theme->getCfg('product_page/tabs_style') . ' show-tabs';
            $jsScriptInit = '{"tabs": {"openedState": "active", "collapsible": true' . $jsOpenedTab . '}}';
            break;

        case 2:
            $innerContainerClasses = 'product data items ' . $theme->getCfg('product_page/tabs_style');
            $jsScriptInit = '{"tabs": {"openedState": "active", "collapsible": true' . $jsOpenedTab . '}}';
            break;

        default:
            $innerContainerClasses = 'product data items style1 stacked-tabs';
            $jsScriptInit = '{"tabs": {"openedState": "active", "collapsible": true}}';
            break;
    }
?>
<?php if ($detailedInfoGroup = $block->getGroupChildNames('detailed_info', 'getChildHtml')):?>

<?php//START CODE FOR LAST TAB
foreach ($detailedInfoGroup as $key => $name){
    if($name == 'amasty_faq_product_tab'){
        unset($detailedInfoGroup[$key]);
        array_push($detailedInfoGroup, $name);
    }
}// END CODE FOR LAST TAB
?>

    <div class="product info detailed collateral-container">
        <?php $layout = $block->getLayout(); ?>
        <div class="<?php echo $innerContainerClasses; ?>" <?php if ($jsScriptInit): ?>data-mage-init='<?php echo $jsScriptInit; ?>'<?php endif; ?>>
            <?php foreach ($detailedInfoGroup as $name):?>
                <?php
                    $html = $layout->renderElement($name);
                    if (!trim($html)) {
                        continue;
                    }
                    $alias = $layout->getElementAlias($name);
                    $label = $block->getChildData($alias, 'title');
                ?>
                <div class="data item title"
                    aria-labeledby="tab-label-<?php /* @escapeNotVerified */ echo $alias; ?>-title"
                    data-role="collapsible" id="tab-label-<?php /* @escapeNotVerified */ echo $alias; ?>">
                    <a class="data switch"
                        tabindex="-1"
                        data-toggle="switch"
                        href="#<?php /* @escapeNotVerified */ echo $alias; ?>"
                        id="tab-label-<?php /* @escapeNotVerified */ echo $alias; ?>-title">
                            <strong><?php /* @escapeNotVerified */ echo $label; ?></strong>
                    </a>
                </div>
                <div class="data item content" id="<?php /* @escapeNotVerified */ echo $alias; ?>" data-role="content">
                    <?php /* @escapeNotVerified */ echo $html; ?>
                </div>
            <?php endforeach;?>
        </div>
    </div>
<?php endif; ?>
3
  • Can you provide the content of Amasty_Faq::product/questions_tab.phtml? and '/public_html/vendor/amasty/module-faq-product-questions/Block/Catalog/Product/Tab.php' Mar 8, 2019 at 10:55
  • updated the question. for rename the title, i tried extend the _toHtml() function by create a module, but didn't work, so i just edit the original Tab.php in the vendor file for now. Mar 8, 2019 at 17:26
  • I added my answer. Feel free to contact me if you have any further questions. Mar 11, 2019 at 9:05

2 Answers 2

1

Overriding the original file isn't a great idea. If you once decide to update your module, all your changes would be overwritten anyway. In my opinion, this should be a setting in your adminpanel. There might be a few solutions to this.

Change the tab title

Solution 1

You can create a module and make your own Block class which extends /vendor/amasty/module-faq-product-questions/Block/Catalog/Product/Tab.php. Since you already did this, you just need to copy the catalog_product_view.xmlfrom the Amasty_Faq module and change the original block class to yours. It should look like this:

catalog_product_view.xml

  <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.details">
            <block class="Kw\Renametab\Block\Catalog\Product\Tab" name="amasty_faq_product_tab" ifconfig="amastyfaq/product_page/show_tab" as="amfaq_product" template="Amasty_Faq::product/questions_tab.phtml" group="detailed_info">
                <action method="setTitle">
                    <argument name="title" xsi:type="string">Q&gt;A</argument>
                </action>                   
                <block class="Amasty\Faq\Block\Lists\QuestionsList" name="amasty_faq_questions" template="Amasty_Faq::lists/questions.phtml" after="-">
                    <block class="Amasty\Faq\Block\Rating\Rating" ifconfig="amastyfaq/rating/enabled" name="amasty_faq_rating" template="Amasty_Faq::rating/rating.phtml">
                        <block class="Magento\Framework\View\Element\Template" name="amasty_faq_rating_item" template="Amasty_Faq::rating/rating_item.phtml"/>
                    </block>
                    <block class="Amasty\Faq\Block\Lists\Pager" name="amasty_faq_pager"/>
                </block>
                <block class="Amasty\Faq\Block\Forms\AskQuestion" ifconfig="amastyfaq/product_page/show_link" name="amasty_faq_ask_question_form" template="Amasty_Faq::forms/askquestion.phtml"/>
            </block>
        </referenceBlock>
        <move element="amasty_faq_product_tab" destination="product.info.details" after="-" />
    </body>
</page>

Also, make sure that your module depends on the Amasty_Faq module.

Solution 2

You can always create a preference in your di.xml, but try to avoid this as many times as possible. This solution should be your last and worst case scenario.

Kw\Renametab\etc\frontend\di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Amasty\Faq\Block\Catalog\Product\Tab" type="Kw\Renametab\Block\Catalog\Product\Tab"/>
</config>

Both of these solutions should be temporary. I highly recommend you to make a request to Amasty and ask a setting for this.

Display as the last tab

The ideal solution would be to move the block to the end of the referenceBlock, but I tried this myself and I couldn't make it work. I did make it work by adding the follow code in Magento_Catalog/templates/product/view/details.phtml in your theme.

$detailedInfoGroup = $block->getGroupChildNames('detailed_info', 'getChildHtml');

//START CODE FOR LAST TAB
foreach ($detailedInfoGroup as $key => $name){
    if($name == 'name.of.your.block.to.display.as.last'){
        unset($detailedInfoGroup[$key]);
        array_push($detailedInfoGroup, $name);
    }
}
// END CODE FOR LAST TAB
?>
<?php if ($detailedInfoGroup):?>
    <div class="product info detailed">
           ...
    </div>
<?php endif; ?>
3
  • Thanks for the in-depth answer. I'm trying these out. For it to display as the last tab, should the name.of.your.block.to.display.as.last be "amasty_faq_product_tab" in this case? doesn't seem to work for me..i added my details.phtml in the question. Mar 12, 2019 at 17:04
  • Thank you. Your code works fine. I edited the wrong details.phtml file, that's why it didn't work. It's at this location app/design/frontend/Infortis/base/Infortis_Base/templates/product/view/details.phtml for my theme, instead of the app/design/frontend/Infortis/base/Magento_Catalog/templates/product/view/details.phtml Mar 12, 2019 at 23:41
  • I'm glad you made it work :) Mar 13, 2019 at 10:08
1

In catalog_product_view.xml Change This :-

    <arguments>
         <argument translate="true" name="title" xsi:type="string">FAQ</argument>
         <argument name="priority" xsi:type="string">sort_order_number</argument>
    </arguments> 
1
  • update the catalog_product_view.xml with this and clear cache, but didn't change anything. please advise. Mar 8, 2019 at 17:55

Your Answer

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

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