L
L
lagudal2020-11-27 13:57:19
PHP
lagudal, 2020-11-27 13:57:19

How to transfer an array with tier prices with tax to JSON?

An array of tier prices can be obtained in Magento 2 in several ways.
For example, something like this:

$allTiers = $_product->getTierPrice();
$checktiers = array_filter($allTiers);

or in the tier_prices.phtml template, you can collect it inside the foreach loop - as an option
$tierPriceArray[] =  array('qty'=> $price['price_qty'], 'price'=> $price['website_price]?>

In general, in any case, I can easily pass an array representation with these prices to the json frontend,
and it looks something like this.
[{"price_id":"30","website_id":"0","all_groups":"1","cust_group":32000,"price":"45.490000","price_qty":"5.0000","percentage_value":null,"website_price":"45.490000"},{"price_id":"31","website_id":"0","all_groups":"1","cust_group":32000,"price":"39.990000","price_qty":"15.0000","percentage_value":null,"website_price":"39.990000"},{"price_id":"32","website_id":"0","all_groups":"1","cust_group":32000,"price":"35.000000","price_qty":"30.0000","percentage_value":null,"website_price":"35.000000"},{"price_id":"33","website_id":"0","all_groups":"1","cust_group":32000,"price":"33.120000","price_qty":"33.0000","percentage_value":null,"website_price":"33.120000"}]

In my case, the quantity and price are important, it is clear that the quantity is price_qty and the price is price or website_price
But! in any case, I get prices without tax at the exit. I have 2 storviews, one for companies, the other for private traders, and the prices in one are with VAT, in the other - without. And you need to get the correct prices in both cases.
Here the hitch for me is that everything is implemented correctly in the Magento core, and if the product has such prices, then they are displayed as they should, i.e. if there is a tax, then with the tax.
Here is the default tier_prices.phtml template file, which displays a table with these prices.
As you can see, the prices in this table are displayed in the $priceAmountBlock variable, and they are displayed correctly. This variable contains html tags besides prices. In principle, I can easily clear them and get the array I need with the correct prices. But I can get it only inside the loop, if I want to get it again after the loop, then the price array element equal to $priceAmountBlock will always have the last value.
If I take $price['price'] or $price['website_price'] as an element of the price array, then I get different prices, but without tax.
As I understand it, it is somewhere in the formation of the $priceAmountBlock variable that what I need is hidden - how to bring the price to the price with tax, but I don’t understand where and how exactly this is due to poor knowledge of php.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question