• I would like to restrict the payment gateways for downloadable products. Now a customer can order a downloadable product and pay by COD. Even if the payment isn’t made, the customer can go to his account en download the product.

    The option to let customers download direct after the checkout have to stay.

    In short,
    – Payment Gateways should be limited for downloadable products (only iDeal by Mollie and no COD, Cheque, Bacs).
    – Direct download after the checkout page have to remain.

    I found this code somewhere which restrict ‘categories’, i’m trying to shape it to my needs. But since i’m not an expert, i definitely need some help 🙂

    function filter_gateways($gateways){
    
    $payment_NAME = 'paypal'; // <--------------- change this
    $category_ID = '20';  // <----------- and this
    
     global $woocommerce;
    
     foreach ($woocommerce->cart->cart_contents as $key => $values ) {
    	// Get the terms, i.e. category list using the ID of the product
    	$terms = get_the_terms( $values['product_id'], 'product_cat' );
    	// Because a product can have multiple categories, we need to iterate through the list of the products category for a match
    	foreach ($terms as $term) {
    		// 20 is the ID of the category for which we want to remove the payment gateway
    		if($term->term_id == $category_ID){
                   unset($gateways[$payment_NAME]);
                       // If you want to remove another payment gateway, add it here i.e. unset($gateways['cod']);
    					break;
              }
    	    break;
    	}
    
       }
    	return $gateways;
    
    }
    
    add_filter('woocommerce_available_payment_gateways','filter_gateways');

    If somebody could help it would be great!

    Thanks anyway!

Viewing 4 replies - 1 through 4 (of 4 total)
  • I recommend asking at https://wordpress.org/support/plugin/woocommerce#postform so the plugin’s developers and support community can help you with this.

    Thread Starter Mikos91

    (@mikos91)

    Thanks for your reply. Good idea! I’ll be back when i have got the answer.

    Thread Starter Mikos91

    (@mikos91)

    After some struggling i have managed to put in an extra option with every payment gateway. This way i can check/uncheck which gateways there can be used.

    I took the file ‘class-wc-gateway-bacs.php’ and added this code to it:

    'enable_for_virtual' => array(
    				'title'             => __( 'Enable for virtual orders', 'woocommerce' ),
    				'label'             => __( 'Enable COD if the order is virtual', 'woocommerce' ),
    				'type'              => 'checkbox',
    				'default'           => 'yes'
    			)

    and this:

    /**
    	 * Check If The Gateway Is Available For Use
    	 *
    	 * @return bool
    	 */
    	public function is_available() {
    		$order = null;
    
    		if ( ! $this->enable_for_virtual ) {
    			if ( WC()->cart && ! WC()->cart->needs_shipping() ) {
    				return false;
    			}
    
    			if ( is_page( wc_get_page_id( 'checkout' ) ) && 0 < get_query_var( 'order-pay' ) ) {
    				$order_id = absint( get_query_var( 'order-pay' ) );
    				$order    = wc_get_order( $order_id );
    
    				// Test if order needs shipping.
    				$needs_shipping = false;
    
    				if ( 0 < sizeof( $order->get_items() ) ) {
    					foreach ( $order->get_items() as $item ) {
    						$_product = $order->get_product_from_item( $item );
    
    						if ( $_product->needs_shipping() ) {
    							$needs_shipping = true;
    							break;
    						}
    					}
    				}
    
    				$needs_shipping = apply_filters( 'woocommerce_cart_needs_shipping', $needs_shipping );
    
    				if ( $needs_shipping ) {
    					return false;
    				}
    			}
    		}
    
    		if ( ! empty( $this->enable_for_methods ) ) {
    
    			// Only apply if all packages are being shipped via local pickup
    			$chosen_shipping_methods_session = WC()->session->get( 'chosen_shipping_methods' );
    
    			if ( isset( $chosen_shipping_methods_session ) ) {
    				$chosen_shipping_methods = array_unique( $chosen_shipping_methods_session );
    			} else {
    				$chosen_shipping_methods = array();
    			}
    
    			$check_method = false;
    
    			if ( is_object( $order ) ) {
    				if ( $order->shipping_method ) {
    					$check_method = $order->shipping_method;
    				}
    
    			} elseif ( empty( $chosen_shipping_methods ) || sizeof( $chosen_shipping_methods ) > 1 ) {
    				$check_method = false;
    			} elseif ( sizeof( $chosen_shipping_methods ) == 1 ) {
    				$check_method = $chosen_shipping_methods[0];
    			}
    
    			if ( ! $check_method ) {
    				return false;
    			}
    
    			$found = false;
    
    			foreach ( $this->enable_for_methods as $method_id ) {
    				if ( strpos( $check_method, $method_id ) === 0 ) {
    					$found = true;
    					break;
    				}
    			}
    
    			if ( ! $found ) {
    				return false;
    			}
    		}
    
    		return parent::is_available();
    	}

    Only problem i’m having now is to save this file in my child-theme and override the original. But sinds it’s a woocommerce plugin i haven’t figured out where to put this file.

    Does somebody know how to save a woocommerce file in a child-theme? Original path:

    woocommerce -> includes -> gateways -> bacs -> 'class-wc-gateway-bacs.php'

    This is indeed a challenging problem, how to customise a plugin and not be hopelessly locked out of updates and maintenance. With themes there is the child theme mechanism which handles this issue. Unfortunately there are no child-plugins (nor are there grandchild themes).

    your best chance is that the team at woo have anticipated your need and are using a filter to let you qualify the list of payment methods, in their code they will call the filter and proceed with the results returned. Then you put your code into the “functions.php” file of your child theme, and hook by name the filter provided by woo.

    So you need to read the woo code and try and find any hooks.

    If you don’t find a hook, then the most elegant and least impactful thing to do is add you own call to a hooked function, it will be very easy to reinstall this change if it ever gets lost. Just find the place in the woo plugin and add a call to your newly named filter.
    Then get back to the support forum at woo, show and explain what you have done, and ask for this to be incorporated into subsequent releases.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Woocommerce, payment gateways for downloads’ is closed to new replies.