Magento – Set position or order of a new tab on the product detail page in Magento2

magento2product-pagetabs

I am adding an alternate reviews tab to the product detail page, and I have the tab appearing, but it always renders as the first tab in the list, the leftmost. I'd like it to be at the end, after (or in place of) the standard Reviews tab.

<referenceBlock name="product.info.details">
    <block class="My\Module\Block\Reviews" name="my.reviews.tab" as="my_reviews" template="My_Module::reviews.phtml" group="detailed_info">
        <arguments>
            <argument translate="true" name="title" xsi:type="string">Reviews</argument>
        </arguments>
    </block>
</referenceBlock>

I've dug around in the mage-catalog and mage-reviews code, but I can't find an argument I can pass to change this.

Best Answer

You can just change inside details.phtml template file and set as per your requirement tabbing order,

set details.phtml file inside your theme folder,

app/design/frontend/Packagename/themename/Magento_Catalog/templates/product/view/details.phtml

Code for details.phtml file,

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

// @codingStandardsIgnoreFile

?>
<?php if ($detailedInfoGroup = $block->getGroupChildNames('detailed_info', 'getChildHtml')):?>
    <?php $newOrderTabbing = array('product.info.description','reviews.tab','product.attributes'); //custom add ?>
    <div class="product info detailed">
        <?php $layout = $block->getLayout(); ?>
        <div class="product data items" data-mage-init='{"tabs":{"openedState":"active"}}'>
            <?php foreach ($newOrderTabbing 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">
                        <?php /* @escapeNotVerified */ echo $label; ?>
                    </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; ?>

After adding above code now review tab display after details tab. Finally Tab orders are display like Details, Review, More Information.

You can change order by setting in

<?php $newOrderTabbing = array('product.info.description','reviews.tab','product.attributes'); //custom add ?> array in above file.

Thanks.

Related Topic