I need the product URL for each product in the checkouts cart.
Therefore I want to edit vendor/magento/module-checkout/view/web/templates/summary/item/details.html
and add my custom method there, lets call it myMethod
.
This is how my code looks like right <strong class="product-item-name" data-bind="text: $parent.name, myMethod:{}"></strong>
.
However, in a console, i have the log that there's no function called myMethod
. Logic!
How can I create myMethod
in Order to let it work? Where do I need to inject my JS?
What do I want to do? For legal reasons, I need the URL of the product. I want KO to do my request by calling an ajax file that returns the URL of the product and then links it. The final code in details.html
will look like this:
<a class="product-item-name" data-bind="text: $parent.name, myMethod:{}"></a>
In onepage.phtml
I've added the following to test, but it does not work:
<script type="text/javascript">
require([
'jquery',
'ko'
], function($, ko) {
ko.bindingHandlers.myMethod = {
init: function(element, valueAccessor, allBindingsAccessor, viewModel) {
jQuery(this).css("background-color","red");
}
};
});
</script>
How can I achieve what I what?
Best Answer
For Magento to apply the bindings you need to add your Knockout JS using the UI Components like so:
Inside the PHTML Template file
Pay extra attention to
example-scope
as it needs to match the scope of the markup.Component links to your JS file, and template links to your HTML (or PHTML) file that contains the data-binds (and scope).
Inside your JS file