Home > Enterprise >  Hide woocmmerce payment gateways if user comes from given page on wordpress site
Hide woocmmerce payment gateways if user comes from given page on wordpress site

Time:07-24

Am trying to hide paypal payment gateway for a digital product. For some complex reason I have to hide the payment gateway not entirely for the product, but if the user proceeds to checkout from another page (referrer page). Here is the code I tried to put together, but it doesnot hide the gateway. Any assistance will be appretiated.

//hide paypal if user access checkout from "send money" page
add_filter( 'woocommerce_available_payment_gateways', 'dealion_payment_gateway_disable_onpage' );
function dealion_payment_gateway_disable_onpage( $available_gateways ) {
$referer=wp_get_referer();
var_dump($referer);

if ( isset( $available_gateways['paypal'] ) && $referer== "https://www.myshop.store/send-money/") {
  unset( $available_gateways['paypal'] );

}
return $available_gateways;
}

CodePudding user response:

You can try using the WC()->session and store a custom variable so that it doesn't clear your referrer.

add_filter( 'woocommerce_available_payment_gateways', 'dealion_payment_gateway_disable_onpage' );
function dealion_payment_gateway_disable_onpage( $available_gateways ) {
    // Don't check gateways on Admin.
    if ( is_admin() ) {
        return $available_gateways;
    }
    if ( null === WC()->session->get( 'dealion_referer' ) ) {
        // Get the Referer and Store in a WC Session Var.
        $referer = wp_get_referer();
        WC()->session->set( 'dealion_referer', $referer );
    } else {
        $referer = WC()->session->get( 'dealion_referer' );
    }
    if ( isset( $available_gateways['paypal'] ) && 'https://www.myshop.store/send-money/' === $referer ) {
        unset( $available_gateways['paypal'] );
    }
    return $available_gateways;
}
  • Related