简体   繁体   English

修正Virtuemart 1.1.9 for Joomla 1.5.26的Flex2运输模块中的错误时,PHP范围问题

[英]PHP scope issue while fixing error in the Flex2 shipping module of Virtuemart 1.1.9 for Joomla 1.5.26

Background 背景

The current free Flex2 shipping module has a bug that causes an incorrect tax calculation on the shipping amount. 当前的免费Flex2送货模块有一个错误,该错误会导致错误计算送货金额。 The bug results in the DOMESTIC tax rate being used in all calculations, even if the shipment is international. 该错误导致即使在国际运输中,也将在所有计算中使用DOMESTIC税率。 In addition, I'm trying to expand the functionality of the module. 另外,我正在尝试扩展模块的功能。

In its current form, Flex2 allows for shipping rates to be set independently for Domestic and International sales. 在当前形式下,Flex2允许针对国内和国际销售独立设置运费。 However, the tax rate may only be specific value for each variant (ie one tax rate for Domestic, one tax rate for International). 但是,税率只能是每个变体的特定值(即,国内税率为一种,国际税率为一种)。

My home country (Canada) requires a different tax rate to be applied within the Domestic market, depending on which Province I am shipping to. 我的祖国(加拿大)要求在国内市场上应用不同的税率,具体取决于我要运送到的省份。 I'm sure there are other countries/jurisdictions that have similar requirements. 我敢肯定还有其他国家/地区也有类似的要求。 I have already been able to successfully change the administrative screens to allow for the setting of an indicator that will allow for a variable tax rate based on the VirtueMart TAX_MODE value (which selects different tax rates based on Store Location, Buyer Location, or EU Zone). 我已经能够成功更改管理屏幕,以允许设置指示器,该指示器将允许根据VirtueMart TAX_MODE值(根据商店位置,买方位置或欧盟区域选择不同的税率)来选择可变的税率。 )。

The issue - communicating information from class "A" to class "B" without parameters 问题-在没有参数的情况下将信息从“ A”类传递到“ B”类

The problem occurs during checkout. 在结帐时出现问题。 The checkout module for VirtueMart is contained in the file ps_checkout.php. VirtueMart的签出模块包含在文件ps_checkout.php中。 The function "calc_order_shipping" begins with the following code snippet: 函数“ calc_order_shipping”以以下代码段开头:

