問題描述
ZenCart 中的優惠券問題 (Coupon problem in ZenCart)
I am creating a ZenCart payment module. I can pass through the products, the shipping details as well as the shipping fee, but it won’t pass the coupon through without messing up the currency format.
If i do this, it won’t display the coupon at all
$mCouponCost => $order->info['coupon_cost'] * $order->info['currency_value'];
If i do it like this, it will display the coupon but it messes up the currency format
$mCouponCost = $order->info['coupon_cost'] -> $order->info['currency_value'];
All the code is below:
$mCouponCost = $order->info['coupon_cost'] - $order->info['currency_value'];
if (!empty($mCouponCost)) {
$j++;
$process_button_string .= zen_draw_hidden_field('LIDSKU' . $j, 'Coupon') .
zen_draw_hidden_field('LIDDesc' . $j, 'Coupon Cost') .
zen_draw_hidden_field('LIDPrice' . $j, number_format($mCouponCost, 2, '.', '')) .
zen_draw_hidden_field('LIDQty' . $j, '1') .
zen_draw_hidden_field('ShippingRequired' . $j, '1') .
zen_draw_hidden_field('IsVoucher' . $j, '0');
}
Where am I going wrong?
參考解法
方法 1:
You posted 3 variations of the $mCouponCost = xxxxxxx
The first uses * to multiply ... which is the correct solution.
The second uses -> which would definitely be wrong because the data is not in the object format which -> would require.
The third, which is part of what you said is "all the code", uses a - which would be subtracting, and definitely would not produce the correct result.
Your code requires a few things, some of which are absent from your post:
a) the $order object must be declared as a global inside the function/method you're using it in
b) you must have written your own code to actually declare and assign a value to the 'coupon_cost' element of the $order->info array
c) if you're dealing with multiple currencies, then multiplying the base cost by $order->info['currency_value'] would yield the correct result for the currency in which the customer is shopping.
A simple test to determine whether your problem is actually related to the line of code you first quoted, would be to simply assign
$mCouponCost = $order->info['coupon_cost'];
ie: without doing any multiplication at all. Doing so would reveal useful information about what other problems might actually be causing the difficulties you're encountering.