diff --git a/assets/js/blocks/checkout-pickup-location-select/index.js b/assets/js/blocks/checkout-pickup-location-select/index.js index f6f2341..0b74b59 100644 --- a/assets/js/blocks/checkout-pickup-location-select/index.js +++ b/assets/js/blocks/checkout-pickup-location-select/index.js @@ -1 +1,2 @@ -import './slotfills/pickup-location-select'; \ No newline at end of file +import './slotfills/pickup-location-select'; +import './set-payment-method'; \ No newline at end of file diff --git a/assets/js/blocks/checkout-pickup-location-select/set-payment-method.js b/assets/js/blocks/checkout-pickup-location-select/set-payment-method.js new file mode 100644 index 0000000..6a68e19 --- /dev/null +++ b/assets/js/blocks/checkout-pickup-location-select/set-payment-method.js @@ -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', +} ); \ No newline at end of file diff --git a/src/Blocks/Checkout.php b/src/Blocks/Checkout.php index 74fe258..6cd47fe 100644 --- a/src/Blocks/Checkout.php +++ b/src/Blocks/Checkout.php @@ -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() { diff --git a/src/Package.php b/src/Package.php index 0ac6280..8eba0d9 100644 --- a/src/Package.php +++ b/src/Package.php @@ -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 ) { diff --git a/src/PickupDelivery.php b/src/PickupDelivery.php index 1184101..00c0818 100644 --- a/src/PickupDelivery.php +++ b/src/PickupDelivery.php @@ -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( @@ -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, ); }