totals items的数据流程
MagentoQuoteModelCartTotalsItem(MagentoQuoteApiDataTotalsItemInterface)
MagentoCheckoutModelDefaultConfigProvider::getConfig()
MagentoCheckoutBlockOnepage::getCheckoutConfig()
window.checkoutConfig.totalsData.items
quote.totals().items (Magento_Checkout/js/model/quote as quote)
可用plugin修改Magento\Checkout\Model\DefaultConfigProvider::getConfig()
来改变totals items
以下是为totals 中的product item添加brand属性的过程
etc/frontend/di.xml
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Checkout\Model\DefaultConfigProvider">
<plugin name="infinity_checkout_model_defaultconfigprovider" type="Infinity\Checkout\Plugin\Magento\Checkout\Model\DefaultConfigProvider" />
</type>
</config>
namespace Infinity\Checkout\Plugin\Magento\Checkout\Model;
class DefaultConfigProvider
{
protected $productRepository;
public function __construct(
\Magento\Catalog\Model\ProductRepository $productRepository
) {
$this->productRepository = $productRepository;
}
public function afterGetConfig(\Magento\Checkout\Model\DefaultConfigProvider $subject, $return) {
if(!empty($return['totalsData']['items']) &&
is_array($return['totalsData']['items']) &&
!empty($return['quoteItemData']) &&
is_array($return['quoteItemData'])
) {
$items = $return['totalsData']['items'];
$quoteItemData = $return['quoteItemData'];
$quoteItemData = array_column($quoteItemData, NULL, 'item_id');
foreach($items as $key => $item) {
$id = $quoteItemData[$item['item_id']]['product_id'];
if($id) {
$product = $this->productRepository->getById($id);
$items[$key]['brand'] = $product->getAttributeText('general_brand');
}
}
$return['totalsData']['items'] = $items;
}
return $return;
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。