【问题标题】:How to pass a variable in phtml/php file to js file?如何将 phtml/php 文件中的变量传递给 js 文件?
【发布时间】:2015-03-17 09:05:32
【问题描述】:

这是关于 Magento 电子商务网站的。
我现在需要将一个变量从 view.phtml 传递给 product.js。

首先,我在 php 文件(view.phtml)中创建了一个 if 语句,以便从数据库中获取折扣率:

if(Mage::getSingleton('customer/session')->isLoggedIn()) {
    $customerData = Mage::getSingleton('customer/session')->getCustomer();
    //echo $customerData->getemail();

    $conn = Mage::getModel('core/resource')->getConnection('core_read');

            $sql = 'select max(sl.discount_amount) from salesrule sl 
                    join salesrule_customer_group scg on scg.rule_id = sl.rule_id
                    join customer_entity ce on ce.group_id = scg.customer_group_id
                    where ce.email = "'.$customerData->getemail().'";';

        $results = $conn->fetchAll($sql);
        //echo $results;
    }



    else
        { $factor =  100;} //only apply to NOTLOGGED IN's custom option price label



    foreach ($results as $result){
        {   $factor = $result['max(sl.discount_amount)'];
        }
        //echo '<small> Discounted price: </small> $',$prices*($factor/100);        
    } 

如您所见,该方法正在重新调整 $factor 作为结果,我需要在 product.js(javascript) 文件中传递此变量 $factor。

  var subPrice = 0;
            var subPriceincludeTax = 0;


            //var a = test;
            Object.values(this.customPrices).each(function(el){
                if (el.excludeTax && el.includeTax) {
                    subPrice += parseFloat(el.excludeTax); // this will affect the price when changing option *important
                    //so need to change somewhere else in the same time to full fill the requirementirement
                    // but it doesn;t change to backend value
                    subPriceincludeTax += parseFloat(el.includeTax);
                } else {
                    subPrice += parseFloat(el.price);
                    subPriceincludeTax += parseFloat(el.price);
                }
            });
            excl += subPrice;//also affect the final price *frontEnd
            incl += subPriceincludeTax;

我试图通过 JSON 传递它,但它根本不起作用,我无法弄清楚原因。我有类似echo json_encode($factor); 的东西来验证输出,它看起来很好,但仍然无法传递到外部 js 文件。

有人可以帮忙吗?谢谢

更新 1: 在 php 文件中,我添加了这些:

var optionsPrice = new Product.OptionsPrice(<?php echo $this->getJsonConfig() ?>); 

var tester = "<?php echo json_encode($factor);?>"
</script>

并将这些代码传递给js,例如:

var subPrice = 0;
            var subPriceincludeTax = 0;
            var tester = tester;

            //var a = test;
            Object.values(this.customPrices).each(function(el){
                if (el.excludeTax && el.includeTax) {
                    subPrice += parseFloat(el.excludeTax)*tester; // this will affect the price when changing option *important
                    //so need to change somewhere else in the same time to full fill the requirementirement
                    // but it doesn;t change to backend value
                    subPriceincludeTax += parseFloat(el.includeTax);
                } else {
                    subPrice += parseFloat(el.price);
                    subPriceincludeTax += parseFloat(el.price);
                }
            });
            excl += subPrice;//also affect the final price *frontEnd
            incl += subPriceincludeTax;

