order_items = array(); $this->is_adjust = false; $this->payment_method = $payment_method; if ($this->payment_method == 'paypal_pro_payflow' || $this->payment_method == 'paypal_advanced') { $this->is_separate_discount = true; } $is_zdp_currency = in_array(get_woocommerce_currency(), $this->zdp_currencies); if ($is_zdp_currency) { $this->decimals = 0; } else { $this->decimals = 2; } } public function order_calculation($order_id) { $this->payment = array(); $order = wc_get_order($order_id); $this->itemamt = 0; $this->discount_amount = 0; $this->order_items = array(); $roundedPayPalTotal = 0; $this->discount_amount = round($order->get_total_discount(), $this->decimals); if ($order->get_discount_total() > 0 && $this->discount_amount == 0) { $this->discount_amount = round($order->get_discount_total(), $this->decimals); } if ($this->get_giftcard_amount($order_id) != false) { $this->discount_amount = round($this->discount_amount + $this->get_giftcard_amount($order_id), $this->decimals); } if ($this->yith_get_giftcard_amount() != false) { $this->discount_amount = round($this->discount_amount + $this->yith_get_giftcard_amount(), $this->decimals); } $desc = ''; foreach ($order->get_items() as $cart_item_key => $values) { $product = $order->get_product_from_item($values); $product_sku = null; if (is_object($product)) { $product_sku = $product->get_sku(); } if (empty($values['name'])) { $name = 'Item'; } else { $name = $values['name']; } $name = $this->clean_product_title($name); $amount = round($values['line_subtotal'] / $values['qty'], $this->decimals); if (is_object($product)) { if ($product->is_type('variation')) { if (version_compare(WC_VERSION, '3.0', '<')) { $attributes = $product->get_variation_attributes(); if (!empty($attributes) && is_array($attributes)) { foreach ($attributes as $key => $value) { $key = str_replace(array('attribute_pa_', 'attribute_'), '', $key); $desc .= ' ' . ucwords($key) . ': ' . $value; } $desc = trim($desc); } } else { $attributes = $product->get_attributes(); if (!empty($attributes) && is_array($attributes)) { foreach ($attributes as $key => $value) { $desc .= ' ' . ucwords($key) . ': ' . $value; } } $desc = trim($desc); } } } $item = array( 'name' => html_entity_decode(wc_trim_string($name ? $name : __('Item', 'angelleye-paypal-invoicing'), 127), ENT_NOQUOTES, 'UTF-8'), 'quantity' => $values['qty'], 'unitPrice' => $this->number_format($amount) ); if (!empty($desc)) { $item['description'] = html_entity_decode(wc_trim_string($desc, 127), ENT_NOQUOTES, 'UTF-8'); } $this->order_items[] = $item; $roundedPayPalTotal += round($amount * $values['qty'], $this->decimals); } foreach ($order->get_fees() as $cart_item_key => $fee_values) { $fee_item_name = version_compare(WC_VERSION, '3.0', '<') ? $fee_values['name'] : $fee_values->get_name(); $amount = $order->get_line_total($fee_values); $fee_item = array( 'name' => html_entity_decode(wc_trim_string($fee_item_name ? $fee_item_name : __('Fee', 'angelleye-paypal-invoicing'), 127), ENT_NOQUOTES, 'UTF-8'), 'quantity' => 1, 'unitPrice' => $this->number_format($amount) ); $this->order_items[] = $fee_item; $roundedPayPalTotal += round($amount * 1, $this->decimals); } $this->taxamt = round($order->get_total_tax(), $this->decimals); $this->shippingamt = round(( version_compare(WC_VERSION, '3.0', '<') ? $order->get_total_shipping() : $order->get_shipping_total()), $this->decimals); $this->itemamt = round($order->get_subtotal(), $this->decimals); $this->order_total = round($this->itemamt + $this->taxamt + $this->shippingamt, $this->decimals); if ($this->itemamt == $this->discount_amount) { unset($this->order_items); $this->order_items = array(); $this->itemamt -= $this->discount_amount; $this->order_total -= $this->discount_amount; } else { if ($this->is_separate_discount == false) { if ($this->discount_amount > 0) { $discLineItem = array( 'name' => 'Discount', 'description' => '', 'quantity' => 1, 'unitPrice' => '-' . $this->number_format($this->discount_amount) ); $this->order_items[] = $discLineItem; $this->itemamt -= $this->discount_amount; $this->order_total -= $this->discount_amount; } } } if (!is_numeric($this->shippingamt)) { $this->shippingamt = 0; } if ($this->shippingamt > 0) { $shipLineItem = array( 'name' => 'Shipping', 'description' => '', 'quantity' => 1, 'unitPrice' => $this->number_format($this->shippingamt) ); $this->order_items[] = $shipLineItem; $this->itemamt += $this->shippingamt; $this->order_total += $this->shippingamt; } $this->order_re_calculate($order); if ($this->taxamt > 0) { $taxLineItem = array( 'name' => 'Tax', 'description' => 'Tax Amount', 'quantity' => 1, 'unitPrice' => $this->number_format(round($this->taxamt, $this->decimals)) ); $this->order_items[] = $taxLineItem; } $this->payment['itemamt'] = $this->number_format(round($this->itemamt, $this->decimals)); $this->payment['taxamt'] = $this->number_format(round($this->taxamt, $this->decimals)); $this->payment['shippingamt'] = $this->number_format(round($this->shippingamt, $this->decimals)); $this->payment['order_items'] = $this->order_items; $this->payment['discount_amount'] = $this->number_format(round($this->discount_amount, $this->decimals)); if ($this->taxamt < 0 || $this->shippingamt < 0) { $this->payment['is_calculation_mismatch'] = true; } else { $this->payment['is_calculation_mismatch'] = false; } return $this->payment; } public function order_re_calculate($order) { $temp_roundedPayPalTotal = 0; if (!empty($this->order_items) && is_array($this->order_items)) { foreach ($this->order_items as $key => $values) { $temp_roundedPayPalTotal += round($values['unitPrice'] * $values['quantity'], $this->decimals); } } $this->itemamt = $temp_roundedPayPalTotal; if ($this->is_separate_discount == true) { $this->temp_total = round($this->itemamt + $this->taxamt - $this->discount_amount, $this->decimals); } else { $this->temp_total = round($this->itemamt + $this->taxamt, $this->decimals); } if (round($order->get_total(), $this->decimals) != $this->temp_total) { $cartItemAmountDifference = round($order->get_total(), $this->decimals) - $this->temp_total; if ($this->taxamt > 0) { $this->taxamt += round($cartItemAmountDifference, $this->decimals); } else { if (count($this->order_items) == 1 && (!empty($this->order_items[0]['quantity']) && $this->order_items[0]['quantity'] > 1 )) { unset($this->order_items); $this->order_total = WC()->cart->total; $this->itemamt = WC()->cart->total; } else { foreach ($this->order_items as $key => $value) { if ($value['qty'] == 1 && $this->is_adjust == false) { $this->order_items[$key]['amt'] = $this->order_items[$key]['unitPrice'] + round($cartItemAmountDifference, $this->decimals); $this->order_total += round($cartItemAmountDifference, $this->decimals); $this->itemamt += round($cartItemAmountDifference, $this->decimals); $this->is_adjust = true; } } if ($this->is_adjust == false) { unset($this->order_items); $this->order_total = WC()->cart->total; $this->itemamt = WC()->cart->total; } } } } } public function get_giftcard_amount($order_id = null) { if (class_exists('WPR_Giftcard')) { if (!empty(WC()->session->giftcard_post)) { $giftCards = WC()->session->giftcard_post; $giftcard = new WPR_Giftcard(); $price = $giftcard->wpr_get_payment_amount(); return $price; } else { $giftCardPayment = get_post_meta($order_id, 'rpgc_payment', true); if (!empty($giftCardPayment) && is_array($giftCardPayment)) { return $giftCardPayment[count($giftCardPayment) - 1]; } return $giftCardPayment; } } else { return false; } } public function yith_get_giftcard_amount() { if (class_exists('YITH_YWGC_Cart_Checkout')) { $amount = 0; if (isset(WC()->cart->applied_gift_cards)) { foreach (WC()->cart->applied_gift_cards as $code) { $amount += isset(WC()->cart->applied_gift_cards_amounts[$code]) ? WC()->cart->applied_gift_cards_amounts[$code] : 0; } } return $amount; } else { return false; } } public function clean_product_title($product_title) { $product_title = strip_tags($product_title); $product_title = str_replace(array("–", "–"), array("-"), $product_title); $product_title = str_replace('&', '-', $product_title); return $product_title; } public function number_format($price) { $decimals = 2; if (!$this->currency_has_decimals(get_woocommerce_currency())) { $decimals = 0; } return number_format($price, $decimals, '.', ''); } public function currency_has_decimals($currency) { if (in_array($currency, array('HUF', 'JPY', 'TWD'))) { return false; } return true; } } }