How to get decimal value of weight in mini cart.
I need to display only 2 decimals 11.00 instead of 11.0000
Asked
Active
Viewed 828 times
1
trilok kumar
- 427
- 2
- 10
- 21
-
you want weight value as like 11.00, right ? – Aditya Shah Oct 15 '18 at 07:33
-
yeah its script had written in knockoutjs Aditya Shah – trilok kumar Oct 15 '18 at 08:14
-
Can you please add your phtml & js file code here ? – Rohan Hapani Oct 15 '18 at 08:52
-
– trilok kumar Oct 15 '18 at 08:54
-
Code is in .html file Rohan Hapan – trilok kumar Oct 15 '18 at 08:54
-
@RohanHapani i was just thinking to tag you here :D for giving an answer, because i saw some post where you have gave solution kind of like this :) – Aditya Shah Oct 15 '18 at 09:07
-
1Yeah :D But, it's knockout. So, It's different que than I gave all answer. – Rohan Hapani Oct 15 '18 at 09:07
-
@trilokkumar please check my answer and let me know if still not solve. – Rohan Hapani Oct 15 '18 at 09:18
3 Answers
2
For that, You need to add one custom function and pass your precision value and weight value. Add this below code in your html file :
<strong class="product-item-Weight">
<!-- ko if: weight -->
<div class="Approx-Weight" data-bind="html: 'Approx Weight : ' + getWeight(2,weight * qty) ">
</div>
<!-- /ko -->
</strong>
Now, create getWeight() function in your knockout file inside Component.extend :
getWeight : function(precision,WeightValue){
var self = this;
return WeightValue / Math.pow(10, self.precision());
},
Now, refresh your html and knockout js file and check it.
Hope, It maybe helpful for you.
Rohan Hapani
- 17,388
- 9
- 54
- 96
-
Where should i create getWeight() function in your knockout file inside Component.extend Rohan Hapani – trilok kumar Oct 15 '18 at 09:23
-
You can add anywhere inside Component.extend. It should be like this : return Component.extend({ defaults:{ ........... }, getWeight : function(precision,WeightValue){ return WeightValue / Math.pow(10, self.precision()); }, .............. }); – Rohan Hapani Oct 15 '18 at 09:26
-
-
-
Any error or log or console error? Please check it that html and js changes apply or not. – Rohan Hapani Oct 15 '18 at 09:43
-
Unable to process binding "if: function(){return weight }" Message: Unable to process binding "html: function(){return 'Approx Weight66 : '+ getWeight(2,weight * qty) }" Message: getWeight is not defined error Rohan Hapani – trilok kumar Oct 15 '18 at 09:45
-
-
1
you can use number_format function like this in minicart template file-
number_format($weight, 2, '.', '');
Shashank Kumrawat
- 2,110
- 23
- 61
1
JS:
var value = 11.0000;
var result = value.toFixed(2);
alert(result); // 11.00
PHP:
$value = "11.0000";
$result = bcadd($value , 0, 2);
echo $result ; // 11.00
Update:
You can create a block, you put your js inside, then you bind that block in your knockoutjs
1. <?php $jsBlock = echo $this->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setTemplate("html/custom.phtml")->toHtml(); ?>
<script type="text/javascript">
var js_block = <?php echo json_encode($jsBlock)?>;
</script>
2. web/template/somename.html
<div class="name-class" data-bind="html:js_block"></div>
3. custom.phtml
<script type="text/javascript">
var value = 11.0000;
var result = value.toFixed(2);
alert(result); // 11.00
</script>
Update2:
app/design/frontend/{Vendor}/{theme}/Magento_Checkout/view/frontend/templates/cart/minicart.phtml
<?php $jsBlock = echo $this->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setTemplate("html/custom.phtml")->toHtml(); ?> <script type="text/javascript"> var js_block = <?php echo json_encode($jsBlock)?>; </script>web/template/somename.html
<div class="name-class" data-bind="html:js_block"></div>
PЯINCƎ
- 11,669
- 3
- 25
- 80
-
-
-
web/template/somename.html my file location here how to get PRINCE – trilok kumar Oct 15 '18 at 08:28
-
-


