Skip to content

Instantly share code, notes, and snippets.

@alexv66
Created October 11, 2022 12:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexv66/09c642760369a6efcb069f683132c1e4 to your computer and use it in GitHub Desktop.
Save alexv66/09c642760369a6efcb069f683132c1e4 to your computer and use it in GitHub Desktop.
class AdpWcOrderEditExtensions
{
public function __construct()
{
}
public function register()
{
add_action('woocommerce_admin_order_item_values', [$this, "printRowItem"], 10, 3);
add_action("woocommerce_admin_order_item_headers", [$this, "addHeader"], 10, 1);
}
public function addHeader()
{
printf('<th class="striked_subtotal" data-sort="float">%s</th>', __('Striked price', 'woocommerce'));
}
public function printRowItem($product, $item, $item_id)
{
if ($product === null) {
return;
}
global $theorder;
printf(
'<td class="striked_subtotal" width="1%%"><div class="view">%s</div></td>',
$this->orderItemFormatStrikedPrice($item, $theorder)
);
}
/**
* @param \WC_Order_Item_Product $item
* @param \WC_Order $order
* @return string
*/
protected function orderItemFormatStrikedPrice($item, $order)
{
if (!$item->meta_exists('_wdp_initial_item_subtotal')) {
return "";
}
$incTax = false;
$qty = (float)$item->get_quantity();
$result = wc_price($order->get_line_subtotal($item, $incTax) / $qty, array('currency' => $order->get_currency())
);
if ($order->get_prices_include_tax() && wc_tax_enabled()) {
$initialSubtotalExclTax = (float)$item->get_meta('_wdp_initial_item_subtotal')
- (float)$item->get_meta('_wdp_initial_item_subtotal_tax');
} else {
$initialSubtotalExclTax = (float)$item->get_meta('_wdp_initial_item_subtotal');
}
$initialSubtotalInclTax = $initialSubtotalExclTax + (float)$item->get_meta('_wdp_initial_item_subtotal_tax');
$initialSubtotalExclTax = round($initialSubtotalExclTax, wc_get_price_decimals());
$initialSubtotalInclTax = round($initialSubtotalInclTax, wc_get_price_decimals());
if ($order->get_line_subtotal($item, $incTax) < $initialSubtotalExclTax) {
if ($incTax) {
$ex_tax_label = $order->get_prices_include_tax() ? 1 : 0;
$initialSubtotalHtml = wc_price($initialSubtotalExclTax / $qty, array(
'ex_tax_label' => $ex_tax_label,
'currency' => $order->get_currency(),
));
} else {
$initialSubtotalHtml = wc_price(
$initialSubtotalInclTax / $qty,
array('currency' => $order->get_currency())
);
}
$result = wc_format_sale_price($initialSubtotalHtml, $result);
}
return $result;
}
}
(new AdpWcOrderEditExtensions())->register();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment