Skip to content

Instantly share code, notes, and snippets.

@xadapter
xadapter / function.php
Last active March 12, 2020 10:03 — forked from Nishadup/function.php
Snippet to add an extra coast to each and every UPS package in a shipment. WooCommerce UPS Shipping Plugin: https://www.pluginhive.com/product/woocommerce-ups-shipping-plugin-with-print-label/
add_filter('wf_ups_rate', 'wf_modify_ups_rate', 10, 2);
function wf_modify_ups_rate($xml, $packages){
$amount_to_add = 3; //change this with your value to be added with each packages
if($xml){
for( $i=0; $i<count($packages); $i++ ){
//if negotiated rate
if( isset($xml->RatedShipment->NegotiatedRates->NetSummaryCharges->GrandTotal->MonetaryValue) ){
if( property_exists($xml->RatedShipment->NegotiatedRates->NetSummaryCharges, 'TotalChargesWithTaxes') ){
$xml->RatedShipment->NegotiatedRates->NetSummaryCharges->TotalChargesWithTaxes->MonetaryValue += $amount_to_add;
@xadapter
xadapter / function.php
Last active March 12, 2020 10:06 — forked from Nishadup/function.php
Snippet to round off the weight and dimensions nearest to the upper integer if destination country is Switzerland. WooCommerce FedEx Shipping Plugin: https://www.pluginhive.com/product/woocommerce-fedex-shipping-plugin-with-print-label/
add_filter('wf_fedex_request', 'remove_fraction_for_switz', 10, 2);
function remove_fraction_for_switz($request, $order) {
if ($request['RequestedShipment']['Shipper']['Address']['CountryCode'] == 'CH') {
$request['RequestedShipment']['TotalWeight']['Value'] = ceil($request['RequestedShipment']['TotalWeight']['Value']);
foreach ($request['RequestedShipment']['RequestedPackageLineItems'] as $key => &$value) {
if( isset( $value['Dimensions'] ) ){
$value['Dimensions']['Length'] = ceil( $value['Dimensions']['Length'] );
$value['Dimensions']['Width'] = ceil( $value['Dimensions']['Width'] );
$value['Dimensions']['Height'] = ceil( $value['Dimensions']['Height'] );
$value['Weight']['Value'] = ceil( $value['Weight']['Value'] );
@xadapter
xadapter / functions.php
Last active March 12, 2020 10:09 — forked from Nishadup/functions.php
Snippet to alter the WooCommerce product weight and dimensions dynamically. Supports PluginHive Shipping Plugins:https://www.pluginhive.com/product-category/woocommerce-plugin/woocommerce-shipping/
function filter_get_hook_prefix_weight($this_weight, $instance) {
if (!$this_weight || $this_weight < 0.6) $this_weight = .6;
return $this_weight;
}
function filter_get_hook_prefix_height($this_height, $instance) {
@xadapter
xadapter / function.php
Last active March 12, 2020 10:10 — forked from Nishadup/function.php
Snippet to change the estimated delivery time format on the cart or checkout page. WooCommerce FedEx Shipping Plugin: https://www.pluginhive.com/product/woocommerce-fedex-shipping-plugin-with-print-label/
add_filter( 'wf_fedex_estimated_delivery', 'wf_update_delivery_time', 15, 2 );
function wf_update_delivery_time($label, $est_delivery){
$date = explode( '-', $est_delivery['fedex_delivery_time'] );
if( sizeof($date) < 2 ){
return $label;
}
$d = $date[0];
$m = $date[1];
$y = '20'.$date[2];
@xadapter
xadapter / function.php
Last active March 12, 2020 10:11 — forked from Nishadup/function.php
Snippet to restrict WooCommerce shipping methods based on the state and ZIP/Post code. Supports PluginHive Shipping Plugins: https://www.pluginhive.com/product-category/woocommerce-plugin/woocommerce-shipping/
add_filter('woocommerce_package_rates', 'wf_restrict_shipping_methods_based_on_state_and_zipcode', 10, 2);
function wf_restrict_shipping_methods_based_on_state_and_zipcode($available_shipping_methods, $package){
$destination = $package['destination'];
$dest_postcode = $destination['postcode'];
$dest_state = $destination['state'];
//Config this array with state code, zip code, and services you have to show.
$restrict_methods = array(
'CA'=> array(
//Show only UPS Ground for California have zip code start with 910
@xadapter
xadapter / function.php
Last active March 12, 2020 10:12 — forked from Nishadup/function.php
Snippet to hide WooCommerce shipping methods if either Free shipping or Flat rate is present in the cart. Supports PluginHive Shipping Plugins: https://www.pluginhive.com/product-category/woocommerce-plugin/woocommerce-shipping/
add_filter('woocommerce_package_rates', 'xa_hide_shipping_methods_if_free_flaterate_exist', 10, 2);
if(!function_exists('xa_hide_shipping_methods_if_free_flaterate_exist')){
function xa_hide_shipping_methods_if_free_flaterate_exist( $available_shipping_methods, $package ){
$hide_if_shpping_method_exist = array('free_shipping','flat_rate'); //If the shipping methods given here is exists.
$method_to_hide = array('wf_australia_post'); //Hide all the shipping method provided here.
$do_hide = false;
foreach ($hide_if_shpping_method_exist as $method) {
@xadapter
xadapter / function.php
Last active March 12, 2020 10:20 — forked from Nishadup/function.php
Snippet to adjust WooCommerce shipping cost based on the country and state. Supports PluginHive Shipping Plugins: https://www.pluginhive.com/product-category/woocommerce-plugin/woocommerce-shipping/
add_filter( 'woocommerce_package_rates', 'wf_add_charge_if_exceed_cost', 15, 2 );
function wf_add_charge_if_exceed_cost( $available_shipping_methods, $package ){
$mothods = array('wf_woocommerce_shipping_pro'); //Set methods to adjust
//Config this array with country code, state code and rate to be added.
$destination_array = array(
'US' => array(
'NY' => 5,
'CA' => 6,
@xadapter
xadapter / function.php
Last active March 12, 2020 10:20 — forked from Nishadup/function.php
Snippet to translate the WooCommerce shipment tracking text. Supports PluginHive Shipping Plugins: https://www.pluginhive.com/product-category/woocommerce-plugin/woocommerce-shipping/
add_filter('wf_custom_tracking_message', 'update_tracking_message', 10, 3);
function update_tracking_message($current_msg, $language, $order){
$wpml_language = get_post_meta( $order, 'wpml_language', true);
if( $wpml_language == 'fr-ca' ){
return "Votre commande a été expédiée le [DATE] via [SERVICE]. Pour suivre les envois, veuillez suivre le lien correspondant aux numéros d'expédition [ID]";
}else{
return "Your order was shipped on [DATE] via [SERVICE]. To track shipment, please follow the link(s) [ID]";
@xadapter
xadapter / function.php
Last active March 12, 2020 10:21 — forked from Nishadup/function.php
Snippet to add extra weight with every package of the UPS rate request. WooCommerce UPS Shipping Plugin: https://www.pluginhive.com/product/woocommerce-ups-shipping-plugin-with-print-label/
add_filter( 'wf_ups_rate_request', 'adjust_package_weight', 20, 2);
function adjust_package_weight( $rate_request_data, $package ){
$package_extra_weight = 5;
$req_arr = explode('<?xml version="1.0" ?>', $rate_request_data);
$xml_obj = new SimpleXMLElement( $req_arr[2] );
foreach ($xml_obj->Shipment->Package as $key => $package) {
$package->PackageWeight->Weight += $package_extra_weight;
}
@xadapter
xadapter / function.php
Last active March 12, 2020 10:22 — forked from Nishadup/function.php
Snippet to add extra shipping cost to every UPS package. WooCommerce UPS Shipping Plugin: https://www.pluginhive.com/product/woocommerce-ups-shipping-plugin-with-print-label/
add_filter('wf_ups_rate', 'wf_modify_ups_rate', 10, 2);
function wf_modify_ups_rate($xml, $packages){
$amount_to_add = 3; //change this with your value to be added with each packages
if($xml){
for( $i=0; $i<count($packages); $i++ ){
//if negotiated rate
if( isset($xml->RatedShipment->NegotiatedRates->NetSummaryCharges->GrandTotal->MonetaryValue) ){
if( property_exists($xml->RatedShipment->NegotiatedRates->NetSummaryCharges, 'TotalChargesWithTaxes') ){
$xml->RatedShipment->NegotiatedRates->NetSummaryCharges->TotalChargesWithTaxes->MonetaryValue += $amount_to_add;