PluginProbe
WooCommerce Square / 3.8.0
WooCommerce Square v3.8.0
5.5.0 5.4.3 5.4.2 5.4.1 5.4.0 trunk 1.0.25 1.0.26 1.0.27 1.0.28 1.0.29 1.0.30 1.0.31 1.0.32 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.38 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 All 132 releases
woocommerce-square / includes / Framework / Compatibility / Order_Compatibility.php
Order_Compatibility.php
248 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WooCommerce Plugin Framework
4 *
5 * This source file is subject to the GNU General Public License v3.0
6 * that is bundled with this package in the file license.txt.
7 * It is also available through the world-wide-web at this URL:
8 * http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 or later
9 * If you did not receive a copy of the license and are unable to
10 * obtain it through the world-wide-web, please send an email
11 * to license@skyverge.com so we can send you a copy immediately.
12 *
13 * @since 3.0.0
14 * @author WooCommerce / SkyVerge
15 * @copyright Copyright (c) 2021-2022, WooCommerce.
16 * @copyright Copyright (c) 2013-2019, SkyVerge, Inc.
17 * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 or later
18 *
19 * Modified by WooCommerce on 01 December 2021.
20 */
21
22 namespace WooCommerce\Square\Framework\Compatibility;
23
24 defined( 'ABSPATH' ) || exit;
25
26 /**
27 * WooCommerce order compatibility class.
28 *
29 * @since 3.0.0
30 */
31 class Order_Compatibility extends Data_Compatibility {
32
33 /** @var array mapped compatibility properties, as `$new_prop => $old_prop` */
34 protected static $compat_props = array(
35 'date_completed' => 'completed_date',
36 'date_paid' => 'paid_date',
37 'date_modified' => 'modified_date',
38 'date_created' => 'order_date',
39 'customer_id' => 'customer_user',
40 'discount' => 'cart_discount',
41 'discount_tax' => 'cart_discount_tax',
42 'shipping_total' => 'total_shipping',
43 'type' => 'order_type',
44 'currency' => 'order_currency',
45 'version' => 'order_version',
46 );
47
48 /**
49 * Gets an order date.
50 *
51 * This should only be used to retrieve WC core date properties.
52 *
53 * @since 3.0.0
54 *
55 * @param \WC_Order $order order object
56 * @param string $type type of date to get
57 * @param string $context if 'view' then the value will be filtered
58 *
59 * @return \WC_DateTime|null
60 */
61 public static function get_date_prop( \WC_Order $order, $type, $context = 'edit' ) {
62
63 $date = null;
64 $prop = "date_{$type}";
65 $date = is_callable( array( $order, "get_{$prop}" ) ) ? $order->{"get_{$prop}"}( $context ) : null;
66
67 return $date;
68 }
69
70
71 /**
72 * Gets an order property.
73 *
74 * @since 3.0.0
75 * @param \WC_Order $object the order object
76 * @param string $prop the property name
77 * @param string $context if 'view' then the value will be filtered
78 * @return mixed
79 */
80 public static function get_prop( $object, $prop, $context = 'edit', $compat_props = array() ) {
81
82 return parent::get_prop( $object, $prop, $context, self::$compat_props );
83 }
84
85
86 /**
87 * Sets an order's properties.
88 *
89 * Note that this does not save any data to the database.
90 *
91 * @since 3.0.0
92 * @param \WC_Order $object the order object
93 * @param array $props the new properties as $key => $value
94 * @return \WC_Data|\WC_Order
95 */
96 public static function set_props( $object, $props, $compat_props = array() ) {
97
98 return parent::set_props( $object, $props, self::$compat_props );
99 }
100
101
102 /**
103 * Order item CRUD compatibility method to add a coupon to an order.
104 *
105 * @since 3.0.0
106 * @param \WC_Order $order the order object
107 * @param array $code the coupon code
108 * @param int $discount the discount amount.
109 * @param int $discount_tax the discount tax amount.
110 * @return int the order item ID
111 */
112 public static function add_coupon( \WC_Order $order, $code = array(), $discount = 0, $discount_tax = 0 ) {
113
114 $item = new \WC_Order_Item_Coupon();
115
116 $item->set_props(
117 array(
118 'code' => $code,
119 'discount' => $discount,
120 'discount_tax' => $discount_tax,
121 'order_id' => $order->get_id(),
122 )
123 );
124
125 $item->save();
126 $order->add_item( $item );
127
128 return $item->get_id();
129 }
130
131
132 /**
133 * Order item CRUD compatibility method to add a fee to an order.
134 *
135 * @since 3.0.0
136 * @param \WC_Order $order the order object
137 * @param object $fee the fee to add
138 * @return int the order item ID
139 */
140 public static function add_fee( \WC_Order $order, $fee ) {
141 $item = new \WC_Order_Item_Fee();
142
143 $item->set_props(
144 array(
145 'name' => $fee->name,
146 'tax_class' => $fee->taxable ? $fee->tax_class : 0,
147 'total' => $fee->amount,
148 'total_tax' => $fee->tax,
149 'taxes' => array(
150 'total' => $fee->tax_data,
151 ),
152 'order_id' => $order->get_id(),
153 )
154 );
155
156 $item->save();
157 $order->add_item( $item );
158
159 return $item->get_id();
160 }
161
162
163 /**
164 * Order item CRUD compatibility method to add a shipping line to an order.
165 *
166 * @since 3.0.0
167 *
168 * @param \WC_Order $order order object
169 * @param \WC_Shipping_Rate $shipping_rate shipping rate to add
170 * @return int the order item ID
171 */
172 public static function add_shipping( \WC_Order $order, $shipping_rate ) {
173 $item = new \WC_Order_Item_Shipping();
174
175 $item->set_props(
176 array(
177 'method_title' => $shipping_rate->label,
178 'method_id' => $shipping_rate->id,
179 'total' => wc_format_decimal( $shipping_rate->cost ),
180 'taxes' => $shipping_rate->taxes,
181 'order_id' => $order->get_id(),
182 )
183 );
184
185 foreach ( $shipping_rate->get_meta_data() as $key => $value ) {
186 $item->add_meta_data( $key, $value, true );
187 }
188
189 $item->save();
190 $order->add_item( $item );
191
192 return $item->get_id();
193 }
194
195
196 /**
197 * Order item CRUD compatibility method to add a tax line to an order.
198 *
199 * @since 3.0.0
200 *
201 * @param \WC_Order $order order object
202 * @param int $tax_rate_id tax rate ID
203 * @param float $tax_amount cart tax amount
204 * @param float $shipping_tax_amount shipping tax amount
205 * @return int order item ID
206 */
207 public static function add_tax( \WC_Order $order, $tax_rate_id, $tax_amount = 0, $shipping_tax_amount = 0 ) {
208 $item = new \WC_Order_Item_Tax();
209
210 $item->set_props(
211 array(
212 'rate_id' => $tax_rate_id,
213 'tax_total' => $tax_amount,
214 'shipping_tax_total' => $shipping_tax_amount,
215 )
216 );
217
218 $item->set_rate( $tax_rate_id );
219 $item->set_order_id( $order->get_id() );
220 $item->save();
221
222 $order->add_item( $item );
223
224 return $item->get_id();
225 }
226
227 /**
228 * Determines if an order has an available shipping address.
229 *
230 * WooCommerce 3.0+ no longer fills the shipping address with the billing if
231 * a shipping address was never set by the customer at checkout, as is the
232 * case with virtual orders. This method is helpful for gateways that may
233 * reject such transactions with blank shipping information.
234 *
235 * TODO: Remove when WC 3.0.4 can be required {CW 2017-04-17}
236 *
237 * @since 3.0.0
238 *
239 * @param \WC_Order $order order object
240 *
241 * @return bool
242 */
243 public static function has_shipping_address( \WC_Order $order ) {
244
245 return self::get_prop( $order, 'shipping_address_1' ) || self::get_prop( $order, 'shipping_address_2' );
246 }
247 }
248