WooCommerce: Restrict payment options based on product

A simple filter to disable a user-specified payment gateway when a product with a user-specified category is added to the shopping cart. Add it to your theme’s functions.php

/*
 * 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.1.12  and WordPress 3.9.1 
 */
function filter_gateways($gateways){

$payment_NAME = 'dibs'; // <--------------- change this
$category_ID = '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_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');

 

 

WooCommerce: Display Price For Variable Product With Same Variations Prices

// Display Price For Variable Product With Same Variations Prices
add_filter('woocommerce_available_variation', function ($value, $object = null, $variation = null) {
    if ($value['price_html'] == '') {
        $value['price_html'] = '<span class="price">' . $variation->get_price_html() . '</span>';
    }
    return $value;
}, 10, 3);

WooCommerce: Product setup fee for 1st purchase

If  you want to add additional set-up fee for 1st time purchase this code will do that. If any customer want to buy any product for 1st time he/she need to pay additional set-up fee for that product. There will be not additional charge for repeat purchase.

 

 

 

Add this code in your function.php

/**
 * Uses the WooCommerce fees API
 *
 * Add to theme functions.php
 */
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
  global $woocommerce;

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $current_user = wp_get_current_user();
        
        if ( 0 == $current_user->ID ) { // User not login
        
    $surcharge  = 10;
    
        $car_item = $woocommerce->cart->get_cart_item_quantities( ); // get wooCommerce current cart items
    $product_ids = array_keys($car_item); // cart item product ids
    
    foreach($product_ids as $product_id)
     {
     $term_list = wp_get_post_terms($product_id,'product_cat',array('fields'=>'ids')); // Check product category
     $cat_id = (int)$term_list[0]; // product category id
     $product_name = get_the_title($product_id); // Product name
     
     if($cat_id == 18 || $cat_id == 21 || $cat_id == 22) // Matching product category
      {
        
        $woocommerce->cart->add_fee( 'Setup fee for '.$product_name, $surcharge, true, 'standard' );
        
      }
     
        
     }
    
    } else {
    // Logged in.
     $customer_id = $current_user->ID;
     $car_item = $woocommerce->cart->get_cart_item_quantities( );
     $product_ids = array_keys($car_item);
     
     /*
     * Get all oder ids of current user
     *
     */
     $args = array(
        'numberposts'     => -1,
        'meta_key'        => '_customer_user',
        'meta_value'      => $customer_id ,
        'post_type'       => 'shop_order',
        'post_status'     => 'publish',
        /*'tax_query'=>array(
                array(
                    'taxonomy'  =>'shop_order_status',
                    'field'     => 'slug',
                    'terms'     =>$status
                    )
        ) */ 
    );
    
    $posts=get_posts($args);
    //get the post ids as order ids
    $orders=wp_list_pluck( $posts, 'ID' );
    
    /*
    *
    * Get all products id purchased by current user
    */
    
    foreach($orders as $order)
      {
    $order = new WC_Order( $order );
        $items = $order->get_items();
    
      foreach ( $items as $item ) {
        
        $p_id[] = $item['product_id'];
        
        }
    
      }
      
     $p_id = array_unique($p_id); 
    foreach($product_ids as $product_id)
     {
     $term_list = wp_get_post_terms($product_id,'product_cat',array('fields'=>'ids'));
     $cat_id = (int)$term_list[0];
     $product_name = get_the_title($product_id);
     
     if($cat_id == 18 || $cat_id == 21 || $cat_id == 22)
      {
       
        if (!in_array($product_id, $p_id )) {
        $surcharge = 10;
         $woocommerce->cart->add_fee( 'Setup fee for '.$product_name, $surcharge, true, 'standard' );
        }    
       
        
      }
     
        
     }
     
     
    
    
}
    

}

WooCommerce: Add a surcharge to cart and checkout – uses fees API

Add a basic surcharge to all transactions

/**
 * Add a 1% surcharge to your cart / checkout
 * change the $percentage to set the surcharge to a value to suit
 * Uses the WooCommerce fees API
 *
 * Add to theme functions.php
 */
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
  global $woocommerce;

	if ( is_admin() && ! defined( 'DOING_AJAX' ) )
		return;

	$percentage = 0.01;
	$surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;	
	$woocommerce->cart->add_fee( 'Surcharge', $surcharge, true, 'standard' );

}


Source: http://docs.woothemes.com/document/add-a-surcharge-to-cart-and-checkout-uses-fees-api/

Add a surcharge based on the delivery country

/**
 * Add a 1% surcharge to your cart / checkout based on delivery country
 * Taxes, shipping costs and order subtotal are all included in the surcharge amount
 *
 * Change $percentage to set the surcharge to a value to suit
 *
 * Add countries to array('US'); to include more countries to surcharge
 * http://en.wikipedia.org/wiki/ISO_3166-1#Current_codes for available alpha-2 country codes 
 *
 * Change in_array to !in_array to EXCLUDE the $countries array from surcharges
 *
 * Uses the WooCommerce fees API
 * Add to theme functions.php
 */
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
  global $woocommerce;
 
	if ( is_admin() && ! defined( 'DOING_AJAX' ) )
		return;

 	$county 	= array('US');
	$percentage 	= 0.01;

	if ( in_array( $woocommerce->customer->get_shipping_country(), $county ) ) :
		$surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;
		$woocommerce->cart->add_fee( 'Surcharge', $surcharge, true, 'standard' );
	endif;
 
}

WooCommerce: One item in cart at a time

If  you want to allow only one item in cart at a time, this code will help you. It will remove previously added product from cart and will add latest one.

Place this code on theme function.php page.

add_filter( 'woocommerce_add_cart_item_data', 'woo_custom_add_to_cart' );

function woo_custom_add_to_cart( $cart_item_data ) {

    global $woocommerce;
    $woocommerce->cart->empty_cart();

    // Do nothing with the data and return
    return $cart_item_data;
}

Display category image on category archive

To show product category image on category listing(Archive) page place this code in your theme function.php

add_action( 'woocommerce_archive_description', 'woocommerce_category_image', 2 );
function woocommerce_category_image() {
    if ( is_product_category() ){
	    global $wp_query;
	    $cat = $wp_query->get_queried_object();
	    $thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
	    $image = wp_get_attachment_url( $thumbnail_id );
	    if ( $image ) {
		    echo '<img src="' . $image . '" alt="" />';
		}
	}
}