New version causes problem with overridden shipping label
-
Hi!
I have had problems with the overridden shipping label and also the new shipping price, that the correct numbers are not shown. I have had to temporarily modify the code in class-shippingdiscount.php to:
- Override Shipping Price
* - @param array $rates Rates.
- @return array
*/
private function recalculate_shipping_taxes( $rate, $cost ) {
$taxes = array();
if ( ! empty( $rate->taxes ) && is_array( $rate->taxes ) ) {
$tax_rates = WC_Tax::get_shipping_tax_rates();
$new_taxes = WC_Tax::calc_shipping_tax( $cost, $tax_rates );
foreach ( $rate->taxes as $tax_rate_id => $tax_amount ) {
$taxes[ $tax_rate_id ] = isset( $new_taxes[ $tax_rate_id ] ) ? $new_taxes[ $tax_rate_id ] : 0;
}
}
return $taxes;
}
public function override_shipping_price( $rates ) {
if ( is_admin() && ! defined( ‘DOING_AJAX’ ) ) {
return $rates;
} $coupons = WC()->cart->get_applied_coupons();
$shipping_coupon = false; foreach ( $coupons as $coupon_code ) {
$coupon = new WC_Coupon( $coupon_code );
$shipping_discount_type = $coupon->get_meta( ‘shipping_discount_type’ );if ( 'shipping_discount' === $coupon->get_discount_type() ) { $shipping_coupon = $coupon; break; }
} if ( false === $shipping_coupon ) {
return $rates;
} foreach ( $rates as $rate ) {
$shipping_before_discount = floatval( $rate->cost );
$coupon_amount = floatval( $shipping_coupon->get_amount() );$max_amount = floatval( $shipping_coupon->get_meta( 'shipping_discount_max_amount' ) ); if ( ! is_numeric( $max_amount ) || $max_amount < 1 ) { $max_amount = 0; } $cost = $shipping_before_discount; // Start with the original cost switch ( $shipping_discount_type ) { case 'free': $cost = 0; break; case 'fixed': $cost = max( $shipping_before_discount - $coupon_amount, 0 ); break; case 'percentage': $coupon_amount_percentage = ( $coupon_amount / 100 ) * $shipping_before_discount; if ( ( $max_amount > 0 ) && ( $coupon_amount_percentage > $max_amount ) ) { $coupon_amount_percentage = $max_amount; } $cost = max( $shipping_before_discount - $coupon_amount_percentage, 0 ); break; } $rate->add_meta_data( '_shipping_discount', array( 'before_discount' => $shipping_before_discount, 'after_discount' => $cost, 'discount_type' => $shipping_discount_type, 'discount_amount' => $coupon_amount, ) ); // Recalculate tax based on the new shipping cost $taxes = $this->recalculate_shipping_taxes( $rate, $cost ); $rate->taxes = $taxes; $rate->cost = $cost;
} return $rates;
} /** Override Shipping Label
*- @param string $label Label.
- @param object $method Method.
*/
public function override_shipping_label($label, $method)
{
$coupons = WC()->cart->get_applied_coupons();
$shipping_coupon = false; foreach ($coupons as $coupon_code) {
$coupon = new WC_Coupon($coupon_code);if ('shipping_discount' === $coupon->get_discount_type()) { $shipping_coupon = true; break; }
} if (false === $shipping_coupon) {
return $label;
} $meta = $method->get_meta_data(); $meta_shipping_discount = $meta[‘_shipping_discount’];
if (!is_array($meta_shipping_discount)) {
return $label;
} $shipping_before_discount = $meta_shipping_discount[‘before_discount’] ?? ”;
if (empty($shipping_before_discount)) {
return $label;
} $label_array = explode(‘:’, $label); if (count($label_array) > 1) {
array_pop($label_array);
} $label_str = implode(‘:’, $label_array); if (‘:’ !== substr($label_str, -1)) {
$label_str .= ‘: ‘;
} if ((float) $shipping_before_discount !== (float) $method->cost + array_sum($method->taxes)) {
$tax_rates = WC_Tax::get_shipping_tax_rates();
$shipping_taxes = WC_Tax::calc_shipping_tax($shipping_before_discount, $tax_rates);
$shipping_before_discount_with_tax = $shipping_before_discount + array_sum($shipping_taxes);
$label = $label_str . ‘
‘ . wc_price($shipping_before_discount_with_tax) . ‘‘ . wc_price($method->cost + array_sum($method->taxes)) . ‘‘;
}return $label; }
The page I need help with: [log in to see the link]
- Override Shipping Price
- You must be logged in to reply to this topic.