如何在 Magento 2 中从 float 创建一个数量实例?
How can I make an amount instance from float in Magento 2?
有一个名为 $price 的自定义生成的 float,它应该显示为产品列表中产品的价格Magento 2. 为了显示含税价格 excluded/included,使用方法 renderAmount。该方法需要一个参数。该参数称为 $amount 并且我如何看待它,它需要是 amount.
类型
如何将 (float) $price 转换为 (amount) $amount 以便能够使用方法 renderAmount 正确吗?
换句话说:虚函数 convertFloatToAmount 会做什么?
<?php
// ...
$price = 100.00;
$amount = convertFloatToAmount( $price );
?>
<?= $block->renderAmount( $amount , [ /* ... */ ] ); ?>
工作解决方案
创建内部实例Amount\Baseclass.
要完成这项工作,需要包含价格。税收和价格税。
提示:如果您不知道这些,您可以通过现有值计算它们。
<?php
// Define price incl. taxes
$price_incl_taxes = 120.00;
// Define the price taxes
$price_taxes = 20.00;
// If you need to know the price excl. taxes (== 100.00 in this example)
/* $price_excl_taxes = $price_incl_taxes - $price_taxes; */
// Create (amount) $amount
$amount = new Magento\Framework\Pricing\Amount\Base($price_incl_taxes, ['tax'=>$price_taxes]);
?>
有一个名为 $price 的自定义生成的 float,它应该显示为产品列表中产品的价格Magento 2. 为了显示含税价格 excluded/included,使用方法 renderAmount。该方法需要一个参数。该参数称为 $amount 并且我如何看待它,它需要是 amount.
类型如何将 (float) $price 转换为 (amount) $amount 以便能够使用方法 renderAmount 正确吗?
换句话说:虚函数 convertFloatToAmount 会做什么?
<?php
// ...
$price = 100.00;
$amount = convertFloatToAmount( $price );
?>
<?= $block->renderAmount( $amount , [ /* ... */ ] ); ?>
工作解决方案
创建内部实例Amount\Baseclass.
要完成这项工作,需要包含价格。税收和价格税。
提示:如果您不知道这些,您可以通过现有值计算它们。
<?php
// Define price incl. taxes
$price_incl_taxes = 120.00;
// Define the price taxes
$price_taxes = 20.00;
// If you need to know the price excl. taxes (== 100.00 in this example)
/* $price_excl_taxes = $price_incl_taxes - $price_taxes; */
// Create (amount) $amount
$amount = new Magento\Framework\Pricing\Amount\Base($price_incl_taxes, ['tax'=>$price_taxes]);
?>