简体   繁体   中英

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

The current free Flex2 shipping module has a bug that causes an incorrect tax calculation on the shipping amount. The bug results in the DOMESTIC tax rate being used in all calculations, even if the shipment is international. 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. 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).

The problem occurs during checkout. The checkout module for VirtueMart is contained in the file ps_checkout.php. The function "calc_order_shipping" begins with the following code snippet:

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.

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". 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).

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. 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).

Both these functions reside in the same source code file: flex2.php, within the class flex2.

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. The corresponding parameter "FLEX2_TAX_CLASS " for International shipping is not referenced anywhere. ”未在任何地方引用。 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.

So, here is my problem...

Is there some way I can define a reference to $d with global scope relative to the class; 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? 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?

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.

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. 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.

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.

I have discovered a code snippet in the php_checkout.php file that shows some promise. 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. Currently, this code does not exist in the "flex2.php" file. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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