Want to retrieve a product’s Magento 2 tier prices as an array and store them in a JavaScript variable for later use? Follow these steps:
|
1 2 3 4 5 6 7 |
<script> var tierPrices = <?= json_encode(array_map(fn($t) => [ 'qty' => (float)$t->getQty(), 'price' => (float)$t->getValue(), 'customer_group_id' => (int)$t->getCustomerGroupId() ], $product->getTierPrices())) ?>; </script> |
You can now access the values from JavaScript, for example using a function that returns the tier price for a given quantity:
|
1 2 3 4 5 6 7 8 9 |
function getTierPriceForQty(qty) { let price = null; tierPrices.forEach(function(tier) { if (qty >= tier.qty) { price = tier.price; } }); return price; } |
Fertig!
Storing and reading tier prices in a JavaScript variable
Need help implementing this solution, or still looking for a fix to your Magento problem? Get in touch and I will be happy to help with your project.