Home Forums WC Vendors Pro Support A solution for per order shipping fee

NOTICE: We've Moved to a Ticket System for Support

As of August 31, 2017 (12am EST) our support forums will be retired (read-only), and we will be moving to a support ticket system.  This will allow us to better organize and answer support requests, and provide a more personalized experience as we assist our customers.

For the time being, we will leave our forums open for reading and learning while we work on creating a more robust Knowledge Base for everyone to use.

If you are a WC Vendors Pro customer please open a support ticket here. 

If you are a WC Vendors user please open a support ticket on the Wordpress.org forums.

The information on this forum is outdated and in most instances no longer relevant. Please be sure to check our documentation for the most up to date information.

https://docs.wcvendors.com/

Thank you to all of our customers!

 

Viewing 18 posts - 1 through 18 (of 18 total)
  • Author
    Posts
  • #47690
    Marc Bovet
    Participant

    Hello,

    Here is my solution for the following scenario :
    Vendor front end
    Vendor can choose the minimum order amount.
    Vendor can choose the shipping fees for an order.
    Vendor can offer shipping when a certain amount order amount is reached.

    Customer
    Vendor minimum amount not reached : a notice is displayed.
    Free shipping is not reached : a notice with the remaining amount is displayed
    Free shipping is reached : local pickup and wc vendors fees are hidden.

    First we need to create our custom fields for the vendor to be displayed on the front end

    /**
     * @snippet       Add custom shipping options for vendor
     * @author        Marc Bovet
     * @testedwith    WooCommerce 2.6.8
     * @date		  14.11.2016
     */
     function WC_Custom_Vendor_Shipping_Options( ){ //call WC_Custom_Vendor_Shipping_Options() in template "store-settings.php"
    	if ( class_exists( 'WCVendors_Pro' ) ){ 
    	
    		//Allow vendor to set a minimum order amount (0=> disabled.)
    		$key = 'wcv_custom_settings_minimum_order_amount'; 
    		$value = get_user_meta( get_current_user_id(), $key, true ); 
    	
    		WCVendors_Pro_Form_Helper::input( array(  
    			'id' 				=> $key, 
    			'label' 			=> __( 'Minimum order amount', 'wcvendors-pro' ), 
    			'placeholder' 			=> __( 'Orders below this amount are not allowed by the vendor', 'wcvendors-pro' ), 
    			'desc_tip' 			=> 'true', 
    			'description' 			=> __( 'Set the minimum oder amount', 'wcvendors-pro' ), 
    			'type' 				=> 'number', 
    			'value'				=> $value, 
    			)
    		);
    		
    		//Allow vendor to set shipping fees for an order 
    		$key = 'wcv_custom_settings_shipping_fees'; 
    		$value = get_user_meta( get_current_user_id(), $key, true ); 
    	
    		WCVendors_Pro_Form_Helper::input( array(  
    			'id' 				=> $key, 
    			'label' 			=> __( 'Shipping fees', 'wcvendors-pro' ), 
    			'placeholder' 			=> __( 'Shipping fees set bythe vendor for 1 order', 'wcvendors-pro' ), 
    			'desc_tip' 			=> 'true', 
    			'description' 			=> __( 'Set shipping fees', 'wcvendors-pro' ), 
    			'type' 				=> 'number', 
    			'value'				=> $value, 
    			)
    		);		
    		
    		//Allow vendor to set a minimal amount to get FREE shipping
    		$key = 'wcv_custom_settings_free_shipping_amount'; 
    		$value = get_user_meta( get_current_user_id(), $key, true ); 
    	
    		WCVendors_Pro_Form_Helper::input( array(  
    			'id' 				=> $key, 
    			'label' 			=> __( 'Free shipping starts', 'wcvendors-pro' ), 
    			'placeholder' 			=> __( 'Order amount to allow free shipping from the vendor', 'wcvendors-pro' ), 
    			'desc_tip' 			=> 'true', 
    			'description' 			=> __( 'Set the order amount for free shippping', 'wcvendors-pro' ), 
    			'type' 				=> 'number', 
    			'value'				=> $value, 
    			)
    		);		
    		
    	
    	} 
    } //end function WC_Custom_Vendor_Shipping_Options
    #47691
    Marc Bovet
    Participant

    Then we need to inform our customer with messages

    
    /**
     * @snippet       WooCommerce: Define Minimum Order Amount & Show Errors
     * @sourcecode    https://businessbloomer.com/?p=19947
     * @author        Rodolfo Melogli + Marc Bovet
     * @testedwith    WooCommerce 2.5.2
     * @date		  14.11.2016
     */
     
    add_action( 'woocommerce_checkout_process', 'gastronomy_wc_minimum_order_amount' );
    add_action( 'woocommerce_before_cart' , 'gastronomy_wc_minimum_order_amount' );
     function gastronomy_wc_minimum_order_amount() {
    	
    	//if cart is empty, there is nothing to display.
    	if ( WC()->cart->get_cart_contents_count() == 0 ) {
    			return;
    	}	
    	
    	//TODO:
    	//minimum order amount is not required for local pickup
    	//return from here.
    	
    	// CAUTION
    	// !!An order can only have products from 1 and only 1 vendor !!
    	// get first product of the cart => then we can get vendor ID	
    	$items = WC()->cart->get_cart();
    	foreach($items as $item => $values) {
    		$_product[] = $values['data']->post;
    	}	
    	
    	$vendor_id = get_post_field( 'post_author', $_product[0]->ID);
    	
    	//get minimum order amount
    	$minimum = get_user_meta( $vendor_id, 'wcv_custom_settings_minimum_order_amount', true ); ;
    	
    	//Minimum order amount
        //Inform user that a minimum order amount is required.
        if ( WC()->cart->subtotal < $minimum ) {
    			$text = sprintf( __('You must have a minimum order amount of %s to place your order. Your current order total is %s.') , 
                        wc_price( $minimum ), 
                        wc_price( WC()->cart->subtotal )
                );
    	
            if( is_cart() ) {
                wc_print_notice($text,'error');
     
            } else {
     
                wc_add_notice( $text,'error');
     
            }
    		return; //comment to display remaining amount to get free shipping.
        }
    	//End minimum amount.
    
    	
    	//Show remaining amount to reach free shipping
    	$minimum_amount_free_shipping = get_user_meta( $vendor_id, 'wcv_custom_settings_free_shipping_amount', true ); ;
    	$delta = $minimum_amount_free_shipping -  WC()->cart->subtotal;
        if ( $delta>0) {
    		
    		
    		$text =  sprintf( __('You only need to spend %s to get FREE shipping !') ,wc_price($delta) );
    				
            if( is_cart() ) {
    			
                wc_print_notice($text,'notice');
    			
             } else {
     
                wc_add_notice($text,'notice');
     
            }
        }
    	//End amount free shipping	
    	
     
    }
    #47692
    Marc Bovet
    Participant

    Enable the free shipping fees when a certain amount is reached.

    /**
     * @snippet       WooCommerce : enable free shipping
     * @author        Marc Bovet
     * @testedwith    WooCommerce 2.6.8
     * @date		  15.11.2016
     */
    //Enable free shipping if minimum amount is reached.
    //Free shipping method need to be enabled from the backend, please read this link : https://docs.woocommerce.com/document/free-shipping/
    // !! caution, see point 7 of weblink. amount value need to be set : N/A – Not available, Free Shipping is an option for all customers
    add_filter( 'woocommerce_shipping_free_shipping_is_available', 'custom_free_per_class' );
    function custom_free_per_class( $is_available) {
    
    	//if cart is empty, there is no free shipping available.
    	if ( WC()->cart->get_cart_contents_count() == 0 ) {
    			return false;
    	}
    
    	// CAUTION
    	// !!An order can only have products from 1 and only 1 vendor !!
    	// get first product of the cart => then we can get vendor ID	
    	$items = WC()->cart->get_cart();
    	foreach($items as $item => $values) {
    		$_product[] = $values['data']->post;
    	}	
    	
    	$vendor_id = get_post_field( 'post_author', $_product[0]->ID);
    	
    	//get minimum order amount for free shipping
    	$minimum = get_user_meta( $vendor_id, 'wcv_custom_settings_free_shipping_amount', true ); ;
    	
    
    	if ( WC()->cart->subtotal >= $minimum ) {
    		return true;
    	}
    	else{
    		return false;
    	}
        }//function
    #47693
    Marc Bovet
    Participant

    When free shipping is available, hide other methods.

    
    /**
     * @snippet       WooCommerce : hide all shipping methods when free is available.
     * @sourcecode   https://gist.github.com/mikejolley/11171530
     * @testedwith    WooCommerce 2.6.8
     * @date		  15.11.2016
     */
    add_filter( 'woocommerce_package_rates', 'adjust_shipping_rates', 10, 2 );
     
    /**
     * Hide shipping rates when free shipping is available
     *
     * @param array $rates Array of rates found for the package
     * @param array $package The package array/object being shipped
     * @return array of modified rates
     */
    function adjust_shipping_rates( $rates, $package ) {
     	
     	// Only hide rates if free_shipping is present
      	if ( isset( $rates['free_shipping:9'] ) ) { //use firebug to get the exact name for free shipping.
      	
      		// To unset a single rate/method, do the following. This example unsets flat_rate shipping
      		unset( $rates['local_pickup:5'] );
    		unset( $rates['wcv_pro_vendor_shipping'] );
      		
    	}
    	else //adjust vendor shipping cost with required value.
    	{
    		// CAUTION
    		// !!An order can only have products from 1 and only 1 vendor !!
    		// get first product of the cart => then we can get vendor ID	
    		$items = WC()->cart->get_cart();
    		foreach($items as $item => $values) {
    			$_product[] = $values['data']->post;
    		}	
    	
    		$vendor_id = get_post_field( 'post_author', $_product[0]->ID);
    	
    		//get shipping fees from vendor.
    		$fee = get_user_meta( $vendor_id, 'wcv_custom_settings_shipping_fees', true ); ;
    		
    		//update shipping fees with vendor shipping fees.
    		if(isset($rates['wcv_pro_vendor_shipping'])){
    			$rates['wcv_pro_vendor_shipping']->cost = $fee;
    		}
    
    	}
    		
    
    	return $rates;
    }
    #47798
    Anna
    Member

    @marc.bovet
    I haven’t tested this yet – but thank you so much for sharing this work, Marc. 🙂

    #47805
    Marc Bovet
    Participant

    @fervous
    you’re welcome.
    I forgot to mention that WC Vendors needs to be configured like this in the backend :
    1° WC vendor shipping needs to be activated
    2° Shipping System : Flate Rate
    3° Procut Cost Nationally set to a value > 0

    read carefully the comment in the provided code.

    cheers,

    #47855
    Anna
    Member

    Understood- thank you for the clarification. 🙂

    #48776
    Jamie
    Keymaster

    Hello,

    As you have mentioned this code will only work if you have one product from one vendor in the cart. Do you prevent customers from adding more than one item in the cart?

    Also version 1.4.0 has all this built in and working.

    cheers,

    Jamie.

    #48785
    Anna
    Member

    @marc.bovet
    While it is nice for members to share code– please understand that other users will have difficulty using this unless you format it into a gist; please visit github and get set up so you can share code in a more usable format.

    Also, if users d not read carefully they may not understand that this will only work if you have modified WC Vendors such that customers can only check out with ONE vendor in the cart. When you make your gist, please specify this very clearly so that users do not have negative results and confusion.

    If I receive questions about this code, I will be tagging you to support the other members who attempt to use this and have questions.
    Thank you!

    #48831
    Marc Bovet
    Participant

    @digitalchild

    yes I prevent customers that they can only order products from 1 vendor (code is not provided), it can be done in the functions.php file.
    A customer can have more than 1 product in the cart.
    As there is no realease date for the 1.4 I have to find solution to progress….


    @fervous

    yes I’ve plannned to use ghist but no time for that right now.
    I did not modified WC vendors but only used functions in my functions.php file.

    cheers,

    #48893
    Marc Bovet
    Participant
    #48970
    Anna
    Member

    Thanks, @marc.bovet

    #55223
    Joe
    Participant

    Hi @Marc.Bovet

    I have seen your code above. Do you have it available for ALL vendors, and not just limited to ONE vendor in the cart?

    I would like to be able to use this, but allow customers to shop from any vendors at the same time.

    Any help with this is very appreciated.

    Many thanks
    Joe

    #55581
    Joe
    Participant

    Hi @Marc.Bovet

    I have seen your code above. Do you have it available for ALL vendors, and not just limited to ONE vendor in the cart?

    I would like to be able to use this, but allow customers to shop from any vendors at the same time.

    Any help with this is very appreciated.

    Many thanks
    Joe

    #55583
    Marc Bovet
    Participant

    @veopolis

    Hi Joe,

    unfortunately not, but this could be adapted I think.

    vendor 1 :
    item 1.1
    item 1.2

    vendor 2
    item 2.1
    item 2.2

    Subtotal :

    shipping fees
    flat rate vendor 1 : $$$
    flat rate vendor 2 : $$$
    Total shippings :

    You have to make trials and adapt the code.

    HTH
    cheers,

    #63797
    Anas Akhtar
    Participant

    Hi @Marc.Bovet

    I am able to successfully define minimum order amount & show errors for each vendor in cart. But for the ‘Enable the free shipping fees when a certain amount is reached’ and ‘When free shipping is available, hide other methods’ part I cant return two or more values for each vendor and the values that are displayed are always of the first product of cart vendor values.

    Current Scenario:
    For Example If I have two products in cart of two different vendors,

    vendor 1 :
    item 1.1

    vendor 2 :
    item 2.1

    The values of rates and enabling of free shipping is always looked out for the first item in the cart that would be vendor 1. If free shipping is enabled for vendor 1 then it also enables for vendor 2. If shipping rate for vendor 1 is 100 then it is for vendor 2.

    For the enable free shipping part I want to return true or false for each vendor, and for the second part I want to return $rates for each vendor and display. Can anyone help me on how to return multiple values and then display with respect to each vendor their values ?

    Regards,
    Anas

    #63998
    Anas Akhtar
    Participant

    Any help ?

    #68632
    Lauryn Wendus
    Participant

    Hi @ANAS-AKHTAR

    Would you be willing to share how you did this for all vendors (more than one vendor per cart):

    I am able to successfully define minimum order amount & show errors for each vendor in cart.

    If I find a solution for the latter part of your post or connect with someone who can help I will certainly pass along to you!!

Viewing 18 posts - 1 through 18 (of 18 total)
  • The forum ‘WC Vendors Pro Support’ is closed to new topics and replies.