WordPress.org

Ready to get started?Download WordPress

Forums

WooCommerce - excelling eCommerce
Restrict payment options based on product (27 posts)

  1. emike09
    Member
    Posted 1 year ago #

    Hey all,
    We're selling a variety of items from our store, but certain high-price items we want the buyer to call in to verify the order and arrange payment over the phone.

    Is there a way to specify that certain products, or products in a certain category, only have a single payment option during checkout?

    http://wordpress.org/extend/plugins/woocommerce/

  2. bheadrick
    Member
    Posted 1 year ago #

    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
    }
  3. bheadrick
    Member
    Posted 1 year ago #

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

  4. emike09
    Member
    Posted 1 year ago #

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

  5. bheadrick
    Member
    Posted 1 year ago #

    throw that in your theme's functions.php

  6. emike09
    Member
    Posted 1 year ago #

    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.

  7. jorgesim
    Member
    Posted 1 year ago #

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

  8. bheadrick
    Member
    Posted 1 year ago #

  9. outlierdesign
    Member
    Posted 1 year ago #

    @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;
    }
  10. outlierdesign
    Member
    Posted 1 year ago #

    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. :)

  11. mwq1971
    Member
    Posted 1 year ago #

    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

  12. Bearscape
    Member
    Posted 1 year ago #

    Hi

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

    Along the lines of what emike09 asked.

    Thanks

  13. abdulr007
    Member
    Posted 1 year ago #

    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...

  14. Sean Donovan
    Member
    Posted 1 year ago #

    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. http://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 - http://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.

  15. bheadrick
    Member
    Posted 1 year ago #

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

  16. bazish
    Member
    Posted 1 year ago #

    @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.

  17. rahul.sahni0105
    Member
    Posted 1 year ago #

    Hey Sean Donovan,

    How much will you charge to make this into a plugin. Say we want a box on the products page, where you can select the payment methods available for that product?

  18. Michael Boldin
    Member
    Posted 1 year ago #

    This worked like a charm! Sean, I'd be interested in buying this plugin or paying to have you develop it.

    Here's what I did to remove mijireh and checks, but leave paypal for a specific product:

    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(24);

    if(in_array($values['product_id'],$highpriceditems)){

    unset($gateways['mijireh_checkout']);
    unset($gateways['cheque']);
    break;
    }}
    return $gateways;
    }

  19. Stephen Woodall
    Member
    Posted 1 year ago #

    Anyone have something similar to restrict shipping methods based on a particular product ID/IDs? Essentially I have a few products that require special handling (HAZMAT) and can only be shipped UPS Ground. I'd like to eliminate other shipping options like USPS and UPS non-ground choices. I think adding a filter like above would work but while I'm ok with CSS I'm way over my head with something like this.

    Any help would be much appreciated!!

  20. Michael Boldin
    Member
    Posted 1 year ago #

    Does anyone have advice on how to do a combo - restrict some gateways for some products and others for others? I can't seem to get the proper function down. Here's what I have, and what I want to add below.

    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(24,55);

    if(in_array($values['product_id'],$highpriceditems)){

    unset($gateways['mijireh_checkout']);
    unset($gateways['cheque']);
    unset($gateways['bitpay']);
    unset($gateways['wepay']);
    break;
    }}
    return $gateways;
    }

    *****

    For products 24 and 55, those 4 gateways do NOT show up at checkout. this is good.

    But, I can't figure out how to do something different for product 75. I want to do the following:

    unset($gateways['mijireh_checkout']);
    unset($gateways['paypal']);

    FYI - I'm not worried about all those products in the cart at the same time, because the first 2 are subscription products, they can't be in there together anyway.

  21. Holyto
    Member
    Posted 11 months ago #

    Excellent solution.
    And would it be possible to set the filter for a certain shipping class?

    Is there a way to apply his filter for the order subtotal?

    For example, if the cart subtotal is less that 50, the "COD" option shall be hidden.

    Do you have any idea on how to achieve that?

  22. tiwelle
    Member
    Posted 10 months ago #

    Hello,
    Thank you for the snippet, it's really helpful.
    I'm trying to achieve the same results, but with the following : if and only if there is only 1 category type in the cart, I want to disable on payment.
    I tried :

    function filter_gateways($gateways){
    
    $payment_NAME = 'cheque'; // <--------------- change this
    $category_ID1 = '13';  // <----------- and this
    $category_ID2 = '14';  // <----------- and this
    $category_ID3 = '15';  // <----------- 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_ID1) AND ($term->term_id != $category_ID2)  AND ($term->term_id != $category_ID3) ){
                   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');

    Because I thought that checking if products are not in all category except the one I'm trying to isolate, I would be able to de activate the check method, but it's not working.
    What do you think ?
    Thank you,
    Manue

  23. SRV_nl
    Member
    Posted 10 months ago #

    Is there also a way to make the choice of payment gateways based on shipping method?
    for example, if someone want the shipping "local pickup" i want to exclude gateway "credit card" and visa versa.

    Hope someone can help me with a usefull code snippet

  24. SRV_nl
    Member
    Posted 9 months ago #

    Anyone?
    i just need a usefull push in the right direction.

  25. Marcoscavd
    Member
    Posted 8 months ago #

    Friends

    I wish I could tell which form of payment a particular product will have.

    Example: Product X will have enabled transfer and card "Cielo"
    Product Y card only (Cielo "
    Product W only "Cielo" and "PagSeguro"
    Product Z All forms of payment authorized

    Could anyone help me?

  26. clickconversion
    Member
    Posted 7 months ago #

    Great thread.
    I'd like to accomplish the same based on 'Order Total.'
    i.e. If Order Total is greater than $100, only allow this/these payment options.
    Thanks for your guidance!

  27. esmi
    Forum Moderator
    Posted 7 months ago #

    @clickconversion: If you require assistance then, as per the Forum Welcome, please post your own topic.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic