Last active
August 9, 2025 01:17
-
-
Save RaiAnsar/a098e5c1828fce512a1acacafdd442b1 to your computer and use it in GitHub Desktop.
DHL Labels Price Override
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Plugin Name: MT DHL Label Price Override | |
| * Description: Overrides product prices on DHL labels for specific countries | |
| * Version: 2.0.0 | |
| * Author: Rai Ansar | |
| */ | |
| if (!defined('ABSPATH')) { | |
| exit; | |
| } | |
| class MT_DHL_Label_Price_Override { | |
| private $fixed_prices = array( | |
| 'FI' => array('value' => 24.99, 'currency' => 'EUR', 'name' => 'Finland'), | |
| 'DK' => array('value' => 186.00, 'currency' => 'DKK', 'name' => 'Denmark'), | |
| 'NO' => array('value' => 299.00, 'currency' => 'NOK', 'name' => 'Norway'), | |
| 'SE' => array('value' => 278.00, 'currency' => 'SEK', 'name' => 'Sweden') | |
| ); | |
| public function __construct() { | |
| $this->init_hooks(); | |
| } | |
| private function init_hooks() { | |
| // This is the CORRECT filter used by DHL plugin | |
| add_filter('pr_shipping_dhl_label_args', array($this, 'modify_label_args'), 999, 2); | |
| // Add info box to show override status | |
| add_action('add_meta_boxes', array($this, 'add_price_info_box')); | |
| } | |
| public function add_price_info_box() { | |
| add_meta_box( | |
| 'mt_dhl_price_info', | |
| 'DHL Price Override', | |
| array($this, 'render_price_info_box'), | |
| 'shop_order', | |
| 'side', | |
| 'high' | |
| ); | |
| } | |
| public function render_price_info_box($post) { | |
| $order = wc_get_order($post->ID); | |
| $shipping_country = $order->get_shipping_country(); | |
| if (isset($this->fixed_prices[$shipping_country])) { | |
| $price_info = $this->fixed_prices[$shipping_country]; | |
| echo '<p style="color: #2271b1; font-weight: bold;">✓ Override Active</p>'; | |
| echo '<p>Fixed price: <strong>' . $price_info['value'] . ' ' . $price_info['currency'] . '</strong></p>'; | |
| echo '<p>Country: ' . $price_info['name'] . '</p>'; | |
| echo '<hr style="margin: 10px 0;">'; | |
| echo '<p style="font-size: 11px; color: #666;">This price will be used on the DHL label instead of actual product prices.</p>'; | |
| } else { | |
| echo '<p style="color: #666;">No override for this country</p>'; | |
| echo '<p style="font-size: 11px;">Using actual product prices</p>'; | |
| } | |
| } | |
| public function modify_label_args($args, $order_id) { | |
| $order = wc_get_order($order_id); | |
| if (!$order) { | |
| return $args; | |
| } | |
| $shipping_country = $order->get_shipping_country(); | |
| // Check if this country should have price override | |
| if (!isset($this->fixed_prices[$shipping_country])) { | |
| return $args; | |
| } | |
| $fixed_price = $this->fixed_prices[$shipping_country]['value']; | |
| $currency = $this->fixed_prices[$shipping_country]['currency']; | |
| // Log for debugging | |
| error_log('[DHL Override] Processing order ' . $order_id . ' for country ' . $shipping_country . ' with price ' . $fixed_price); | |
| // Modify the items array if it exists | |
| if (isset($args['items']) && is_array($args['items'])) { | |
| foreach ($args['items'] as &$item) { | |
| // Override item value | |
| $item['item_value'] = $fixed_price; | |
| // Also set customs value if it exists | |
| if (isset($item['customs_value'])) { | |
| $item['customs_value'] = $fixed_price; | |
| } | |
| // Update line total based on quantity | |
| if (isset($item['qty'])) { | |
| $item['line_total'] = $fixed_price * $item['qty']; | |
| } | |
| error_log('[DHL Override] Modified item: SKU=' . (isset($item['sku']) ? $item['sku'] : 'N/A') . ', New value=' . $fixed_price); | |
| } | |
| } | |
| // Update total items value if it exists | |
| if (isset($args['order_details']['items_value'])) { | |
| $total_items = isset($args['items']) ? count($args['items']) : 1; | |
| $total_qty = 0; | |
| // Calculate total quantity | |
| if (isset($args['items'])) { | |
| foreach ($args['items'] as $item) { | |
| $total_qty += isset($item['qty']) ? $item['qty'] : 1; | |
| } | |
| } | |
| $args['order_details']['items_value'] = $fixed_price * $total_qty; | |
| error_log('[DHL Override] Set total items value to: ' . $args['order_details']['items_value']); | |
| } | |
| // Set currency if possible | |
| if (isset($args['order_details']['currency'])) { | |
| $args['order_details']['currency'] = $currency; | |
| } | |
| // Add order note | |
| $order->add_order_note(sprintf( | |
| 'DHL price override applied: %s %s for %s', | |
| $fixed_price, | |
| $this->fixed_prices[$shipping_country]['currency'], | |
| $this->fixed_prices[$shipping_country]['name'] | |
| )); | |
| return $args; | |
| } | |
| } | |
| // Initialize the plugin | |
| add_action('plugins_loaded', function() { | |
| new MT_DHL_Label_Price_Override(); | |
| }, 20); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment