Title: code 17
Last modified: February 10, 2023

---

# code 17

 *  Resolved [Luke](https://wordpress.org/support/users/lukestorm/)
 * (@lukestorm)
 * [3 years, 4 months ago](https://wordpress.org/support/topic/code-17-2/)
 * This code snippet is failing, but has been working for over a year no issue until
   now. has there been any changes with code snippet? i have replaced the domain
   with DOMAINNAME. this error stops the cron job **action_scheduler_run_queue**
   from working.
 * any ideas?
 * ‘PHP message: PHP Fatal error: Uncaught Error: Call to a member function get()
   on null in /home/DOMAINNAME.com.au/htdocs/wp-content/plugins/code-snippets/php/
   snippet-ops.php(505) : eval()’d code:17\nStack trace:\n#0 /home/DOMAINNAME.com.
   au/htdocs/wp-includes/class-wp-hook.php(308): bbloomer_gateway_disable_shipping_326()\
   n#1 /home/DOMAINNAME.com.au/htdocs/wp-includes/plugin.php(205): WP_Hook->apply_filters()\
   n#2 /home/DOMAINNAME.com.au/htdocs/wp-content/plugins/woocommerce/includes/class-
   wc-payment-gateways.php(163): apply_filters()\n#3 /home/DOMAINNAME.com.au/htdocs/
   wp-content/plugins/woocommerce-paypal-payments/modules/ppcp-wc-gateway/services.
   php(1322): WC_Payment_Gateways->get_available_payment_gateways()\n#4 /home/DOMAINNAME.
   com.au/htdocs/wp-content/plugins/woocommerce-paypal-payments/lib/packages/Dhii/
   Container/DelegatingContainer.php(117): WooCommerce\PayPalCommerce\WcGateway\
   WCGatewayModule::WooCommerce\PayPalCommerce\WcGateway\{closure}()\n#5 /home/DOMAINNAME.
   com.au/
 * Following is the code that has been working fine.
 *     ```wp-block-code
       /**
        * @snippet       Disable Payment Gateway For Specific Shipping Method
        * @how-to        Watch tutorial @ https://businessbloomer.com/?p=19055
        * @sourcecode    https://businessbloomer.com/?p=19867
        * @author        Rodolfo Melogli
        * @testedwith    WooCommerce 3.2.6
        */
   
       if(!is_admin() ) add_filter( 'woocommerce_available_payment_gateways', 'bbloomer_gateway_disable_shipping_326' );
   
       function bbloomer_gateway_disable_shipping_326( $available_gateways ) {
   
           global $woocommerce;
   
           if ( !is_admin() ) {
   
               $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
   
               $chosen_shipping = $chosen_methods[0];
   
       if ( isset( $available_gateways['ppcp-gateway'] ) && 0 === strpos( $chosen_shipping, 'local_pickup' ) ) {
          unset( $available_gateways['ppcp-gateway'] );
       }
        if ( isset( $available_gateways['afterpay'] ) && 0 === strpos( $chosen_shipping, 'local_pickup' ) ) {
         unset( $available_gateways['afterpay'] );
       }
        if ( isset( $available_gateways['klarna_payments'] ) && 0 === strpos( $chosen_shipping, 'local_pickup' ) ) {
         unset( $available_gateways['klarna_payments'] );
       }
       	   if ( isset( $available_gateways['cheque'] ) && 0 === strpos( $chosen_shipping, 'ozpost' ) ) {
         unset( $available_gateways['cheque'] );
       }
       	  	   if ( isset( $available_gateways['cheque'] ) && 0 === strpos( $chosen_shipping, 'wf_australia_post' ) ) {
         unset( $available_gateways['cheque'] );
       }
       	  	  	   if ( isset( $available_gateways['cheque'] ) && 0 === strpos( $chosen_shipping, 'wf_australia_mypost' ) ) {
         unset( $available_gateways['cheque'] );
       }
   
       	}
   
       return $available_gateways;
   
       }
       ```
   

Viewing 1 replies (of 1 total)

 *  Plugin Author [Shea Bunge](https://wordpress.org/support/users/bungeshea/)
 * (@bungeshea)
 * [3 years, 4 months ago](https://wordpress.org/support/topic/code-17-2/#post-16457987)
 * Hi [@lukestorm](https://wordpress.org/support/users/lukestorm/),
 * From the looks of things, it seems like something about WooCommerce may have 
   changed, because the `WC()->session` is apparently set to `null` instead of the
   class object it was previously.
 * Unfortunately, I’m not a WooCommerce expert so I cannot say for sure why this
   is occuring. You could add a check to ensure that `WC()->session` is set before
   attempting to access the object, but while this will prevent the code from cashing,
   it still won’t work correctly unless you update the snippet:
 *     ```wp-block-code
       <?php
   
       /**
        * @snippet       Disable Payment Gateway For Specific Shipping Method
        * @how-to        Watch tutorial @ https://businessbloomer.com/?p=19055
        * @sourcecode    https://businessbloomer.com/?p=19867
        * @author        Rodolfo Melogli
        * @testedwith    WooCommerce 3.2.6
        */
   
       if ( ! is_admin() ) {
       	add_filter( 'woocommerce_available_payment_gateways', 'bbloomer_gateway_disable_shipping_326' );
       }
   
       function bbloomer_gateway_disable_shipping_326( $available_gateways ) {
       	global $woocommerce;
   
       	if ( ! is_admin() && WC()->session ) {
   
       		$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
       		$chosen_shipping = $chosen_methods[0];
   
       		if ( isset( $available_gateways['ppcp-gateway'] ) && 0 === strpos( $chosen_shipping, 'local_pickup' ) ) {
       			unset( $available_gateways['ppcp-gateway'] );
       		}
       		if ( isset( $available_gateways['afterpay'] ) && 0 === strpos( $chosen_shipping, 'local_pickup' ) ) {
       			unset( $available_gateways['afterpay'] );
       		}
       		if ( isset( $available_gateways['klarna_payments'] ) && 0 === strpos( $chosen_shipping, 'local_pickup' ) ) {
       			unset( $available_gateways['klarna_payments'] );
       		}
       		if ( isset( $available_gateways['cheque'] ) && 0 === strpos( $chosen_shipping, 'ozpost' ) ) {
       			unset( $available_gateways['cheque'] );
       		}
       		if ( isset( $available_gateways['cheque'] ) && 0 === strpos( $chosen_shipping, 'wf_australia_post' ) ) {
       			unset( $available_gateways['cheque'] );
       		}
       		if ( isset( $available_gateways['cheque'] ) && 0 === strpos( $chosen_shipping, 'wf_australia_mypost' ) ) {
       			unset( $available_gateways['cheque'] );
       		}
   
       	}
   
       	return $available_gateways;
       }
       ```
   

Viewing 1 replies (of 1 total)

The topic ‘code 17’ is closed to new replies.

 * ![](https://ps.w.org/code-snippets/assets/icon.svg?rev=2148878)
 * [Code Snippets](https://wordpress.org/plugins/code-snippets/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/code-snippets/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/code-snippets/)
 * [Active Topics](https://wordpress.org/support/plugin/code-snippets/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/code-snippets/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/code-snippets/reviews/)

 * 1 reply
 * 2 participants
 * Last reply from: [Shea Bunge](https://wordpress.org/support/users/bungeshea/)
 * Last activity: [3 years, 4 months ago](https://wordpress.org/support/topic/code-17-2/#post-16457987)
 * Status: resolved