function calc_order_shipping( &$d ) {

  $auth = $_SESSION['auth']; $shipping_total = $this->_SHIPPING->get_rate( $d ); $shipping_taxrate = $this->_SHIPPING->get_tax_rate(); 

This is part of the "core" VirtueMart system, so I don't want to modify it. 这是“核心” VirtueMart系统的一部分,因此我不想对其进行修改。

The first function call extracts the Shipping Total (ie the result of the tax calculation performed at the beginning of the checkout process) from the array "$d". 第一个函数调用从数组“ $ d”中提取装运总计(即,在结帐过程开始时执行的税收计算的结果)。 The second line is meant to retrieve the tax rate that was used to do this calculation. 第二行用于检索用于进行此计算的税率。 The current implementation of ALL shipping modules is to return a static tax rate. 所有运输模块的当前实现是返回静态税率。 As you can see, the array "$d" is NOT BEING PASSED to the second function, so I don't have enough information to determine whether the customer is foreign or domestic (this information can be derived from the shipping address information which is part of the $d array). 如您所见,数组“ $ d”没有传递给第二个函数,因此我没有足够的信息来确定客户是外国的还是国内的(此信息可以从送货地址信息中得出$ d数组的一部分)。

Unfortunately, prior to my requirements for Flex2 variable tax rates, there was only one tax rate per Domestic/International zone allowed - and this was set manually during configuration of the shipping module. 不幸的是,在我对Flex2可变税率提出要求之前,每个国内/国际区域仅允许使用一种税率-这是在配置运输模块时手动设置的。 So, there was no chance of the value "changing". 因此,没有机会“改变”价值。 It was OK (in the past) to simply extract the tax rate after the fact from the static configuration files of the shipping module. (过去)可以简单地从运输模块的静态配置文件中提取事实之后的税率。 However, with my newly required functionality, I need to potentially alter the tax rate during checkout (based on the possibility that the shopper may select a delivery address other than the default address). 但是,使用我新需要的功能,我需要在结帐时更改税率(基于购物者可能选择默认地址以外的收货地址的可能性)。

Details of "Class B" “ B级”的详细信息

Both these functions reside in the same source code file: flex2.php, within the class flex2. 这两个函数都位于类flex2中的相同源代码文件:flex2.php中。

Here, for the sake of completeness, is the code for these two functions: 为了完整起见,这里是这两个函数的代码:

function get_rate( &$d ) {

  $shipping_rate_id = $d["shipping_rate_id"]; $is_arr = explode("|", urldecode(urldecode($shipping_rate_id)) ); $order_shipping = (float)$is_arr[3]; return $order_shipping; } function get_tax_rate() { /** Read current Configuration ***/ require_once(CLASSPATH ."shipping/".__CLASS__.".cfg.php"); if( intval(FLEX2_TAX_CLASS)== 0 ) { return( 0 ); } else { require_once( CLASSPATH. "ps_tax.php" ); $tax_rate = ps_tax::get_taxrate_by_id( intval(FLEX2_TAX_CLASS) ); return $tax_rate; } } 

The "bug" in Flex2 (which is not the topic of this question) lies in the fact that the parameter "FLEX2_TAX_CLASS" is only associated with Domestic shipping. Flex2中的“错误”(不是本问题的主题)在于,参数“ FLEX2_TAX_CLASS”仅与国内运输相关。 The corresponding parameter "FLEX2_TAX_CLASS _I " for International shipping is not referenced anywhere. 国际运输的相应参数“ FLEX2_TAX_CLASS _I ”未在任何地方引用。 For me to determine whether to use the Domestic vs. the International version of the variable, I have to examine the shipping address, which is stored in the $d array. 对于我来说,要确定是否使用该变量的国内版本还是国际版本,我必须检查送货地址,该地址存储在$ d数组中。

The challenge to be overcome - aka the reason for this post 要克服的挑战-也就是发布该帖子的原因

So, here is my problem... 所以,这是我的问题...

Is there some way I can define a reference to $d with global scope relative to the class; 有什么办法可以定义相对于该类具有全局作用域的$ d引用? and then modify the "get_rate" function so that it stores a copy of its $d input array in this class-scoped reference variable so it can be referenced within the "get_tax_rate" function? 然后修改“ get_rate”函数,以便将其$ d输入数组的副本存储在此类作用域的引用变量中,以便可以在“ get_tax_rate”函数中对其进行引用? If it is possible, what would be the correct syntax for defining the class-scoped variable, assigning the value of &$d to that variable, and the correct syntax for accessing that newly created variable from the "get_tax_rate" function? 如果可能的话,用于定义类作用域变量,将&$ d的值分配给该变量的正确语法,以及用于从“ get_tax_rate”函数访问该新创建的变量的正确语法是什么?

I've previously researched some generic questions about PHP variable scope, and found a couple of answers that appear to be on-point, but I'm too afraid to try them without getting more specific advice, since the answers I found did not address the specific requirement of function "x" receiving the variable "by reference" from a function in a different class and creating a way that will allow function "y" to also retrieve (and update) information from the same array by reference. 之前,我已经研究了有关PHP变量范围的一些通用问题,并发现了一些似乎很对的答案,但是我很害怕在没有得到更具体建议的情况下尝试这些问题,因为我发现的答案没有解决函数“ x”的特定要求是从不同类的函数中接收“通过引用”变量,并创建一种方式,以允许函数“ y”也可以通过引用从同一数组中检索(和更新)信息。

This is my first question on this forum, and I wanted to be as specific as possible with my request. 这是我在这个论坛上的第一个问题,我想对我的要求尽可能具体。 Thanks for any assistance. 感谢您的协助。

PS Once I get this problem solved, I will be submitting the "Enhanced" Flex2 shipping module as a non-commercial add-on to the Joomla/Virtuemart community. PS:解决此问题后,我将把“增强型” Flex2运输模块作为非商业附件提交给Joomla / Virtuemart社区。 I'm hoping the original author of the Flex2 component will be able to continue supporting it, as my knowledge of PHP is not as good as it should be. 我希望Flex2组件的原始作者能够继续支持它,因为我对PHP的了解不如应有的好。

I have not found an answer for the technical question posed, but I have discovered what might be a viable workaround to solve the business problem. 我没有找到所提出的技术问题的答案,但是我发现了解决业务问题的可行解决方法。

The php_checkout module in VirtueMart performs a number of functions, some of which (like the two functions above) call functions from a different class. VirtueMart中的php_checkout模块执行许多函数,其中一些函数(如上述两个函数)从不同的类调用函数。

I have discovered a code snippet in the php_checkout.php file that shows some promise. 我在php_checkout.php文件中发现了一个显示了一些承诺的代码段。 It is shown below for reference: 如下所示以供参考:

  // Export the order_id so the checkout complete page can get it $d["order_id"] = $order_id; /* * Let the shipping module know which shipping method * was selected. This way it can save any information * it might need later to print a shipping label. */ if( is_callable( array($this->_SHIPPING, 'save_rate_info') )) { $this->_SHIPPING->save_rate_info($d); } 

Apparently, ps_checkout.php potentially contains a call to a function "save_rate_info" within the shipping module. 显然,ps_checkout.php 可能包含对运输模块中的函数“ save_rate_info”的调用。 Currently, this code does not exist in the "flex2.php" file. 当前,该代码在“ flex2.php”文件中不存在。 But, if it were to be added, it would give me the opportunity to update the "$d" array with the information I want to pass on to the checkout module without having to do it directly within the "get_tax_rate" function. 但是,如果要添加它,这将使我有机会使用我想要传递给checkout模块的信息来更新“ $ d”数组,而不必直接在“ get_tax_rate”函数中进行操作。

I would still prefer to receive an answer to the technical question I posed originally, but I hope that this work around may help someone who has a similar problem with applying variable tax rates to a shipping charge. 我仍然希望收到我最初提出的技术问题的答案,但是我希望这种解决方法可以对在将可变税率应用于运费中有类似问题的人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM