Magento – Magento 2, display gift options in checkout page

frontendmagento2PHP

In magento 2, when you switch on the gift option from the backend. It gets displayed on the Cart page.

I need to display this on cart page and checkout page as well, under cart items.

How can i achieve that.

I have tried editing the xml for checkout_index_index adding block for gift in itemsAfter but to no avail.

Best Answer

Try this method

Step1: app/code/Vendor/Module/view/frontend/layout/checkout_index_index.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="checkout" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <referenceBlock name="checkout.root">
                <arguments>
                    <argument name="jsLayout" xsi:type="array">
                        <item name="components" xsi:type="array">
                            <item name="checkout" xsi:type="array">
                                <item name="children" xsi:type="array">
                                    <item name="sidebar" xsi:type="array">
                                        <item name="children" xsi:type="array">
                                            <item name="summary" xsi:type="array">
                                                <item name="children" xsi:type="array">
                                                    <item name="itemsAfter" xsi:type="array">
                                                        <item name="children" xsi:type="array">
                                                            <item name="gift_message" xsi:type="array">
                                                                <item name="component" xsi:type="string">Magento_GiftMessage/js/view/gift-message</item>
                                                                <item name="displayArea" xsi:type="string">item_message</item>
                                                                <item name="config" xsi:type="array">
                                                                    <item name="template" xsi:type="string">Magento_GiftMessage/gift-message</item>
                                                                    <item name="formTemplate" xsi:type="string">Magento_GiftMessage/gift-message-form</item>
                                                                </item>
                                                            </item>
                                                        </item>
                                                    </item>
                                                </item>
                                            </item>
                                        </item>
                                    </item>
                                </item>
                            </item>
                        </item>
                    </argument>
                </arguments>
            </referenceBlock>
            <block class="Magento\GiftMessage\Block\Cart\GiftOptions" name="checkout.order.actions.gift_options" template="Vendor_Module::checkout/gift_options.phtml" cacheable="false" />
        </referenceContainer>
    </body>
</page>

Step2: app/code/Vendor/Module/view/frontend/templates/checkout/gift_options.phtml

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
?>
<script>
    window.giftOptionsConfig = <?= /* @escapeNotVerified */ $block->getGiftOptionsConfigJson() ?>;
</script>
Related Topic