Skip to content

Instantly share code, notes, and snippets.

@Nishadup
Nishadup / gist:2ae0b0a57fac406abcabef8556645de3
Created September 25, 2018 13:43
Snippet to auto choose the shipping method on creating the UPS label, even the order is placed non-ups services. Work with pluginhive UPS shipping https://www.pluginhive.com/product/woocommerce-ups-shipping-plugin-with-print-label/
add_filter('ph_ups_label_shipping_method', 'ph_ups_label_shipping_method', 10, 2);
function ph_ups_label_shipping_method( $shippingmethod, $order ){
//Config this array
$shipping_method_map = array(
'free_shipping' => '03',
'flat_rate' => '12',
);
add_filter('xa_canadapost_rate_request', 'sanitize_postal_code_for_rate_req', 10, 2);
function sanitize_postal_code_for_rate_req($xmlRequest, $package){
$xml_obj = new SimpleXMLElement($xmlRequest);
if( isset($xml_obj->{'destination'}->{'domestic'}->{'postal-code'}) ){
$xml_obj->{'destination'}->{'domestic'}->{'postal-code'} = str_replace("-","",$xml_obj->{'destination'}->{'domestic'}->{'postal-code'} );
}
$doc = new DOMDocument();
$doc->formatOutput = TRUE;
$doc->loadXML($xml_obj->asXML());
@Nishadup
Nishadup / functions.php
Created April 3, 2018 14:56
Code snippet to limit insured amount to 100 in the cart with Xadapter FedEx plugin https://www.xadapter.com/product/woocommerce-fedex-shipping-plugin-with-print-label/
add_filter('wf_fedex_calculate_shipping_request', 'modify_fedex_request', 10, 2);
function modify_fedex_request($fedex_requests, $fedex_packages){
$insurance_amount = 100; // New Insurance amount
foreach ($fedex_requests as $req_no => &$request) {
if( isset($request['RequestedShipment']['TotalInsuredValue']['Amount']) ) {
$count = 0;
foreach( $request['RequestedShipment']['RequestedPackageLineItems'] as $key => $item ) {
$request['RequestedShipment']['RequestedPackageLineItems'][$key]['InsuredValue']['Amount'] = $insurance_amount;
@Nishadup
Nishadup / functions.php
Last active April 2, 2018 10:44
Print doctab in FedEx EPL/ZPL label Work with Xadapter FedEx plugin https://www.xadapter.com/product/woocommerce-fedex-shipping-plugin-with-print-label/
add_filter('wf_fedex_request','custom_label_orientation', 10, 2 );
function custom_label_orientation($request, $order){
$fedex_settings = get_option( 'woocommerce_wf_fedex_woocommerce_shipping_settings', null );
$selected_service = $request['RequestedShipment']['ServiceType'];
$custom_services = $fedex_settings['services'];
$shipping_charge = $order->get_total_shipping();
$handling = $shipping_charge;
// Cost adjustment %
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;
@Nishadup
Nishadup / function.php
Last active October 24, 2017 14:39
Snippet to add extra weight with each package of the rate request with Woocommerce UPS plugin. https://www.xadapter.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;
}
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;
@Nishadup
Nishadup / function.php
Created October 9, 2017 14:07
Snippet to translate Tracking text.
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]";
@Nishadup
Nishadup / function.php
Created October 5, 2017 18:38
Adjust shipping coast based on country and state
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,
@Nishadup
Nishadup / function.php
Created October 3, 2017 15:22
Hide shipping methods if freeshipping/flatrate exists.
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) {