Skip to content

Commit

Permalink
Added support cart checks to pickup locations.
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisnissle committed Apr 4, 2024
1 parent 6a0d754 commit ffacbd8
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 47 deletions.
35 changes: 14 additions & 21 deletions assets/js/packages/checkout/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,32 +44,25 @@ export const hasPickupLocation = () => {
};

export const getCheckoutData = () => {
return useSelect( ( select ) => {
const store = select( CHECKOUT_STORE_KEY );
const extensionsData = select( CHECKOUT_STORE_KEY ).getExtensionData();

const extensionsData = store.getExtensionData();
const defaultData = {
'pickup_location': '',
'pickup_location_customer_number': ''
};
const defaultData = {
'pickup_location': '',
'pickup_location_customer_number': ''
};

return extensionsData.hasOwnProperty( 'woocommerce-gzd-shipments' ) ? extensionsData['woocommerce-gzd-shipments'] : defaultData;
} );
return extensionsData.hasOwnProperty( 'woocommerce-gzd-shipments' ) ? extensionsData['woocommerce-gzd-shipments'] : defaultData;
};

export const getCartData = () => {
return useSelect( ( select ) => {
const store = select( CART_STORE_KEY );
const extensionsData = select( CART_STORE_KEY ).getCartData().extensions;

const extensionsData = store.getCartData().extensions;
const defaultData = {
'pickup_location_delivery_available': false,
'pickup_locations': [],
'default_pickup_location': '',
'default_pickup_location_customer_number': '',
};

const defaultData = {
'pickup_location_delivery_available': false,
'pickup_locations': [],
'default_pickup_location': '',
'default_pickup_location_customer_number': '',
};

return extensionsData.hasOwnProperty( 'woocommerce-gzd-shipments' ) ? extensionsData['woocommerce-gzd-shipments'] : defaultData;
} );
return extensionsData.hasOwnProperty( 'woocommerce-gzd-shipments' ) ? extensionsData['woocommerce-gzd-shipments'] : defaultData;
};
43 changes: 23 additions & 20 deletions src/Blocks/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,29 +334,32 @@ private function get_cart_schema() {
}

private function get_cart_data() {
$customer = wc()->customer;
$provider = false;
$is_available = false;
$locations = array();
$shipping_method = wc_gzd_get_current_shipping_provider_method();

if ( $shipping_method ) {
$provider = $shipping_method->get_shipping_provider_instance();
}
$customer = wc()->customer;
$provider = false;
$is_available = false;
$locations = array();

if ( $provider && is_a( $provider, '\Vendidero\Germanized\Shipments\Interfaces\ShippingProviderAuto' ) ) {
$address = array(
'postcode' => $customer->get_shipping_postcode(),
'country' => $customer->get_shipping_country(),
'address_1' => $customer->get_shipping_address_1(),
'city' => $customer->get_shipping_city(),
);
if ( PickupDelivery::is_available() ) {
$shipping_method = wc_gzd_get_current_shipping_provider_method();

if ( $shipping_method ) {
$provider = $shipping_method->get_shipping_provider_instance();
}

if ( $provider && is_a( $provider, '\Vendidero\Germanized\Shipments\Interfaces\ShippingProviderAuto' ) ) {
$address = array(
'postcode' => $customer->get_shipping_postcode(),
'country' => $customer->get_shipping_country(),
'address_1' => $customer->get_shipping_address_1(),
'city' => $customer->get_shipping_city(),
);

$query_args = PickupDelivery::get_pickup_delivery_cart_args();
$is_available = $provider->supports_pickup_location_delivery( $address, $query_args );
$query_args = PickupDelivery::get_pickup_delivery_cart_args();
$is_available = $provider->supports_pickup_location_delivery( $address, $query_args );

if ( $is_available ) {
$locations = $provider->get_pickup_locations( $address, $query_args );
if ( $is_available ) {
$locations = $provider->get_pickup_locations( $address, $query_args );
}
}
}

Expand Down
24 changes: 19 additions & 5 deletions src/PickupDelivery.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public static function register_pickup_location_field( $field, $key, $args, $val
* @return void
*/
public static function register_classic_checkout_order_data( $order, $data ) {
if ( ! self::is_enabled() ) {
if ( ! self::is_available() ) {
return;
}

Expand Down Expand Up @@ -175,7 +175,7 @@ public static function register_classic_checkout_order_data( $order, $data ) {
* @return void
*/
public static function register_classic_checkout_validation( $data, $errors ) {
if ( ! self::is_enabled() ) {
if ( ! self::is_available() ) {
return;
}

Expand Down Expand Up @@ -225,7 +225,7 @@ public static function register_classic_checkout_validation( $data, $errors ) {
}

public static function register_order_review_fragments( $fragments ) {
if ( ! self::is_enabled() ) {
if ( ! self::is_available() ) {
return $fragments;
}

Expand Down Expand Up @@ -265,7 +265,7 @@ public static function register_order_review_fragments( $fragments ) {
}

public static function register_classic_checkout_scripts() {
if ( ! is_checkout() || ! wc()->cart->needs_shipping() || ! self::is_enabled() ) {
if ( ! is_checkout() || ! self::is_available() ) {
return;
}

Expand Down Expand Up @@ -365,8 +365,22 @@ public static function is_enabled() {
return apply_filters( 'woocommerce_gzd_shipments_enable_pickup_delivery', true );
}

public static function is_available() {
$available = self::is_enabled();

if ( wc()->cart && ! wc()->cart->needs_shipping() ) {
$available = false;
}

if ( 'billing_only' === get_option( 'woocommerce_ship_to_destination' ) ) {
$available = false;
}

return apply_filters( 'woocommerce_gzd_shipments_pickup_delivery_available', $available );
}

public static function register_classic_checkout_fields( $fields ) {
if ( ! wc()->cart->needs_shipping() || ! self::is_enabled() ) {
if ( ! self::is_available() ) {
return $fields;
}

Expand Down
15 changes: 14 additions & 1 deletion src/ShippingProvider/Auto.php
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ public function is_valid_pickup_location( $location_code, $address ) {
* @param $address
* @param $query_args
*
* @return null
* @return null|PickupLocation[]
*/
protected function fetch_pickup_locations( $address, $query_args = array() ) {
return null;
Expand Down Expand Up @@ -476,6 +476,19 @@ public function get_pickup_locations( $address, $query_args = array() ) {
}
}

/**
* Make sure that (cached) pickup locations support data.
*/
if ( ! empty( $pickup_locations ) ) {
foreach ( $pickup_locations as $k => $pickup_location ) {
if ( ! $pickup_location->supports_cart( $query_args ) ) {
unset( $pickup_locations[ $k ] );
}
}

$pickup_locations = array_values( $pickup_locations );
}

return $pickup_locations;
}

Expand Down
30 changes: 30 additions & 0 deletions src/ShippingProvider/PickupLocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,36 @@ public function supports_customer_number() {
return $this->supports_customer_number;
}

public function supports_cart( $cart_data ) {
$cart_data = wp_parse_args(
$cart_data,
array(
'max_dimensions' => array(),
'max_weight' => 0.0,
)
);

return $this->supports_dimensions( $cart_data['max_dimensions'] ) && $this->supports_weight( $cart_data['max_weight'] );
}

/**
* @param array $dimensions
*
* @return boolean
*/
public function supports_dimensions( $dimensions ) {
return true;
}

/**
* @param $weight
*
* @return boolean
*/
public function supports_weight( $weight ) {
return true;
}

public function customer_number_is_mandatory() {
return $this->customer_number_is_mandatory;
}
Expand Down

0 comments on commit ffacbd8

Please sign in to comment.