Viewing 15 replies - 1 through 15 (of 26 total)
  • You could use a filter to check the items in the cart and then disable payment methods you don’t want to be available if certain items are in the cart.

    add_filter('woocommerce_available_payment_gateways','filter_gateways',1);
    
    function filter_gateways($gateways){
    global $woocommerce;
     foreach ($woocommerce->cart->cart_contents as $key => $values ) {
    //store product id's in array
    $highpriceditems = array(1111,2232,4235);			
    
      if(in_array($values['product_id'],$highpriceditems)){	
    
                                        unset($gateways['paypal']);
    break;
                                    }}
    return $gateways
    }

    you could also filter by category or tag (or some other attribute), but this should be enough to get you started.

    Thread Starter emike09

    (@emike09)

    Sweet I think this is exactly what I’m looking for. What file will I be editing?

    throw that in your theme’s functions.php

    Thread Starter emike09

    (@emike09)

    Seems to break the site. Maybe a theme incompatibility? The front and back-end go completely blank when adding that anywhere into my themes function.php file.

    bheadrick thanks for the code. Could you please also provide the code to filter by product category? Couldn’t work it out myself.
    Thanks

    outlierdesign

    (@outlierdesign)

    @emike09,
    The code provided by bheadrick does work, but there was one typo, which is probably what broke your site. There was a missing semicolon. The code below should do it.

    Change:

    return $gateways

    to:

    return $gateways;

    Giving:

    add_filter('woocommerce_available_payment_gateways','filter_gateways',1);
    
    function filter_gateways($gateways){
    global $woocommerce;
     foreach ($woocommerce->cart->cart_contents as $key => $values ) {
    //store product id's in array
    $highpriceditems = array(1111,2232,4235);			
    
      if(in_array($values['product_id'],$highpriceditems)){	
    
                                        unset($gateways['paypal']);
    break;
                                    }}
    return $gateways;
    }
    outlierdesign

    (@outlierdesign)

    Oh, and @bheadrick, thanks for that code snippet! Exactly what I was looking for and no way in hell I’d ever have been able to come up with it myself. ??

    Hi, thanks for your code, i am looking to use the product category as filter.
    I tried changing product_id by category_id but doesent works. Can you give me some advice about what to change?

    Thanks

    Hi

    I know this thread is old but can I make a small request please . . .

    Along the lines of what emike09 asked.

    Thanks

    Hey bheadrick, is it possible to share the code with the use of categories to filter… We are dying here with cash on deliver option for mobile phones…

    A simple filter to disable a user-specified payment gateway when a product with a user-specified category is added to the shopping cart

    /*
     * A simple filter to disable a user-specified payment gateway when a product with a user-specified category is added to the shopping cart
     *  - Note:  If multiple products are added and only one has a matching category, it will remove the payment gateway
     * Requires:
     *    payment_NAME : One of the five hardcoded Woocommerce standard types of payment gateways - paypal, cod, bacs, cheque or mijireh_checkout
     *    category_ID :   The ID of the category for which the gateway above will be removed.  Get the ID by clicking on the category under Products -> Categories and reading the "tag_ID" in the address bar
     *                             i.e. https://ubuntu.humble.lan/wp-admin/edit-tags.php?action=edit&taxonomy=product_cat&tag_ID=20&post_type=product <-- the tag_ID is 20
     * Coded by sean _ at _ techonfoot.com
     * Thanks to boxoft - https://stackoverflow.com/questions/15303031/woocommerce-get-category-for-product-page
     * Usual free code disclaimer - use at your own risk
     * This code was tested against Woocommerce 2.0.8 and WordPress 3.5.1
     */
    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');

    Add it to your theme’s functions.php

    I will turn it into a plugin for a few $$$ if anybody is interested.

    @sean – that’s great! I had started working on building a plugin that does just that, but got a bit sidetracked. nice work!

    @sean and @bheadrick

    both solutions are great way to get customize Woocommerce, thanks for the codes..

    i have another problem, where i want items from same shipping class to be added to cart, i.e.

    Product A have shipping class SC1
    Product B have Shipping class SC1
    Product C have Shipping class SC2
    Product D have Shipping class SC3

    User added Product A to cart,
    than user added Product C to cart, it should give them message or not allowing them to add this product as it’s shipping class different than the one already in Cart.

    user can only add Product A, Product B to cart for one checkout order.
    Product C to cart for one checkout order.
    and Product D to cart for one checkout order.

    no same shipping class to be checked out in one order.

    i am using WordPress 3.5.1, WooCommerce 2.0.12 and WooCommerce Table Rate Shipping plugin.

Viewing 15 replies - 1 through 15 (of 26 total)
  • The topic ‘Restrict payment options based on product’ is closed to new replies.