如何将phtml/php文件中的变量传递给js文件?

How to pass a variable in phtml/php file to js file?

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

首先,我在 php 文件中创建了一个 if 语句 (view.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);        
    } 

如您所见,该方法正在重新调整 $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);
                }
            });

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

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

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

首先添加一个输入隐藏字段,并在$factor变量已经initialized.Then通过javascript[=14访问这个变量后,将$factor变量赋值给它=]

说 在您的 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();

我希望这就是你的意思,伙计。 :)