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('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;
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 %
@Nishadup
Nishadup / function.php
Last active November 27, 2017 11:28
Change Estimated delivery time format in cart/checkout page with FedEx plugin. https://www.xadapter.com/product/woocommerce-fedex-shipping-plugin-with-print-label/
add_filter( 'wf_estimate_delivery_date_format', 'change_estimate_delivery_date_format', 15, 2 );
function change_estimate_delivery_date_format($current_formate){
return 'l, F jS Y'; // Give here desired format, This format gives output like Tuesday, September 13th 2017.
}
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:40
Round to weight and dimensions nearest upper integer if destination country is Switzerland. Work with WooCommerce FedEx plugin https://www.xadapter.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'] );
@Nishadup
Nishadup / functions.php
Created August 16, 2017 14:52 — forked from xadapter/functions.php
Alter WooCommerce Product Weight and Dimension Dynamically
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) {
@Nishadup
Nishadup / function.php
Created September 29, 2017 08:32
Restrict the shipping methods based on state and zip code
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