checkout页面给window.checkoutConfig添加自定义变量

2.99K 浏览开发笔记

checkout页面给window.checkoutConfig添加自定义变量

这也是项目开发中经常遇到的问题,对checkout支付页面给些js变量,方便业务逻辑修改。

因为checkout页面内容html基本上都用是js渲染生成的。

比如我想加一个变量,比如is_show_custom_price。然后js里判断如果is_show_custom_price值为true的话 就显示html出来。

分3步

假设你已经创建了一个插件,名为Zou_Test。路径为app/code/Zou/Test.

一,在di.xml里定义逻辑类

注意,必须放在frontend/di.xml里。

vim 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="MagentoCheckoutModelCompositeConfigProvider">        <arguments>            <argument name="configProviders" xsi:type="array">                <item name="test_additional_provider" xsi:type="object">ZouTestModelAdditionalConfigProvider</item>            </argument>        </arguments>    </type> </config>

二,编写逻辑类代码

vim ModelAdditionalConfigProvider.php

<?php namespace ZouTestModel; class AdditionalConfigProvider implements MagentoCheckoutModelConfigProviderInterface {   public function getConfig()   {   $output = []; $output[&#39;is_show_custom_price&#39;] = true; return $output; } }

三,前台js里输出测试

比如在某个js里这样输出

console.log(window.checkoutConfig.is_show_custom_price);

0