get_items(); $linedata = array(); /** * @var $orderline WC_Order_item */ foreach ($orderlines as $orderline_key => $orderline) { if ($orderline['qty'] == 0) { // WooCommerce may return 0 for some items during a partial refund if ($orderline['line_total'] != 0 || $orderline['line_tax'] != 0) { return new WP_Error('error', __('Quantity cannot be 0 for item ' . $orderline['name'], 'valitor')); } continue; // Ignore this order line } $_product = wc_get_product($orderline['product_id']); $sku = $_product->get_sku(); $variation_id = $orderline['variation_id']; if ($variation_id) { $result = $wpdb->get_results("SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = '_sku' AND post_id = '" . $variation_id . "'"); if ($result[0]->meta_value) { $sku = $result[0]->meta_value; } } $qty = $orderline['qty']; $totalTax = $orderline['line_tax']; // Skip the products that are not defined in the list - if there is such a list if (!empty($products)) { if (in_array($sku, $products['skuList'])) { $totalTax = (float)number_format(($totalTax / $qty) * $products['skuQty'][$sku], 2, '.', ''); $qty = $products['skuQty'][$sku]; } else continue; // Ignore this order line if is not part of the product list } if ($returnRefundOrderLines) { $linedata[$orderline_key] = array( 'qty' => $qty, 'refund_total' => wc_format_decimal($orderline['line_total']), 'refund_tax' => wc_format_decimal($totalTax) ); } else { $linedata[] = array( 'description' => $orderline['name'], 'itemId' => $sku, 'quantity' => $qty, 'unitPrice' => (float)number_format($orderline['line_total'] / $qty, 2, '.', ''), 'taxAmount' => (float)number_format($totalTax, 2, '.', ''), //'discount' => (float) ($line_discount_percent > 0) ? $line_discount_percent : '', ); } } // Add shipping prices $order_shipping_methods = $order->get_shipping_methods(); $shipping_id = 'NaN'; foreach ($order_shipping_methods as $orderShippingKey => $orderShippingMethods) { $shipping_id = $orderShippingMethods['method_id']; } // In a refund it's possible to have order_shipping == 0 and order_shipping_tax != 0 at the same time if ($order->get_shipping_total() <> 0 || $order->get_shipping_tax() <> 0) { if (!empty($products)) { if (!in_array($shipping_id, $products['skuList'])) { return $linedata; } } $totalShipping = $order->get_shipping_total(); $totalShippingTax = $order->get_shipping_tax(); if ($returnRefundOrderLines) { $linedata[$orderShippingKey] = array( 'qty' => 1, 'refund_total' => wc_format_decimal($totalShipping), 'refund_tax' => wc_format_decimal($totalShippingTax) ); } else { $linedata[] = array( 'description' => $order->get_shipping_method(), 'itemId' => $shipping_id, 'quantity' => 1, 'unitPrice' => (float)number_format($totalShipping, 2, '.', ''), 'taxAmount' => (float)number_format($totalShippingTax, 2, '.', ''), 'goodsType' => 'shipment' ); } } return $linedata; }