Last active
November 30, 2016 21:22
-
-
Save dummyphp/9d56499299e68e7f39472d60139fc79c to your computer and use it in GitHub Desktop.
WC Vendors : Shipping fees per order
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//******************************************************************************* | |
// !! CAUTION !! | |
// PROVIDED CODE WILL ONLY WORKS WHEN A CUSTOMER ORDER FROM ONLY 1 SHOP AT A TIME | |
//******************************************************************************* | |
// This code is provided as is and without any warranty | |
//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 can only order the products from 1 vendor | |
//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 | |
//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 | |
} | |
//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 | |
//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; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Pls I want to build a social network with lavarel socialite I need help, any attention at all I will be glad