0

In Magento 1, there is custom message based on product id, added in cart as below:-

enter image description here

Same I want to show in Magento 2.3.

For static message I follow this article.

Please help me how to add condition based on product ids in checkout .

2

Try the following way:

app/code/SR/MagentoCommunity/view/frontend/layout/checkout_index_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="checkout" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <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="steps" xsi:type="array">
                                    <item name="children" xsi:type="array">
                                        <item name="billing-step" xsi:type="array">
                                            <item name="children" xsi:type="array">
                                                <item name="payment" xsi:type="array">
                                                    <item name="children" xsi:type="array">
                                                        <item name="beforeMethods" xsi:type="array">
                                                            <item name="children" xsi:type="array">
                                                                <item name="custom_block" xsi:type="array">
                                                                    <item name="component" xsi:type="string">SR_MagentoCommunity/js/view/custom-block</item>
                                                                </item>
                                                            </item>
                                                        </item>
                                                    </item>
                                                </item>
                                            </item>
                                        </item>
                                    </item>
                                </item>
                            </item>
                        </item>
                    </item>
                </argument>
            </arguments>
        </referenceBlock>
    </body>
</page>

app/code/SR/MagentoCommunity/view/frontend/web/js/view/custom-block.js

define([
    'jquery',
    'ko',
    'uiComponent',
    'Magento_Checkout/js/model/quote'
], function ($, ko, Component, quote) {
    'use strict';

    return Component.extend({
        defaults: {
            template: 'SR_MagentoCommunity/custom-block'
        },
        isVisibleFirstBlock:  ko.observable(false),
        isVisibleSecondBlock: ko.observable(false),
        initialize: function () {
            this._super();
            var items = quote.getItems();
            var hasProductInCart = 0;
            $.each(items, function(index, item) {
                if (item.product_id == 14) {
                    hasProductInCart = 1;
                }
            }.bind(this));

            var regionId = 0;
            var self = this;
            quote.shippingAddress.subscribe(function (address) {
                regionId = address.regionId;
                if (hasProductInCart && (regionId == 12)) {
                    self.isVisibleFirstBlock(true);
                } else {
                    self.isVisibleSecondBlock(true);
                }
            });

            return this;
        }
    });
});

app/code/SR/MagentoCommunity/view/frontend/web/template/custom-block.html

<!-- ko if: (isVisibleFirstBlock()) -->
    <div>
        <span data-bind="i18n: 'Custom Message 1'"></span>
    </div>
<!-- /ko -->

<!-- ko if: (isVisibleSecondBlock()) -->
    <div>
        <span data-bind="i18n: 'Custom Message 2'"></span>
    </div>
<!-- /ko -->
  • I also want to check region id with product id, how to get region id here. Thanks – Purushotam Sharma Apr 4 at 2:35
  • 1
    Check updated answer – Sohel Rana Apr 4 at 13:01
  • Accept this answer, that will help other – Sohel Rana Apr 4 at 16:01
  • Already upvote for your answer – Purushotam Sharma Apr 4 at 16:13

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.