7

I'm working on a credit card (CC) payment module.
I see that Magento 2 already offers a template for CC payments but it contains only the form needed for the payment.
It does not have the radio button that I can check during checkout.
So if I create my payment method renderer like this.

define(
    [
        'Magento_Payment/js/view/payment/cc-form'
    ],
    function (Component) {
        'use strict';

        return Component.extend({
            defaults: {
               template: 'Magento_Payment/payment/cc-form'
            },
            isActive: function() {
                return true;
            },
            getCode: function() {
                return 'payment_code_here';
            }
        });
    }
);

my payment section in the checkout would look like this (Check/money order is an other payment method):

And I want it to look like this:

I know that I can create my own template and define the method renderer like this:

define(
    [
        'Magento_Payment/js/view/payment/cc-form'
    ],
    function (Component) {
        'use strict';

        return Component.extend({
            defaults: {
                template: '<Vendor>_<Module>/payment/form'
            },
            isActive: function() {
                return true;
            },
            getCode: function() {
                return 'payment_code_here';
            }
        });
    }
);

And in that template add the markup for the radio button and just copy/paste the default cc-form template where I need it, but I'm trying to avoid that.
Is there a way to include an other template inside my template?
This may be more of a KO question than Magento.

To explain it better...

My custom template looks like this:

<my custom code here />
<code copied from cc-form /> 
<some other custom code here />

I want to replace <code copied from cc-form /> with an include of some kind that will fetch the content from cc-form.

  • Marius, How can i use Magento 2 template for CC payments in my custom payment module.? I wanted to show a CC form on checkout. – Charlie Aug 4 '17 at 7:48
8
+100

I'm not sure I understand the question fully, do you mean you want to call a KO HTML template from another template/module?

If so the I believe the following markup works (unable to test currently):

<!-- ko template: "Package_Namespace/template-name" --><!-- /ko -->

Or alternatively you can define the template in an object and pass a property like so:

<!-- ko template: propertyExample --><!-- /ko -->
  • Yes, I want to include in my template an other template. I will try it and come back with the results. – Marius Mar 23 '17 at 14:11
  • Cool, I've just added an alternative way using properties. – Ben Crook Mar 23 '17 at 14:13
  • I can't believe it was so easy. Thanks. I will give you the fake internet points in about 23h, when I'ma allowed to do it. – Marius Mar 23 '17 at 14:20
  • 1
    The KO implementation is slowly growing on me, at first it was terrifying. Thanks, I love me some fake internet points. – Ben Crook Mar 23 '17 at 14:27
  • @BenCrook helpful info. – Khoa TruongDinh Mar 23 '17 at 15:23

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.