I want to pass the data from model file to knockout.js in Magento2.
My Code:
Model file:
<?php
namespace Amy\MyPackage\Model\Total\Quote;
/**
* Class Custom
* @package Mageplaza\HelloWorld\Model\Total\Quote
*/
class Package extends \Magento\Quote\Model\Quote\Address\Total\AbstractTotal
{
/**
* @var \Magento\Framework\Pricing\PriceCurrencyInterface
*/
protected $_priceCurrency;
/**
* Custom constructor.
* @param \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
*/
public function __construct(
\Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
){
$this->_priceCurrency = $priceCurrency;
}
/**
* @param \Magento\Quote\Model\Quote $quote
* @param \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment
* @param \Magento\Quote\Model\Quote\Address\Total $total
* @return $this|bool
*/
public function collect(
\Magento\Quote\Model\Quote $quote,
\Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment,
\Magento\Quote\Model\Quote\Address\Total $total
)
{
parent::collect($quote, $shippingAssignment, $total);
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('\Magento\Customer\Model\Session');
$customerId = $customerSession->getCustomer()->getId();
$customerPackageId = $customerSession->getCustomer()->getPackagePercentage();
return $customerPackageId;
}
}
js file:
define(
[
'jquery',
'Magento_Checkout/js/view/summary/abstract-total'
],
function ($,Component) {
"use strict";
return Component.extend({
defaults: {
template: 'Amy_MyPackage/checkout/summary/discount'
},
isDisplayedDiscount : function(){
return true;
},
getDiscount : function(){
return customerPackageId;
}
});
}
);
HTML file:(binding the data)
<!-- ko if: isDisplayedDiscount() -->
<tr class="totals discount excl">
<th class="mark" colspan="1" scope="row" data-bind="text: discount"></th>
<td class="amount">
<span class="price" data-bind="text: getDiscount(), attr: {'data-th': discount}"></span>
</td>
</tr>
<!-- /ko -->
As I am new to knockoutjs, please help me
Any help would be appreciated.