Skip to content

Instantly share code, notes, and snippets.

Avatar

Shadi Manna shadimanna

View GitHub Profile
@shadimanna
shadimanna / Weight in DE
Created Jan 20, 2021
Return flat weight value if Germany shipping destination
View Weight in DE
add_filter('pr_shipping_dhl_order_weight', 'custom_add_weight', 10, 2);
function custom_add_weight($total_weight, $order_id) {
$order = wc_get_order( $order_id );
if ($order) {
$shipping_address = $order->get_address( 'shipping' );
if( $shipping_address['country'] == 'DE' ) {
return 30;
}
}
View Modify order id to order number
add_filter('pr_shipping_dhl_label_args', 'modify_order_id_to_number', 10, 2);
function modify_order_id_to_number($args, $order_id) {
$order = wc_get_order( $order_id );
$args['dhl_settings']['dhl_label_ref'] = $order->get_order_number();
return $args;
}
View Change SKU argument to Product ID
add_filter('pr_shipping_dhl_label_args', 'modify_item_sku', 10, 2);
function modify_item_sku($args, $order_id) {
if( isset( $args['items'] ) ) {
foreach ($args['items'] as $item_key => $item_value) {
if( isset( $item_value['sku'] ) ) {
$product_id = wc_get_product_id_by_sku( $item_value['sku'] );
if( $product_id ) {
$args['items'][ $item_key ]['sku'] = $product_id;
}
}
View Ensure Item Price 2 Decimal Float
add_filter('pr_shipping_dhl_label_args', 'modify_item_price', 10, 2);
function modify_item_price($args, $order_id) {
if( isset( $args['items'] ) ) {
foreach ($args['items'] as $item_key => $item_value) {
if( isset( $item_value['item_value'] ) ) {
$args['items'][ $item_key ]['item_value'] = round( floatval( $item_value['item_value'] ), 2);
}
}
}
return $args;
View Modify Free Products
add_filter('pr_shipping_dhl_label_args', 'modify_item_price', 10, 2);
function modify_item_price($args, $order_id) {
if( isset( $args['items'] ) ) {
foreach ($args['items'] as $item_key => $item_value) {
if( $item_value['item_value'] == 0 ) {
$args['items'][ $item_key ]['item_value'] = 0.01;
}
}
}
return $args;
View Modify base country
add_filter('pr_shipping_dhl_base_country', 'my_custom_base_country');
function my_custom_base_country( $base_country ) {
return 'DE';
}
View Add or Update Post Number
add_filter('pr_shipping_dhl_label_args', 'modify_post_number', 10, 2);
function modify_post_number($args, $order_id) {
if( $order_id == 12345 ){
$args['shipping_address']['dhl_postnum'] = 54321;
}
return $args;
}
@shadimanna
shadimanna / gist:669913cad84edaea25bfaf415f44a956
Created Jul 17, 2018
Modify WooCommerce "shipping_address_2" to be custom address number field
View gist:669913cad84edaea25bfaf415f44a956
add_filter('pr_shipping_dhl_label_args', 'custom_shipping_address_no', 10, 2);
function custom_shipping_address_no($args, $order_id) {
$args['shipping_address']['address_2'] = get_post_meta($order_id, 'my_custom_address_no', true); // Set to your custom address number field, most likely a order meta data
return $args;
}
View gist:a7bc62eb3dc50145fcd331023c022cf5
add_filter('pr_shipping_dhl_label_args', 'remove_email_number', 10, 2);
function remove_email_number($args, $order_id) {
if(isset($args['shipping_address']['email'])){
unset($args['shipping_address']['email']);
}
return $args;
}
View gist:6751305c06a852b972247af0e595d145
add_filter('pr_shipping_dhl_order_weight', 'custom_add_weight', 10, 2);
function custom_add_weight($total_weight, $order_id) {
$total_weight += 1; // Add any weight needed, units of measure will be whatever is set in WooCommerce
return $total_weight;
}