但这似乎不起作用:(

更新 2 根据outlooker,我尝试在phtml文件中添加输入类型,如下所示:

<script type="text/javascript">

var optionsPrice = new Product.OptionsPrice(<?php echo $this->getJsonConfig() ?>); //the var of the option price, disable to disable price options

**<input type="hidden" id="factorDiscount" value="<?php echo $factor;?>"/>**
</script>

之后我也将var实现到js文件中,调用并使用:

    var subPrice = 0;
            var subPriceincludeTax = 0;
            var factor = $("#factorDiscount").val();//is not global


            Object.values(this.customPrices).each(function(el){
                if (el.excludeTax && el.includeTax) {
                    subPrice += parseFloat(el.excludeTax)*factor; // this will affect the price when changing option *important
                    subPriceincludeTax += parseFloat(el.includeTax);
                } else {
                    subPrice += parseFloat(el.price);
                    subPriceincludeTax += parseFloat(el.price);
                }
            });

它现在不起作用,但我明白你的意思,但我可能没有以正确的方式插入代码。任何意见?先生。谢谢你

更新 3 谢谢你的帮助,先生。我将代码完美地插入了phtml文件中,但是当我尝试使用它时,它似乎没有从php中获取数据

var subPrice = 0;
            var subPriceincludeTax = 0;
            var factor = $("#factorDiscount").val();//I declare the var here


            Object.values(this.customPrices).each(function(el){
                if (el.excludeTax && el.includeTax) {
                    subPrice += parseFloat(el.excludeTax)*factor; // trying to times the var factor with the price but don't know if i am doing the right things.
                    subPriceincludeTax += parseFloat(el.includeTax);
                } else {
                    subPrice += parseFloat(el.price);
                    subPriceincludeTax += parseFloat(el.price);
                }
            });

【问题讨论】:

  • 将值保存在隐藏字段中并通过javascript访问它
  • 介意给我更多细节吗?

标签: javascript php jquery json magento


【解决方案1】:

首先添加一个输入隐藏字段,在$factor变量初始化完成后,将$factor变量赋值给它。然后通过javascript访问这个变量

说 在你的 phtml 文件中添加

    if(Mage::getSingleton('customer/session')->isLoggedIn()) {
        $customerData = Mage::getSingleton('customer/session')->getCustomer();
        //echo $customerData->getemail();

        $conn = Mage::getModel('core/resource')->getConnection('core_read');

                $sql = 'select max(sl.discount_amount) from salesrule sl 
                        join salesrule_customer_group scg on scg.rule_id = sl.rule_id
                        join customer_entity ce on ce.group_id = scg.customer_group_id
                        where ce.email = "'.$customerData->getemail().'";';

            $results = $conn->fetchAll($sql);
            //echo $results;
        }



        else
            { $factor =  100;} //only apply to NOTLOGGED IN's custom option price label



        foreach ($results as $result){
            {   $factor = $result['max(sl.discount_amount)'];
            }
            //echo '<small> Discounted price: </small> $',$prices*($factor/100);        
        } 

?>
    <input type="hidden" id="factorDiscount" value="<?php echo $factor;?>"/> 
//Hidden field added here

<?php
  //If you have other codes place it here
?>

在 js 文件中使用访问上述变量

var factor = $("#factorDiscount").val();

我希望这就是你的意思 mate 。 :)

【讨论】:

  • 谢谢,但请检查我的最新更新,我对这些修改很陌生。
  • foreach ($results as $result){ { $factor = $result['max(sl.discount_amount)']; }后面添加隐藏字段
  • 不在脚本标签内
  • foreach ($results as $result) { $factor = $result['max(sl.discount_amount)'];} '&lt;input type="hidden" id="factorDiscount" value="&lt;?php echo $factor;?&gt;"/&gt;; 我在该行之后插入它,但使用 '' 因为 php 不允许 html 标记?对不起,我是菜鸟
  • 当我插入隐藏的输入类型标签时,它会使我的页面为空,就像你在答案中提到的那样
【解决方案2】:

如果您的外部 js 文件具有 .js 扩展名,则它可能不会被作为 php 处理,并且 php 源代码将保留在那里,从而破坏 javascript。

有几种方法可以解决这个问题,例如:

  • 把你的js文件的扩展名改成.php;
  • 通过页面脚本/magento传递变量并检查它是否存在于您的外部js文件中。
  • 设置服务器将所有.js文件处理为php文件;

【讨论】:

  • 我认为我可以选择的唯一方法是“通过页面脚本/magento 传递变量并检查它是否存在于您的外部 js 文件中。”请看更新代码
  • @anson920520 这就是我通常在我的 js 中用作 php 的方式,这意味着我无法最小化、缓存等。
  • 我也是,但我使用的是电商电话 Magento,它的结构是这样的
  • 你应该检查html / javascript的来源,它是什么样的?并检查生成的 javascript 变量的范围。
  • 我正在同时查看源代码,但它太大了,需要我永远去挖掘。总之谢谢
猜你喜欢
  • 2010-11-18
  • 2014-07-05
  • 1970-01-01
  • 2018-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-29
  • 2021-12-10
相关资源
最近更新 更多