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' );
        }    
       
        
      }
     
        
     }
     
     
    
    
}
    

}

Advertisements

2 thoughts on “WooCommerce: Product setup fee for 1st purchase

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s