• Resolved dwdonline

    (@dwdonline)


    Hello, for most customers, going through the shipping step first, makes sense. However, we need to force customers who choose cheque as the payment method to only be able to choose local pickup as their shipping method. With the regular checkout, we used this code:

    add_action('woocommerce_review_order_before_shipping', 'show_check_for_local_pickup_only', 100, 2);
    function show_check_for_local_pickup_only() {
    	add_filter( 'woocommerce_available_payment_gateways', 'dwd_enable_checks_local_pickup' );
    	function dwd_enable_checks_local_pickup( $available_gateways ) {
    	   if ( ! is_admin() ) {
    	      $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
    	      $chosen_shipping = $chosen_methods[0];
    	              if ( isset( $available_gateways['cheque'] ) && 0 === strpos( $chosen_shipping, 'local_pickup' ) ) {
    	         isset( $available_gateways['cheque'] );
    	      } else {
    	      	unset( $available_gateways['cheque'] );
    	      }
    	   }
    	   return $available_gateways;
    	}
    }
    

    However, that no longer works when it is in steps. Is there a way to force them to choose the shipping method again if they choose cheque as the payment method?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter dwdonline

    (@dwdonline)

    In the alternative, if we could hide all payment methods except check if they select the local pickup as the shipping.

    Plugin Author Diego Versiani

    (@diegoversiani)

    Hi @dwdonline,

    Currently, the payment step has to be the last step of the checkout process due to the way the section is updated when the customer goes through the other fields.

    Depending on the payment method plugin, and most of them do, the payment details are lost during checkout updates.

    The action hook you are using to add the customization woocommerce_review_order_before_shipping is probably running too late. That action hook is intended to output extra content right before the shipping costs in the order summary.

    It worked before because the native WooCommerce checkout displays the shipping method options right after that hook had run.

    It seems you can simply hook directly into the filter woocommerce_available_payment_gateways. Here is a modified code snippet that will only allow cheque payments for local pickup shipping methods:

    /**
     * Only allow check payments for local pickup.
     */
    function fluidcheckout_enable_check_payment_only_local_pickup( $available_gateways ) {
    	// Bail if visiting admin pages
    	if ( is_admin() && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) ) { return $available_gateways; }
    	
    	$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
    	$chosen_shipping = $chosen_methods[0];
    	if ( array_key_exists( 'cheque', $available_gateways ) && 0 !== strpos( $chosen_shipping, 'local_pickup' ) ) {
    		unset( $available_gateways['cheque'] );
    	}
    	
    	return $available_gateways;
    }
    add_filter( 'woocommerce_available_payment_gateways', 'fluidcheckout_enable_check_payment_only_local_pickup' );

    This code snippet also works with native WooCommerce checkout, without Fluid Checkout.

    Check our article “How to safely add code snippets to your WooCommerce website” in the link below:
    https://support.fluidcheckout.com/portal/en/kb/articles/how-to-add-code-snippets

    I’m closing this topic for now. If you need further assistance, please reply to this topic.

    Best,
    Diego

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Change shipping based on payment’ is closed to new replies.