Merge pull request #20237 from woocommerce/fix/defend-cod-notices

get_canonical_package_rate_ids should check it has an array
This commit is contained in:
Mike Jolley 2018-05-29 10:39:16 +01:00 committed by GitHub
commit 37762024e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 14 deletions

View File

@ -71,12 +71,13 @@ class WC_Gateway_COD extends WC_Payment_Gateway {
$zones[] = new WC_Shipping_Zone( $raw_zone );
}
$zones[] = new WC_Shipping_Zone(0);
$zones[] = new WC_Shipping_Zone( 0 );
foreach ( WC()->shipping()->load_shipping_methods() as $method ) {
$options[ $method->get_method_title() ] = array();
// Translators: %1$s shipping method name.
$options[ $method->get_method_title() ][ $method->id ] = sprintf( __( 'Any "%1$s" method', 'woocommerce' ), $method->get_method_title() );
foreach ( $zones as $zone ) {
@ -89,9 +90,13 @@ class WC_Gateway_COD extends WC_Payment_Gateway {
continue;
}
$option_id = $shipping_method_instance->get_rate_id();
$option_id = $shipping_method_instance->get_rate_id();
// Translators: %1$s shipping method title, %2$s shipping method id.
$option_instance_title = sprintf( __( '%1$s (#%2$s)', 'woocommerce' ), $shipping_method_instance->get_title(), $shipping_method_instance_id );
$option_title = sprintf( __( '%1$s – %2$s', 'woocommerce' ), $zone->get_id() ? $zone->get_zone_name() : __( 'Other locations', 'woocommerce' ), $option_instance_title );
// Translators: %1$s zone name, %2$s shipping method instance name.
$option_title = sprintf( __( '%1$s – %2$s', 'woocommerce' ), $zone->get_id() ? $zone->get_zone_name() : __( 'Other locations', 'woocommerce' ), $option_instance_title );
$options[ $method->get_method_title() ][ $option_id ] = $option_title;
}
@ -175,7 +180,6 @@ class WC_Gateway_COD extends WC_Payment_Gateway {
}
}
}
} elseif ( WC()->cart && WC()->cart->needs_shipping() ) {
$needs_shipping = true;
}
@ -199,7 +203,7 @@ class WC_Gateway_COD extends WC_Payment_Gateway {
$canonical_rate_ids = $this->get_canonical_package_rate_ids( $chosen_shipping_methods_session );
}
if ( ! sizeof( $this->get_matching_rates( $canonical_rate_ids ) ) ) {
if ( ! count( $this->get_matching_rates( $canonical_rate_ids ) ) ) {
return false;
}
}
@ -212,8 +216,8 @@ class WC_Gateway_COD extends WC_Payment_Gateway {
*
* @since 3.4.0
*
* @param array $order_shipping_items Array of WC_Order_Item_Shipping objects.
* @return array $canonical_rate_ids Rate IDs in a canonical format.
* @param array $order_shipping_items Array of WC_Order_Item_Shipping objects.
* @return array $canonical_rate_ids Rate IDs in a canonical format.
*/
private function get_canonical_order_shipping_item_rate_ids( $order_shipping_items ) {
@ -231,18 +235,20 @@ class WC_Gateway_COD extends WC_Payment_Gateway {
*
* @since 3.4.0
*
* @param array $rate_ids Rate IDs as generated by shipping methods. Can be anything if a shipping method doesn't honor WC conventions.
* @return array $canonical_rate_ids Rate IDs in a canonical format.
* @param array $chosen_package_rate_ids Rate IDs as generated by shipping methods. Can be anything if a shipping method doesn't honor WC conventions.
* @return array $canonical_rate_ids Rate IDs in a canonical format.
*/
private function get_canonical_package_rate_ids( $chosen_package_rate_ids ) {
$shipping_packages = WC()->shipping->get_packages();
$canonical_rate_ids = array();
foreach ( $chosen_package_rate_ids as $package_key => $chosen_package_rate_id ) {
if ( ! empty( $shipping_packages[ $package_key ][ 'rates' ][ $chosen_package_rate_id ] ) ) {
$chosen_rate = $shipping_packages[ $package_key ][ 'rates' ][ $chosen_package_rate_id ];
$canonical_rate_ids[] = $chosen_rate->get_method_id() . ':' . $chosen_rate->get_instance_id();
if ( ! empty( $chosen_package_rate_ids ) && is_array( $chosen_package_rate_ids ) ) {
foreach ( $chosen_package_rate_ids as $package_key => $chosen_package_rate_id ) {
if ( ! empty( $shipping_packages[ $package_key ]['rates'][ $chosen_package_rate_id ] ) ) {
$chosen_rate = $shipping_packages[ $package_key ]['rates'][ $chosen_package_rate_id ];
$canonical_rate_ids[] = $chosen_rate->get_method_id() . ':' . $chosen_rate->get_instance_id();
}
}
}
@ -254,7 +260,7 @@ class WC_Gateway_COD extends WC_Payment_Gateway {
*
* @since 3.4.0
*
* @param array $rate_ids
* @param array $rate_ids Rate ids to check.
* @return boolean
*/
private function get_matching_rates( $rate_ids ) {