Order.php
3 years ago
OrderRefund.php
3 years ago
OrderTraits.php
9 months ago
ThemeUpgrader.php
4 years ago
ThemeUpgraderSkin.php
4 years ago
OrderTraits.php
128 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WC Admin Order Trait |
| 4 | * |
| 5 | * WC Admin Order Trait class that houses shared functionality across order and refund classes. |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\WooCommerce\Admin\Overrides; |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; |
| 11 | |
| 12 | /** |
| 13 | * OrderTraits class. |
| 14 | */ |
| 15 | trait OrderTraits { |
| 16 | /** |
| 17 | * Calculate shipping amount for line item/product as a total shipping amount ratio based on quantity. |
| 18 | * |
| 19 | * @param WC_Order_Item $item Line item from order. |
| 20 | * @param int $order_items_count (optional) The number of order items in an order. This could be the remaining items left to refund. |
| 21 | * @param float $shipping_amount (optional) The shipping fee amount in an order. This could be the remaining shipping amount left to refund. |
| 22 | * |
| 23 | * @return float|int |
| 24 | */ |
| 25 | public function get_item_shipping_amount( $item, $order_items_count = null, $shipping_amount = null ) { |
| 26 | // Shipping amount loosely based on woocommerce code in includes/admin/meta-boxes/views/html-order-item(s).php |
| 27 | // distributed simply based on number of line items. |
| 28 | $product_qty = $item->get_quantity( 'edit' ); |
| 29 | |
| 30 | // Use the passed order_items_count if provided, otherwise get the total number of items in the order. |
| 31 | // This is useful when calculating refunds for partial items in an order. |
| 32 | // For example, if 2 items are refunded from an order with 4 items. The remaining 2 items should have the shipping fee of the refunded items distributed to them. |
| 33 | $order_items = null !== $order_items_count ? $order_items_count : $this->get_item_count(); |
| 34 | |
| 35 | if ( 0 === $order_items ) { |
| 36 | return 0; |
| 37 | } |
| 38 | |
| 39 | // Use the passed shipping_amount if provided, otherwise get the total shipping amount in the order. |
| 40 | // This is useful when calculating refunds for partial shipping in an order. |
| 41 | // For example, if $10 shipping is refunded from an order with $30 shipping, the remaining $20 should be distributed to the remaining items. |
| 42 | $total_shipping_amount = null !== $shipping_amount ? $shipping_amount : (float) $this->get_shipping_total(); |
| 43 | |
| 44 | return $total_shipping_amount / $order_items * $product_qty; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Calculate shipping tax amount for line item/product as a total shipping tax amount ratio based on quantity. |
| 49 | * |
| 50 | * Loosely based on code in includes/admin/meta-boxes/views/html-order-item(s).php. |
| 51 | * |
| 52 | * @todo If WC is currently not tax enabled, but it was before (or vice versa), would this work correctly? |
| 53 | * |
| 54 | * @param WC_Order_Item $item Line item from order. |
| 55 | * @param int $order_items_count (optional) The number of order items in an order. This could be the remaining items left to refund. |
| 56 | * @param float $shipping_tax_amount (optional) The shipping tax amount in an order. This could be the remaining shipping tax amount left to refund. |
| 57 | * |
| 58 | * @return float|int |
| 59 | */ |
| 60 | public function get_item_shipping_tax_amount( $item, $order_items_count = null, $shipping_tax_amount = null ) { |
| 61 | // Use the passed order_items_count if provided, otherwise get the total number of items in the order. |
| 62 | // This is useful when calculating refunds for partial items in an order. |
| 63 | // For example, if 2 items are refunded from an order with 4 items. The remaining 2 items should have the shipping tax of the refunded items distributed to them. |
| 64 | $order_items = null !== $order_items_count ? $order_items_count : $this->get_item_count(); |
| 65 | |
| 66 | if ( 0 === $order_items ) { |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | // Use the passed shipping_tax_amount if provided, otherwise initialize it to 0 and calculate the total shipping tax amount in the order. |
| 71 | // This is useful when calculating refunds for partial shipping tax in an order. |
| 72 | // For example, if $1 shipping tax is refunded from an order with $3 shipping tax, the remaining $2 should be distributed to the remaining items. |
| 73 | $total_shipping_tax_amount = $shipping_tax_amount ? $shipping_tax_amount : 0; |
| 74 | |
| 75 | if ( null === $shipping_tax_amount ) { |
| 76 | $order_taxes = $this->get_taxes(); |
| 77 | $line_items_shipping = $this->get_items( 'shipping' ); |
| 78 | foreach ( $line_items_shipping as $item_id => $shipping_item ) { |
| 79 | $tax_data = $shipping_item->get_taxes(); |
| 80 | if ( $tax_data ) { |
| 81 | foreach ( $order_taxes as $tax_item ) { |
| 82 | $tax_item_id = $tax_item->get_rate_id(); |
| 83 | $tax_item_total = isset( $tax_data['total'][ $tax_item_id ] ) ? (float) $tax_data['total'][ $tax_item_id ] : 0; |
| 84 | $total_shipping_tax_amount += $tax_item_total; |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | $product_qty = $item->get_quantity( 'edit' ); |
| 91 | |
| 92 | return $total_shipping_tax_amount / $order_items * $product_qty; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Calculates coupon amount for specified line item/product. |
| 97 | * |
| 98 | * Coupon calculation based on woocommerce code in includes/admin/meta-boxes/views/html-order-item.php. |
| 99 | * |
| 100 | * @param WC_Order_Item $item Line item from order. |
| 101 | * |
| 102 | * @return float |
| 103 | */ |
| 104 | public function get_item_coupon_amount( $item ) { |
| 105 | return floatval( $item->get_subtotal( 'edit' ) - $item->get_total( 'edit' ) ); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Calculate cart tax amount for line item/product. |
| 110 | * |
| 111 | * @param WC_Order_Item $item Line item from order. |
| 112 | * |
| 113 | * @return float |
| 114 | */ |
| 115 | public function get_item_cart_tax_amount( $item ) { |
| 116 | $order_taxes = $this->get_taxes(); |
| 117 | $tax_data = $item->get_taxes(); |
| 118 | $cart_tax_amount = 0.0; |
| 119 | |
| 120 | foreach ( $order_taxes as $tax_item ) { |
| 121 | $tax_item_id = $tax_item->get_rate_id(); |
| 122 | $cart_tax_amount += isset( $tax_data['total'][ $tax_item_id ] ) ? (float) $tax_data['total'][ $tax_item_id ] : 0; |
| 123 | } |
| 124 | |
| 125 | return $cart_tax_amount; |
| 126 | } |
| 127 | } |
| 128 |