Skip to content

Commit

Permalink
Store separate WC blocks checkout selected payment gateway to improve…
Browse files Browse the repository at this point in the history
… gateway-specific pickup delivery allowance checks.
  • Loading branch information
dennisnissle committed Nov 4, 2024
1 parent 3f43ac2 commit fa30741
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 2 deletions.
3 changes: 2 additions & 1 deletion assets/js/blocks/checkout-pickup-location-select/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
import './slotfills/pickup-location-select';
import './slotfills/pickup-location-select';
import './set-payment-method';
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { registerPlugin } from "@wordpress/plugins";
import { useEffect, useState, useCallback, useRef } from '@wordpress/element';
import { useSelect } from '@wordpress/data';
import { extensionCartUpdate } from '@woocommerce/blocks-checkout';
import _ from 'lodash';
import { PAYMENT_STORE_KEY } from '@woocommerce/block-data';

const render = () => {
const {
currentPaymentMethod
} = useSelect( ( select ) => {
const paymentStore = select( PAYMENT_STORE_KEY );

return {
currentPaymentMethod: paymentStore.getActivePaymentMethod(),
}
} );

useEffect( () => {
if ( currentPaymentMethod ) {
extensionCartUpdate( {
namespace: 'woocommerce-gzd-shipments-set-payment-method',
data: {
'active_method': currentPaymentMethod,
},
} );
}
}, [
currentPaymentMethod
] );

return null;
};

registerPlugin( 'woocommerce-gzd-shipments-set-payment-method', {
render,
scope: 'woocommerce-checkout',
} );
11 changes: 11 additions & 0 deletions src/Blocks/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,17 @@ private function register_endpoint_data() {
},
)
);

woocommerce_store_api_register_update_callback(
array(
'namespace' => 'woocommerce-gzd-shipments-set-payment-method',
'callback' => function ( $data ) {
$active_method = isset( $data['active_method'] ) ? wc_clean( wp_unslash( $data['active_method'] ) ) : '';

WC()->session->set( 'wc_gzd_shipments_blocks_chosen_payment_method', $active_method );
},
)
);
}

private function get_checkout_schema() {
Expand Down
11 changes: 11 additions & 0 deletions src/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,17 @@ public static function is_hpos_enabled() {
return \Automattic\WooCommerce\Utilities\OrderUtil::custom_orders_table_usage_is_enabled();
}

public static function get_current_payment_gateway() {
$current_gateway = WC()->session ? WC()->session->get( 'chosen_payment_method' ) : '';
$has_block_checkout = has_block( 'woocommerce/checkout' ) || has_block( 'woocommerce/cart' ) || WC()->is_rest_api_request();

if ( $has_block_checkout ) {
$current_gateway = WC()->session ? WC()->session->get( 'wc_gzd_shipments_blocks_chosen_payment_method', '' ) : '';
}

return $current_gateway;
}

public static function inject_endpoints() {
if ( function_exists( 'WC' ) && WC()->query ) {
foreach ( self::get_endpoints() as $endpoint ) {
Expand Down
13 changes: 12 additions & 1 deletion src/PickupDelivery.php
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,17 @@ public static function register_assets() {
wp_enqueue_script( 'wc-gzd-shipments-pickup-locations' );
}

public static function get_excluded_gateways() {
/**
* Filter to disable pickup delivery for certain gateways.
*
* @param array $gateways Array of gateway IDs to exclude.
*/
$codes = apply_filters( 'woocommerce_gzd_shipments_pickup_delivery_excluded_gateways', array( 'cod', 'amazon_payments_advanced' ) );

return $codes;
}

public static function get_pickup_delivery_cart_args() {
if ( ! wc()->cart ) {
return array(
Expand Down Expand Up @@ -804,7 +815,7 @@ public static function get_pickup_delivery_cart_args() {
return array(
'max_weight' => $max_weight,
'max_dimensions' => $max_dimensions,
'payment_gateway' => WC()->session ? WC()->session->get( 'chosen_payment_method' ) : '',
'payment_gateway' => Package::get_current_payment_gateway(),
'shipping_method' => $shipping_method,
);
}
Expand Down

0 comments on commit fa30741

Please sign in to comment.