1

I am using one custom payment module and i want to do code in html file. Path is

app/code/Custom/Payment/view/frontend/web/template/payment/custompayment.html

and my code is

$om = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $om->get('Magento\Customer\Model\Session');
if($customerSession->isLoggedIn()){
  -----------code ---------
}

js file which i trying to do but not working

define(
[      
    'jquery',
    'Magento_Customer/js/customer-data',
    'Magento_Checkout/js/view/payment/default'
],
function (Component) {
    'use strict';

    var getCustomerInfo = function () {
        var customer = customerData.get('customer');

        return customer();
    };

    var isLoggedIn = function (customerInfo) {
        customerInfo = customerInfo || getCustomerInfo();

        return customerInfo && customerInfo.firstname;
    };

    return Component.extend({
        defaults: {
            template: 'XJ_Payment/payment/custompayment'
        },

        /** Returns send check to info */
        getMailingAddress: function() {
            return window.checkoutConfig.payment.checkmo.mailingAddress;
        },


    });
}
);

but i am not able to get result. i know this code is perfect work if i put on controller or model file. but still i am facing the issue which is output is after -> on second line all the code are print on frontend. i can't understand what's going on.

Can you please suggest me how to resolve the issue ?

Khoa TruongDinh
  • 32,054
  • 11
  • 88
  • 155
Kartik Asodariya
  • 471
  • 1
  • 8
  • 23

1 Answers1

1

By default Magento, impossible to add PHP code to html template. We can get the customer from local storage. We can read more here: How to check that customer is logged in on frontend in Magento 2.1?

UPDATE:

In the return js, it should be:

return Component.extend({
    defaults: {
        template: 'XJ_Payment/payment/xjpayment'
    },

    var customerInfo = getCustomerInfo();

    /** Returns send check to info */
    getMailingAddress: function() {
        return window.checkoutConfig.payment.checkmo.mailingAddress;
    },
    isLoggedIn : isLoggedIn(customerInfo)
});

In your html template:

......
<!-- ko if: isLoggedIn() -->
    <span>Customer is logged</span>
<!-- /ko -->
......

Remember to delete static content and run again.

Khoa TruongDinh
  • 32,054
  • 11
  • 88
  • 155