From d1ac3731a57dd373c48743a01d137bde7e7f0817 Mon Sep 17 00:00:00 2001 From: Boro Sitnikovski Date: Mon, 18 May 2020 21:04:13 +0200 Subject: [PATCH 1/3] Add actions before/after shipping calculation --- includes/abstracts/abstract-wc-shipping-method.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/includes/abstracts/abstract-wc-shipping-method.php b/includes/abstracts/abstract-wc-shipping-method.php index c5922a75627..b62e4458a22 100644 --- a/includes/abstracts/abstract-wc-shipping-method.php +++ b/includes/abstracts/abstract-wc-shipping-method.php @@ -230,7 +230,9 @@ abstract class WC_Shipping_Method extends WC_Settings_API { public function get_rates_for_package( $package ) { $this->rates = array(); if ( $this->is_available( $package ) && ( empty( $package['ship_via'] ) || in_array( $this->id, $package['ship_via'] ) ) ) { + do_action( 'woocommerce_before_calculate_shipping', $package, $this ); $this->calculate_shipping( $package ); + do_action( 'woocommerce_after_calculate_shipping', $package, $this ); } return $this->rates; } From dd512c6f40e1bbc79ebfc2ff165db4f60812f741 Mon Sep 17 00:00:00 2001 From: Boro Sitnikovski Date: Mon, 1 Jun 2020 19:25:18 +0200 Subject: [PATCH 2/3] Move filter out of abstract class and into the manager class --- includes/abstracts/abstract-wc-shipping-method.php | 2 -- includes/class-wc-shipping.php | 2 ++ 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/abstracts/abstract-wc-shipping-method.php b/includes/abstracts/abstract-wc-shipping-method.php index b62e4458a22..c5922a75627 100644 --- a/includes/abstracts/abstract-wc-shipping-method.php +++ b/includes/abstracts/abstract-wc-shipping-method.php @@ -230,9 +230,7 @@ abstract class WC_Shipping_Method extends WC_Settings_API { public function get_rates_for_package( $package ) { $this->rates = array(); if ( $this->is_available( $package ) && ( empty( $package['ship_via'] ) || in_array( $this->id, $package['ship_via'] ) ) ) { - do_action( 'woocommerce_before_calculate_shipping', $package, $this ); $this->calculate_shipping( $package ); - do_action( 'woocommerce_after_calculate_shipping', $package, $this ); } return $this->rates; } diff --git a/includes/class-wc-shipping.php b/includes/class-wc-shipping.php index 8027b84a693..8c9acfb1d8c 100644 --- a/includes/class-wc-shipping.php +++ b/includes/class-wc-shipping.php @@ -332,7 +332,9 @@ class WC_Shipping { if ( ! is_array( $stored_rates ) || $package_hash !== $stored_rates['package_hash'] || 'yes' === get_option( 'woocommerce_shipping_debug_mode', 'no' ) ) { foreach ( $this->load_shipping_methods( $package ) as $shipping_method ) { if ( ! $shipping_method->supports( 'shipping-zones' ) || $shipping_method->get_instance_id() ) { + do_action( 'woocommerce_before_get_rates_for_package', $package, $shipping_method, $this ); $package['rates'] = $package['rates'] + $shipping_method->get_rates_for_package( $package ); // + instead of array_merge maintains numeric keys + do_action( 'woocommerce_after_get_rates_for_package', $package, $shipping_method, $this ); } } From 855b1d3b6b10a9367d645974145a863ffa29d90c Mon Sep 17 00:00:00 2001 From: Claudio Sanches Date: Mon, 1 Jun 2020 14:35:31 -0300 Subject: [PATCH 3/3] Added docblocks and removed $this --- includes/class-wc-shipping.php | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/includes/class-wc-shipping.php b/includes/class-wc-shipping.php index 8c9acfb1d8c..a803c400f2f 100644 --- a/includes/class-wc-shipping.php +++ b/includes/class-wc-shipping.php @@ -332,9 +332,26 @@ class WC_Shipping { if ( ! is_array( $stored_rates ) || $package_hash !== $stored_rates['package_hash'] || 'yes' === get_option( 'woocommerce_shipping_debug_mode', 'no' ) ) { foreach ( $this->load_shipping_methods( $package ) as $shipping_method ) { if ( ! $shipping_method->supports( 'shipping-zones' ) || $shipping_method->get_instance_id() ) { - do_action( 'woocommerce_before_get_rates_for_package', $package, $shipping_method, $this ); - $package['rates'] = $package['rates'] + $shipping_method->get_rates_for_package( $package ); // + instead of array_merge maintains numeric keys - do_action( 'woocommerce_after_get_rates_for_package', $package, $shipping_method, $this ); + /** + * Fires before getting shipping rates for a package. + * + * @since 4.3.0 + * @param array $package Package of cart items. + * @param WC_Shipping_Method $shipping_method Shipping method instance. + */ + do_action( 'woocommerce_before_get_rates_for_package', $package, $shipping_method ); + + // Use + instead of array_merge to maintain numeric keys. + $package['rates'] = $package['rates'] + $shipping_method->get_rates_for_package( $package ); + + /** + * Fires after getting shipping rates for a package. + * + * @since 4.3.0 + * @param array $package Package of cart items. + * @param WC_Shipping_Method $shipping_method Shipping method instance. + */ + do_action( 'woocommerce_after_get_rates_for_package', $package, $shipping_method ); } }