DHL Express WooCommerce Snippets

Please find the below useful snippets to your website

Navigation Guide

    Filter to restrict the country

    This code snippet creates a filter hook in WordPress to restrict shipping rates for a specific country (in this case, Russia). When the filter hook is applied, it executes the callback function. Inside the callback function, it checks the destination country of each shipping package. If the destination country is Russia, it removes the package from the list of available shipping options. Otherwise, it leaves the package unchanged.

    function a2z_dhlexpress_rate_packages_func($package){
        $return_package = array();
        if($package['destination']['country'] == 'RU'){
            return $return_package;
        }
        return $package;
    }
    add_filter("a2z_dhlexpress_rate_packages","a2z_dhlexpress_rate_packages_func",10,1);

    Flat Rate based on order total for services

    This code snippet defines a function in WordPress used to calculate shipping costs based on the total order value for certain services. It checks if the order total is greater than 250. If it is, it returns a shipping rate of 0 for free shipping. Otherwise, it returns a flat shipping rate of 20. The function is hooked into a filter hook named "hitstacks_dhlexpress_rate_cost", which is applied when calculating shipping costs.

    function hitstacks_dhlexpress_rate_cost_fnc($rate_cost, $rate_code, $order_total){
        if($order_total > 250){
            return 0;
        }
        return 20; // Return currency is must to be a DHL configured currency.
    }
    add_filter("hitstacks_dhlexpress_rate_cost", "hitstacks_dhlexpress_rate_cost_fnc", 10, 3);

    Change estimated delivery date format or text.

    This code snippet defines a function in WordPress used to modify the format or text of the estimated delivery date. However, in its current form, the function simply returns the original estimated delivery date string unchanged. This suggests that the function can be used as a placeholder for custom modification logic. The function is hooked into a filter hook named 'hitstacks_dhlexpres_delivery_date', which is applied when generating the estimated delivery date.

    function hit_dhl_est_delivery_format($string, $est_date, $est_time){
        return $string;
    }
    add_filter('hitstacks_dhlexpres_delivery_date', 'hit_dhl_est_delivery_format',10,3);

    To Sort the rates from Lowest to Highest

    This code snippet defines a function in WordPress used to sort shipping rates from lowest to highest based on their cost. It hooks into the 'woocommerce_package_rates' filter, which is applied when calculating shipping rates for a package. The function sorts the rates array using the uasort() function and returns the sorted array.

    function hitshipo_sort_shipping_methods( $rates, $package ) {
        if ( empty( $rates ) ) return;
        if ( ! is_array( $rates ) ) return;
        uasort( $rates, function ( $a, $b ) { 
            if ( $a == $b ) return 0;
            return ( $a->cost < $b->cost ) ? -1 : 1; 
        } );
        return $rates;
    }
    add_filter( 'woocommerce_package_rates' , 'hitshipo_sort_shipping_methods', 10, 2 );

    To display Fixed rate during checkout

    This code snippet defines a function in WordPress used to provide fixed rate options during the checkout process. It hooks into the "a2z_dhlexpress_manual_flat_rates" filter, which is applied when calculating manual flat rates. The function returns an array of fixed rate options, each represented by an associative array containing rate details such as rate code, name, and rate amount.

    add_filter("a2z_dhlexpress_manual_flat_rates","a2z_dhlexpress_manual_flat_rates_func",10,1);
    function a2z_dhlexpress_manual_flat_rates_func($package){
        return array(
            array("rate_code" => "I", "name" => "DHLExpress Domestic - I", "rate" => "10"),
            array("rate_code" => "N", "name" => "DHLExpress Domestic - N", "rate" => "20")
        );
    }

    Display free Shipping based on product SKU

    This code snippet defines a function in WordPress used to determine whether free shipping should be displayed based on product SKUs. It hooks into the "a2z_dhlexpress_rate_based_product" filter, which is applied when calculating shipping rates based on products. The function removes products with specific SKUs from the list of products used for shipping rate calculation. If all products are removed, it returns 0 to indicate free shipping. Otherwise, it returns the modified array of products.

    function a2z_dhlexpress_rate_based_product_fun($products){
        // Add all SKU of free shipping products in array
        $free_product = array(
            'SHSJGAHGH', 'CFSL6813DB'
        );
    
        foreach($products as $item_key => $val){
            $product = $val['data'];
            $product_data = $product->get_data();
            $exist = array_intersect(array($product_data['sku']), $free_product);
            if(isset($exist) && !empty($exist)){
                unset($products[$item_key]);
            }
        }
    
        if(empty($products)){
            return 0;
        }
        return $products;
    }
    add_filter("a2z_dhlexpress_rate_based_product","a2z_dhlexpress_rate_based_product_fun",10,1);

    Customize insurance option

    This code snippet defines a function in WordPress used to customize the insurance option for shipping. It hooks into the 'hitshipo_ins_ship' filter, which is applied when determining whether insurance should be included in the shipping process. The function always returns "yes", indicating that insurance is enabled for shipping regardless of the input parameters.

    function hitshipo_ins_ship_fun($insurance="no", $vendor="default", $order=[]){
        return "yes";
    }
    add_filter( 'hitshipo_ins_ship' , 'hitshipo_ins_ship_fun', 10, 3 );

    Customize insurance amount

    This code snippet defines a function in WordPress used to customize the insurance amount for shipping. It hooks into the 'hitshipo_ins_val_ship' filter, which is applied when determining the insurance value for shipping. The function always returns 10, indicating the customized insurance amount for shipping regardless of the input parameters.

    function hitshipo_ins_val_ship_fun($insurance_value=0, $vendor="default", $order=[]){
        return 10;
    }
    add_filter( 'hitshipo_ins_val_ship' , 'hitshipo_ins_val_ship_fun', 10, 3 );

    Disable currency conversion for shipment

    This code snippet defines a function in WordPress used to disable currency conversion for shipment labels. It hooks into the 'hit_do_conversion_while_label_generation' filter, which is applied when determining whether currency conversion should be performed during label generation. The function always returns false, indicating that currency conversion should be disabled.

    function hits_curr_con($enabled = true, $toCounty = ""){
        return false;
    }
    add_filter('hit_do_conversion_while_label_generation', 'hits_curr_con', 10, 2);

    Modify products for shipment

    This code snippet defines a function in WordPress used to modify the products included in a shipment. It hooks into the 'hitshipo_prods_to_ship' filter, which is applied when determining the products to be included in a shipment. You can add custom code inside the function to modify the products as needed, and then return the updated array of products.

    function hits_product_modifier($products = []){
        // your code
        return $products;
    }
    add_filter('hitshipo_prods_to_ship', 'hits_product_modifier', 10, 1);

    Hide invoice for specific countries

    This code snippet defines a function in WordPress used to hide the invoice for shipments to specific countries. It hooks into the 'hits_show_invoice' filter, which is applied when determining whether the invoice should be displayed. The function checks if the destination country is in a predefined list of disabled countries and, if so, disables the invoice by returning false. Otherwise, it returns the original value of $enabled.

    function hits_show_invoice_handle($enabled = true, $toCounty = "", $type=""){
        $disabled_countries = ["GB", "US", "DE"];
        if(!empty($toCounty) && in_array($toCounty, $disabled_countries)){
            $enabled = false;
        }
        return $enabled;
    }
    add_filter('hits_show_invoice', 'hits_show_invoice_handle', 10, 3);

    Set receiver VAT

    This code snippet defines a function in WordPress used to set the receiver’s VAT for DHL Express shipments. It hooks into the 'hitshipo_dhlexpress_receiver_vat' filter, which is applied when determining the receiver’s VAT. The function sets the receiver’s VAT to a predefined value "qw12345" and returns it.

    function hitshipo_dhl_receiver_vat_fun($rec_vat="", $order=[]){
        $rec_vat = "qw12345";
        return $rec_vat;
    }
    add_filter( 'hitshipo_dhlexpress_receiver_vat' , 'hitshipo_dhl_receiver_vat_fun', 10, 2 );

    Change inbound commodity based on receiver country

    This code snippet defines a function in WordPress used to change the inbound commodity code based on the receiver’s country for DHL Express shipments. It hooks into the 'a2z_dhlexpress_cc_inb' filter, which is applied when determining the inbound commodity code. The function sets the inbound commodity code to a predefined value "12345678" and returns it.

    function a2z_dhlexpress_cc_inb_fun($saved_cc_inb="", $product_id="", $to_country=""){
        $saved_cc_inb = "12345678";	// Inbound commodity code
        return $saved_cc_inb;
    }
    add_filter('a2z_dhlexpress_cc_inb', 'a2z_dhlexpress_cc_inb_fun', 10, 3);