Keine Beschreibung

class-wc-discounts.php 33KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  1. <?php
  2. /**
  3. * Discount calculation
  4. *
  5. * @package WooCommerce\Classes
  6. * @since 3.2.0
  7. */
  8. use Automattic\WooCommerce\Utilities\NumberUtil;
  9. defined( 'ABSPATH' ) || exit;
  10. /**
  11. * Discounts class.
  12. */
  13. class WC_Discounts {
  14. /**
  15. * Reference to cart or order object.
  16. *
  17. * @since 3.2.0
  18. * @var WC_Cart|WC_Order
  19. */
  20. protected $object;
  21. /**
  22. * An array of items to discount.
  23. *
  24. * @var array
  25. */
  26. protected $items = array();
  27. /**
  28. * An array of discounts which have been applied to items.
  29. *
  30. * @var array[] Code => Item Key => Value
  31. */
  32. protected $discounts = array();
  33. /**
  34. * WC_Discounts Constructor.
  35. *
  36. * @param WC_Cart|WC_Order $object Cart or order object.
  37. */
  38. public function __construct( $object = null ) {
  39. if ( is_a( $object, 'WC_Cart' ) ) {
  40. $this->set_items_from_cart( $object );
  41. } elseif ( is_a( $object, 'WC_Order' ) ) {
  42. $this->set_items_from_order( $object );
  43. }
  44. }
  45. /**
  46. * Set items directly. Used by WC_Cart_Totals.
  47. *
  48. * @since 3.2.3
  49. * @param array $items Items to set.
  50. */
  51. public function set_items( $items ) {
  52. $this->items = $items;
  53. $this->discounts = array();
  54. uasort( $this->items, array( $this, 'sort_by_price' ) );
  55. }
  56. /**
  57. * Normalise cart items which will be discounted.
  58. *
  59. * @since 3.2.0
  60. * @param WC_Cart $cart Cart object.
  61. */
  62. public function set_items_from_cart( $cart ) {
  63. $this->items = array();
  64. $this->discounts = array();
  65. if ( ! is_a( $cart, 'WC_Cart' ) ) {
  66. return;
  67. }
  68. $this->object = $cart;
  69. foreach ( $cart->get_cart() as $key => $cart_item ) {
  70. $item = new stdClass();
  71. $item->key = $key;
  72. $item->object = $cart_item;
  73. $item->product = $cart_item['data'];
  74. $item->quantity = $cart_item['quantity'];
  75. $item->price = wc_add_number_precision_deep( (float) $item->product->get_price() * (float) $item->quantity );
  76. $this->items[ $key ] = $item;
  77. }
  78. uasort( $this->items, array( $this, 'sort_by_price' ) );
  79. }
  80. /**
  81. * Normalise order items which will be discounted.
  82. *
  83. * @since 3.2.0
  84. * @param WC_Order $order Order object.
  85. */
  86. public function set_items_from_order( $order ) {
  87. $this->items = array();
  88. $this->discounts = array();
  89. if ( ! is_a( $order, 'WC_Order' ) ) {
  90. return;
  91. }
  92. $this->object = $order;
  93. foreach ( $order->get_items() as $order_item ) {
  94. $item = new stdClass();
  95. $item->key = $order_item->get_id();
  96. $item->object = $order_item;
  97. $item->product = $order_item->get_product();
  98. $item->quantity = $order_item->get_quantity();
  99. $item->price = wc_add_number_precision_deep( $order_item->get_subtotal() );
  100. if ( $order->get_prices_include_tax() ) {
  101. $item->price += wc_add_number_precision_deep( $order_item->get_subtotal_tax() );
  102. }
  103. $this->items[ $order_item->get_id() ] = $item;
  104. }
  105. uasort( $this->items, array( $this, 'sort_by_price' ) );
  106. }
  107. /**
  108. * Get the object concerned.
  109. *
  110. * @since 3.3.2
  111. * @return object
  112. */
  113. public function get_object() {
  114. return $this->object;
  115. }
  116. /**
  117. * Get items.
  118. *
  119. * @since 3.2.0
  120. * @return object[]
  121. */
  122. public function get_items() {
  123. return $this->items;
  124. }
  125. /**
  126. * Get items to validate.
  127. *
  128. * @since 3.3.2
  129. * @return object[]
  130. */
  131. public function get_items_to_validate() {
  132. return apply_filters( 'woocommerce_coupon_get_items_to_validate', $this->get_items(), $this );
  133. }
  134. /**
  135. * Get discount by key with or without precision.
  136. *
  137. * @since 3.2.0
  138. * @param string $key name of discount row to return.
  139. * @param bool $in_cents Should the totals be returned in cents, or without precision.
  140. * @return float
  141. */
  142. public function get_discount( $key, $in_cents = false ) {
  143. $item_discount_totals = $this->get_discounts_by_item( $in_cents );
  144. return isset( $item_discount_totals[ $key ] ) ? $item_discount_totals[ $key ] : 0;
  145. }
  146. /**
  147. * Get all discount totals.
  148. *
  149. * @since 3.2.0
  150. * @param bool $in_cents Should the totals be returned in cents, or without precision.
  151. * @return array
  152. */
  153. public function get_discounts( $in_cents = false ) {
  154. $discounts = $this->discounts;
  155. return $in_cents ? $discounts : wc_remove_number_precision_deep( $discounts );
  156. }
  157. /**
  158. * Get all discount totals per item.
  159. *
  160. * @since 3.2.0
  161. * @param bool $in_cents Should the totals be returned in cents, or without precision.
  162. * @return array
  163. */
  164. public function get_discounts_by_item( $in_cents = false ) {
  165. $discounts = $this->discounts;
  166. $item_discount_totals = (array) array_shift( $discounts );
  167. foreach ( $discounts as $item_discounts ) {
  168. foreach ( $item_discounts as $item_key => $item_discount ) {
  169. $item_discount_totals[ $item_key ] += $item_discount;
  170. }
  171. }
  172. return $in_cents ? $item_discount_totals : wc_remove_number_precision_deep( $item_discount_totals );
  173. }
  174. /**
  175. * Get all discount totals per coupon.
  176. *
  177. * @since 3.2.0
  178. * @param bool $in_cents Should the totals be returned in cents, or without precision.
  179. * @return array
  180. */
  181. public function get_discounts_by_coupon( $in_cents = false ) {
  182. $coupon_discount_totals = array_map( 'array_sum', $this->discounts );
  183. return $in_cents ? $coupon_discount_totals : wc_remove_number_precision_deep( $coupon_discount_totals );
  184. }
  185. /**
  186. * Get discounted price of an item without precision.
  187. *
  188. * @since 3.2.0
  189. * @param object $item Get data for this item.
  190. * @return float
  191. */
  192. public function get_discounted_price( $item ) {
  193. return wc_remove_number_precision_deep( $this->get_discounted_price_in_cents( $item ) );
  194. }
  195. /**
  196. * Get discounted price of an item to precision (in cents).
  197. *
  198. * @since 3.2.0
  199. * @param object $item Get data for this item.
  200. * @return int
  201. */
  202. public function get_discounted_price_in_cents( $item ) {
  203. return absint( NumberUtil::round( $item->price - $this->get_discount( $item->key, true ) ) );
  204. }
  205. /**
  206. * Apply a discount to all items using a coupon.
  207. *
  208. * @since 3.2.0
  209. * @param WC_Coupon $coupon Coupon object being applied to the items.
  210. * @param bool $validate Set to false to skip coupon validation.
  211. * @throws Exception Error message when coupon isn't valid.
  212. * @return bool|WP_Error True if applied or WP_Error instance in failure.
  213. */
  214. public function apply_coupon( $coupon, $validate = true ) {
  215. if ( ! is_a( $coupon, 'WC_Coupon' ) ) {
  216. return new WP_Error( 'invalid_coupon', __( 'Invalid coupon', 'woocommerce' ) );
  217. }
  218. $is_coupon_valid = $validate ? $this->is_coupon_valid( $coupon ) : true;
  219. if ( is_wp_error( $is_coupon_valid ) ) {
  220. return $is_coupon_valid;
  221. }
  222. if ( ! isset( $this->discounts[ $coupon->get_code() ] ) ) {
  223. $this->discounts[ $coupon->get_code() ] = array_fill_keys( array_keys( $this->items ), 0 );
  224. }
  225. $items_to_apply = $this->get_items_to_apply_coupon( $coupon );
  226. // Core discounts are handled here as of 3.2.
  227. switch ( $coupon->get_discount_type() ) {
  228. case 'percent':
  229. $this->apply_coupon_percent( $coupon, $items_to_apply );
  230. break;
  231. case 'fixed_product':
  232. $this->apply_coupon_fixed_product( $coupon, $items_to_apply );
  233. break;
  234. case 'fixed_cart':
  235. $this->apply_coupon_fixed_cart( $coupon, $items_to_apply );
  236. break;
  237. default:
  238. $this->apply_coupon_custom( $coupon, $items_to_apply );
  239. break;
  240. }
  241. return true;
  242. }
  243. /**
  244. * Sort by price.
  245. *
  246. * @since 3.2.0
  247. * @param array $a First element.
  248. * @param array $b Second element.
  249. * @return int
  250. */
  251. protected function sort_by_price( $a, $b ) {
  252. $price_1 = $a->price * $a->quantity;
  253. $price_2 = $b->price * $b->quantity;
  254. if ( $price_1 === $price_2 ) {
  255. return 0;
  256. }
  257. return ( $price_1 < $price_2 ) ? 1 : -1;
  258. }
  259. /**
  260. * Filter out all products which have been fully discounted to 0.
  261. * Used as array_filter callback.
  262. *
  263. * @since 3.2.0
  264. * @param object $item Get data for this item.
  265. * @return bool
  266. */
  267. protected function filter_products_with_price( $item ) {
  268. return $this->get_discounted_price_in_cents( $item ) > 0;
  269. }
  270. /**
  271. * Get items which the coupon should be applied to.
  272. *
  273. * @since 3.2.0
  274. * @param object $coupon Coupon object.
  275. * @return array
  276. */
  277. protected function get_items_to_apply_coupon( $coupon ) {
  278. $items_to_apply = array();
  279. foreach ( $this->get_items_to_validate() as $item ) {
  280. $item_to_apply = clone $item; // Clone the item so changes to this item do not affect the originals.
  281. if ( 0 === $this->get_discounted_price_in_cents( $item_to_apply ) || 0 >= $item_to_apply->quantity ) {
  282. continue;
  283. }
  284. if ( ! $coupon->is_valid_for_product( $item_to_apply->product, $item_to_apply->object ) && ! $coupon->is_valid_for_cart() ) {
  285. continue;
  286. }
  287. $items_to_apply[] = $item_to_apply;
  288. }
  289. return $items_to_apply;
  290. }
  291. /**
  292. * Apply percent discount to items and return an array of discounts granted.
  293. *
  294. * @since 3.2.0
  295. * @param WC_Coupon $coupon Coupon object. Passed through filters.
  296. * @param array $items_to_apply Array of items to apply the coupon to.
  297. * @return int Total discounted.
  298. */
  299. protected function apply_coupon_percent( $coupon, $items_to_apply ) {
  300. $total_discount = 0;
  301. $cart_total = 0;
  302. $limit_usage_qty = 0;
  303. $applied_count = 0;
  304. $adjust_final_discount = true;
  305. if ( null !== $coupon->get_limit_usage_to_x_items() ) {
  306. $limit_usage_qty = $coupon->get_limit_usage_to_x_items();
  307. }
  308. $coupon_amount = $coupon->get_amount();
  309. foreach ( $items_to_apply as $item ) {
  310. // Find out how much price is available to discount for the item.
  311. $discounted_price = $this->get_discounted_price_in_cents( $item );
  312. // Get the price we actually want to discount, based on settings.
  313. $price_to_discount = ( 'yes' === get_option( 'woocommerce_calc_discounts_sequentially', 'no' ) ) ? $discounted_price : NumberUtil::round( $item->price );
  314. // See how many and what price to apply to.
  315. $apply_quantity = $limit_usage_qty && ( $limit_usage_qty - $applied_count ) < $item->quantity ? $limit_usage_qty - $applied_count : $item->quantity;
  316. $apply_quantity = max( 0, apply_filters( 'woocommerce_coupon_get_apply_quantity', $apply_quantity, $item, $coupon, $this ) );
  317. $price_to_discount = ( $price_to_discount / $item->quantity ) * $apply_quantity;
  318. // Run coupon calculations.
  319. $discount = floor( $price_to_discount * ( $coupon_amount / 100 ) );
  320. if ( is_a( $this->object, 'WC_Cart' ) && has_filter( 'woocommerce_coupon_get_discount_amount' ) ) {
  321. // Send through the legacy filter, but not as cents.
  322. $filtered_discount = wc_add_number_precision( apply_filters( 'woocommerce_coupon_get_discount_amount', wc_remove_number_precision( $discount ), wc_remove_number_precision( $price_to_discount ), $item->object, false, $coupon ) );
  323. if ( $filtered_discount !== $discount ) {
  324. $discount = $filtered_discount;
  325. $adjust_final_discount = false;
  326. }
  327. }
  328. $discount = wc_round_discount( min( $discounted_price, $discount ), 0 );
  329. $cart_total = $cart_total + $price_to_discount;
  330. $total_discount = $total_discount + $discount;
  331. $applied_count = $applied_count + $apply_quantity;
  332. // Store code and discount amount per item.
  333. $this->discounts[ $coupon->get_code() ][ $item->key ] += $discount;
  334. }
  335. // Work out how much discount would have been given to the cart as a whole and compare to what was discounted on all line items.
  336. $cart_total_discount = wc_round_discount( $cart_total * ( $coupon_amount / 100 ), 0 );
  337. if ( $total_discount < $cart_total_discount && $adjust_final_discount ) {
  338. $total_discount += $this->apply_coupon_remainder( $coupon, $items_to_apply, $cart_total_discount - $total_discount );
  339. }
  340. return $total_discount;
  341. }
  342. /**
  343. * Apply fixed product discount to items.
  344. *
  345. * @since 3.2.0
  346. * @param WC_Coupon $coupon Coupon object. Passed through filters.
  347. * @param array $items_to_apply Array of items to apply the coupon to.
  348. * @param int $amount Fixed discount amount to apply in cents. Leave blank to pull from coupon.
  349. * @return int Total discounted.
  350. */
  351. protected function apply_coupon_fixed_product( $coupon, $items_to_apply, $amount = null ) {
  352. $total_discount = 0;
  353. $amount = $amount ? $amount : wc_add_number_precision( $coupon->get_amount() );
  354. $limit_usage_qty = 0;
  355. $applied_count = 0;
  356. if ( null !== $coupon->get_limit_usage_to_x_items() ) {
  357. $limit_usage_qty = $coupon->get_limit_usage_to_x_items();
  358. }
  359. foreach ( $items_to_apply as $item ) {
  360. // Find out how much price is available to discount for the item.
  361. $discounted_price = $this->get_discounted_price_in_cents( $item );
  362. // Get the price we actually want to discount, based on settings.
  363. $price_to_discount = ( 'yes' === get_option( 'woocommerce_calc_discounts_sequentially', 'no' ) ) ? $discounted_price : $item->price;
  364. // Run coupon calculations.
  365. if ( $limit_usage_qty ) {
  366. $apply_quantity = $limit_usage_qty - $applied_count < $item->quantity ? $limit_usage_qty - $applied_count : $item->quantity;
  367. $apply_quantity = max( 0, apply_filters( 'woocommerce_coupon_get_apply_quantity', $apply_quantity, $item, $coupon, $this ) );
  368. $discount = min( $amount, $item->price / $item->quantity ) * $apply_quantity;
  369. } else {
  370. $apply_quantity = apply_filters( 'woocommerce_coupon_get_apply_quantity', $item->quantity, $item, $coupon, $this );
  371. $discount = $amount * $apply_quantity;
  372. }
  373. if ( is_a( $this->object, 'WC_Cart' ) && has_filter( 'woocommerce_coupon_get_discount_amount' ) ) {
  374. // Send through the legacy filter, but not as cents.
  375. $discount = wc_add_number_precision( apply_filters( 'woocommerce_coupon_get_discount_amount', wc_remove_number_precision( $discount ), wc_remove_number_precision( $price_to_discount ), $item->object, false, $coupon ) );
  376. }
  377. $discount = min( $discounted_price, $discount );
  378. $total_discount = $total_discount + $discount;
  379. $applied_count = $applied_count + $apply_quantity;
  380. // Store code and discount amount per item.
  381. $this->discounts[ $coupon->get_code() ][ $item->key ] += $discount;
  382. }
  383. return $total_discount;
  384. }
  385. /**
  386. * Apply fixed cart discount to items.
  387. *
  388. * @since 3.2.0
  389. * @param WC_Coupon $coupon Coupon object. Passed through filters.
  390. * @param array $items_to_apply Array of items to apply the coupon to.
  391. * @param int $amount Fixed discount amount to apply in cents. Leave blank to pull from coupon.
  392. * @return int Total discounted.
  393. */
  394. protected function apply_coupon_fixed_cart( $coupon, $items_to_apply, $amount = null ) {
  395. $total_discount = 0;
  396. $amount = $amount ? $amount : wc_add_number_precision( $coupon->get_amount() );
  397. $items_to_apply = array_filter( $items_to_apply, array( $this, 'filter_products_with_price' ) );
  398. $item_count = array_sum( wp_list_pluck( $items_to_apply, 'quantity' ) );
  399. if ( ! $item_count ) {
  400. return $total_discount;
  401. }
  402. if ( ! $amount ) {
  403. // If there is no amount we still send it through so filters are fired.
  404. $total_discount = $this->apply_coupon_fixed_product( $coupon, $items_to_apply, 0 );
  405. } else {
  406. $per_item_discount = absint( $amount / $item_count ); // round it down to the nearest cent.
  407. if ( $per_item_discount > 0 ) {
  408. $total_discount = $this->apply_coupon_fixed_product( $coupon, $items_to_apply, $per_item_discount );
  409. /**
  410. * If there is still discount remaining, repeat the process.
  411. */
  412. if ( $total_discount > 0 && $total_discount < $amount ) {
  413. $total_discount += $this->apply_coupon_fixed_cart( $coupon, $items_to_apply, $amount - $total_discount );
  414. }
  415. } elseif ( $amount > 0 ) {
  416. $total_discount += $this->apply_coupon_remainder( $coupon, $items_to_apply, $amount );
  417. }
  418. }
  419. return $total_discount;
  420. }
  421. /**
  422. * Apply custom coupon discount to items.
  423. *
  424. * @since 3.3
  425. * @param WC_Coupon $coupon Coupon object. Passed through filters.
  426. * @param array $items_to_apply Array of items to apply the coupon to.
  427. * @return int Total discounted.
  428. */
  429. protected function apply_coupon_custom( $coupon, $items_to_apply ) {
  430. $limit_usage_qty = 0;
  431. $applied_count = 0;
  432. if ( null !== $coupon->get_limit_usage_to_x_items() ) {
  433. $limit_usage_qty = $coupon->get_limit_usage_to_x_items();
  434. }
  435. // Apply the coupon to each item.
  436. foreach ( $items_to_apply as $item ) {
  437. // Find out how much price is available to discount for the item.
  438. $discounted_price = $this->get_discounted_price_in_cents( $item );
  439. // Get the price we actually want to discount, based on settings.
  440. $price_to_discount = wc_remove_number_precision( ( 'yes' === get_option( 'woocommerce_calc_discounts_sequentially', 'no' ) ) ? $discounted_price : $item->price );
  441. // See how many and what price to apply to.
  442. $apply_quantity = $limit_usage_qty && ( $limit_usage_qty - $applied_count ) < $item->quantity ? $limit_usage_qty - $applied_count : $item->quantity;
  443. $apply_quantity = max( 0, apply_filters( 'woocommerce_coupon_get_apply_quantity', $apply_quantity, $item, $coupon, $this ) );
  444. // Run coupon calculations.
  445. $discount = wc_add_number_precision( $coupon->get_discount_amount( $price_to_discount / $item->quantity, $item->object, true ) ) * $apply_quantity;
  446. $discount = wc_round_discount( min( $discounted_price, $discount ), 0 );
  447. $applied_count = $applied_count + $apply_quantity;
  448. // Store code and discount amount per item.
  449. $this->discounts[ $coupon->get_code() ][ $item->key ] += $discount;
  450. }
  451. // Allow post-processing for custom coupon types (e.g. calculating discrepancy, etc).
  452. $this->discounts[ $coupon->get_code() ] = apply_filters( 'woocommerce_coupon_custom_discounts_array', $this->discounts[ $coupon->get_code() ], $coupon );
  453. return array_sum( $this->discounts[ $coupon->get_code() ] );
  454. }
  455. /**
  456. * Deal with remaining fractional discounts by splitting it over items
  457. * until the amount is expired, discounting 1 cent at a time.
  458. *
  459. * @since 3.2.0
  460. * @param WC_Coupon $coupon Coupon object if appliable. Passed through filters.
  461. * @param array $items_to_apply Array of items to apply the coupon to.
  462. * @param int $amount Fixed discount amount to apply.
  463. * @return int Total discounted.
  464. */
  465. protected function apply_coupon_remainder( $coupon, $items_to_apply, $amount ) {
  466. $total_discount = 0;
  467. foreach ( $items_to_apply as $item ) {
  468. for ( $i = 0; $i < $item->quantity; $i ++ ) {
  469. // Find out how much price is available to discount for the item.
  470. $price_to_discount = $this->get_discounted_price_in_cents( $item );
  471. // Run coupon calculations.
  472. $discount = min( $price_to_discount, 1 );
  473. // Store totals.
  474. $total_discount += $discount;
  475. // Store code and discount amount per item.
  476. $this->discounts[ $coupon->get_code() ][ $item->key ] += $discount;
  477. if ( $total_discount >= $amount ) {
  478. break 2;
  479. }
  480. }
  481. if ( $total_discount >= $amount ) {
  482. break;
  483. }
  484. }
  485. return $total_discount;
  486. }
  487. /**
  488. * Ensure coupon exists or throw exception.
  489. *
  490. * @since 3.2.0
  491. * @throws Exception Error message.
  492. * @param WC_Coupon $coupon Coupon data.
  493. * @return bool
  494. */
  495. protected function validate_coupon_exists( $coupon ) {
  496. if ( ! $coupon->get_id() && ! $coupon->get_virtual() ) {
  497. /* translators: %s: coupon code */
  498. throw new Exception( sprintf( __( 'Coupon "%s" does not exist!', 'woocommerce' ), esc_html( $coupon->get_code() ) ), 105 );
  499. }
  500. return true;
  501. }
  502. /**
  503. * Ensure coupon usage limit is valid or throw exception.
  504. *
  505. * @since 3.2.0
  506. * @throws Exception Error message.
  507. * @param WC_Coupon $coupon Coupon data.
  508. * @return bool
  509. */
  510. protected function validate_coupon_usage_limit( $coupon ) {
  511. if ( ! $coupon->get_usage_limit() ) {
  512. return true;
  513. }
  514. $usage_count = $coupon->get_usage_count();
  515. $data_store = $coupon->get_data_store();
  516. $tentative_usage_count = is_callable( array( $data_store, 'get_tentative_usage_count' ) ) ? $data_store->get_tentative_usage_count( $coupon->get_id() ) : 0;
  517. if ( $usage_count + $tentative_usage_count < $coupon->get_usage_limit() ) {
  518. // All good.
  519. return true;
  520. }
  521. // Coupon usage limit is reached. Let's show as informative error message as we can.
  522. if ( 0 === $tentative_usage_count ) {
  523. // No held coupon, usage limit is indeed reached.
  524. $error_code = WC_Coupon::E_WC_COUPON_USAGE_LIMIT_REACHED;
  525. } elseif ( is_user_logged_in() ) {
  526. $recent_pending_orders = wc_get_orders(
  527. array(
  528. 'limit' => 1,
  529. 'post_status' => array( 'wc-failed', 'wc-pending' ),
  530. 'customer' => get_current_user_id(),
  531. 'return' => 'ids',
  532. )
  533. );
  534. if ( count( $recent_pending_orders ) > 0 ) {
  535. // User logged in and have a pending order, maybe they are trying to use the coupon.
  536. $error_code = WC_Coupon::E_WC_COUPON_USAGE_LIMIT_COUPON_STUCK;
  537. } else {
  538. $error_code = WC_Coupon::E_WC_COUPON_USAGE_LIMIT_REACHED;
  539. }
  540. } else {
  541. // Maybe this user was trying to use the coupon but got stuck. We can't know for sure (performantly). Show a slightly better error message.
  542. $error_code = WC_Coupon::E_WC_COUPON_USAGE_LIMIT_COUPON_STUCK_GUEST;
  543. }
  544. throw new Exception( $coupon->get_coupon_error( $error_code ), $error_code );
  545. }
  546. /**
  547. * Ensure coupon user usage limit is valid or throw exception.
  548. *
  549. * Per user usage limit - check here if user is logged in (against user IDs).
  550. * Checked again for emails later on in WC_Cart::check_customer_coupons().
  551. *
  552. * @since 3.2.0
  553. * @throws Exception Error message.
  554. * @param WC_Coupon $coupon Coupon data.
  555. * @param int $user_id User ID.
  556. * @return bool
  557. */
  558. protected function validate_coupon_user_usage_limit( $coupon, $user_id = 0 ) {
  559. if ( empty( $user_id ) ) {
  560. if ( $this->object instanceof WC_Order ) {
  561. $user_id = $this->object->get_customer_id();
  562. } else {
  563. $user_id = get_current_user_id();
  564. }
  565. }
  566. if ( $coupon && $user_id && apply_filters( 'woocommerce_coupon_validate_user_usage_limit', $coupon->get_usage_limit_per_user() > 0, $user_id, $coupon, $this ) && $coupon->get_id() && $coupon->get_data_store() ) {
  567. $data_store = $coupon->get_data_store();
  568. $usage_count = $data_store->get_usage_by_user_id( $coupon, $user_id );
  569. if ( $usage_count >= $coupon->get_usage_limit_per_user() ) {
  570. if ( $data_store->get_tentative_usages_for_user( $coupon->get_id(), array( $user_id ) ) > 0 ) {
  571. $error_message = $coupon->get_coupon_error( WC_Coupon::E_WC_COUPON_USAGE_LIMIT_COUPON_STUCK );
  572. $error_code = WC_Coupon::E_WC_COUPON_USAGE_LIMIT_COUPON_STUCK;
  573. } else {
  574. $error_message = $coupon->get_coupon_error( WC_Coupon::E_WC_COUPON_USAGE_LIMIT_REACHED );
  575. $error_code = WC_Coupon::E_WC_COUPON_USAGE_LIMIT_REACHED;
  576. }
  577. throw new Exception( $error_message, $error_code );
  578. }
  579. }
  580. return true;
  581. }
  582. /**
  583. * Ensure coupon date is valid or throw exception.
  584. *
  585. * @since 3.2.0
  586. * @throws Exception Error message.
  587. * @param WC_Coupon $coupon Coupon data.
  588. * @return bool
  589. */
  590. protected function validate_coupon_expiry_date( $coupon ) {
  591. if ( $coupon->get_date_expires() && apply_filters( 'woocommerce_coupon_validate_expiry_date', time() > $coupon->get_date_expires()->getTimestamp(), $coupon, $this ) ) {
  592. throw new Exception( __( 'This coupon has expired.', 'woocommerce' ), 107 );
  593. }
  594. return true;
  595. }
  596. /**
  597. * Ensure coupon amount is valid or throw exception.
  598. *
  599. * @since 3.2.0
  600. * @throws Exception Error message.
  601. * @param WC_Coupon $coupon Coupon data.
  602. * @return bool
  603. */
  604. protected function validate_coupon_minimum_amount( $coupon ) {
  605. $subtotal = wc_remove_number_precision( $this->get_object_subtotal() );
  606. if ( $coupon->get_minimum_amount() > 0 && apply_filters( 'woocommerce_coupon_validate_minimum_amount', $coupon->get_minimum_amount() > $subtotal, $coupon, $subtotal ) ) {
  607. /* translators: %s: coupon minimum amount */
  608. throw new Exception( sprintf( __( 'The minimum spend for this coupon is %s.', 'woocommerce' ), wc_price( $coupon->get_minimum_amount() ) ), 108 );
  609. }
  610. return true;
  611. }
  612. /**
  613. * Ensure coupon amount is valid or throw exception.
  614. *
  615. * @since 3.2.0
  616. * @throws Exception Error message.
  617. * @param WC_Coupon $coupon Coupon data.
  618. * @return bool
  619. */
  620. protected function validate_coupon_maximum_amount( $coupon ) {
  621. $subtotal = wc_remove_number_precision( $this->get_object_subtotal() );
  622. if ( $coupon->get_maximum_amount() > 0 && apply_filters( 'woocommerce_coupon_validate_maximum_amount', $coupon->get_maximum_amount() < $subtotal, $coupon ) ) {
  623. /* translators: %s: coupon maximum amount */
  624. throw new Exception( sprintf( __( 'The maximum spend for this coupon is %s.', 'woocommerce' ), wc_price( $coupon->get_maximum_amount() ) ), 112 );
  625. }
  626. return true;
  627. }
  628. /**
  629. * Ensure coupon is valid for products in the list is valid or throw exception.
  630. *
  631. * @since 3.2.0
  632. * @throws Exception Error message.
  633. * @param WC_Coupon $coupon Coupon data.
  634. * @return bool
  635. */
  636. protected function validate_coupon_product_ids( $coupon ) {
  637. if ( count( $coupon->get_product_ids() ) > 0 ) {
  638. $valid = false;
  639. foreach ( $this->get_items_to_validate() as $item ) {
  640. if ( $item->product && in_array( $item->product->get_id(), $coupon->get_product_ids(), true ) || in_array( $item->product->get_parent_id(), $coupon->get_product_ids(), true ) ) {
  641. $valid = true;
  642. break;
  643. }
  644. }
  645. if ( ! $valid ) {
  646. throw new Exception( __( 'Sorry, this coupon is not applicable to selected products.', 'woocommerce' ), 109 );
  647. }
  648. }
  649. return true;
  650. }
  651. /**
  652. * Ensure coupon is valid for product categories in the list is valid or throw exception.
  653. *
  654. * @since 3.2.0
  655. * @throws Exception Error message.
  656. * @param WC_Coupon $coupon Coupon data.
  657. * @return bool
  658. */
  659. protected function validate_coupon_product_categories( $coupon ) {
  660. if ( count( $coupon->get_product_categories() ) > 0 ) {
  661. $valid = false;
  662. foreach ( $this->get_items_to_validate() as $item ) {
  663. if ( $coupon->get_exclude_sale_items() && $item->product && $item->product->is_on_sale() ) {
  664. continue;
  665. }
  666. $product_cats = wc_get_product_cat_ids( $item->product->get_id() );
  667. if ( $item->product->get_parent_id() ) {
  668. $product_cats = array_merge( $product_cats, wc_get_product_cat_ids( $item->product->get_parent_id() ) );
  669. }
  670. // If we find an item with a cat in our allowed cat list, the coupon is valid.
  671. if ( count( array_intersect( $product_cats, $coupon->get_product_categories() ) ) > 0 ) {
  672. $valid = true;
  673. break;
  674. }
  675. }
  676. if ( ! $valid ) {
  677. throw new Exception( __( 'Sorry, this coupon is not applicable to selected products.', 'woocommerce' ), 109 );
  678. }
  679. }
  680. return true;
  681. }
  682. /**
  683. * Ensure coupon is valid for sale items in the list is valid or throw exception.
  684. *
  685. * @since 3.2.0
  686. * @throws Exception Error message.
  687. * @param WC_Coupon $coupon Coupon data.
  688. * @return bool
  689. */
  690. protected function validate_coupon_sale_items( $coupon ) {
  691. if ( $coupon->get_exclude_sale_items() ) {
  692. $valid = true;
  693. foreach ( $this->get_items_to_validate() as $item ) {
  694. if ( $item->product && $item->product->is_on_sale() ) {
  695. $valid = false;
  696. break;
  697. }
  698. }
  699. if ( ! $valid ) {
  700. throw new Exception( __( 'Sorry, this coupon is not valid for sale items.', 'woocommerce' ), 110 );
  701. }
  702. }
  703. return true;
  704. }
  705. /**
  706. * All exclusion rules must pass at the same time for a product coupon to be valid.
  707. *
  708. * @since 3.2.0
  709. * @throws Exception Error message.
  710. * @param WC_Coupon $coupon Coupon data.
  711. * @return bool
  712. */
  713. protected function validate_coupon_excluded_items( $coupon ) {
  714. $items = $this->get_items_to_validate();
  715. if ( ! empty( $items ) && $coupon->is_type( wc_get_product_coupon_types() ) ) {
  716. $valid = false;
  717. foreach ( $items as $item ) {
  718. if ( $item->product && $coupon->is_valid_for_product( $item->product, $item->object ) ) {
  719. $valid = true;
  720. break;
  721. }
  722. }
  723. if ( ! $valid ) {
  724. throw new Exception( __( 'Sorry, this coupon is not applicable to selected products.', 'woocommerce' ), 109 );
  725. }
  726. }
  727. return true;
  728. }
  729. /**
  730. * Cart discounts cannot be added if non-eligible product is found.
  731. *
  732. * @since 3.2.0
  733. * @throws Exception Error message.
  734. * @param WC_Coupon $coupon Coupon data.
  735. * @return bool
  736. */
  737. protected function validate_coupon_eligible_items( $coupon ) {
  738. if ( ! $coupon->is_type( wc_get_product_coupon_types() ) ) {
  739. $this->validate_coupon_sale_items( $coupon );
  740. $this->validate_coupon_excluded_product_ids( $coupon );
  741. $this->validate_coupon_excluded_product_categories( $coupon );
  742. }
  743. return true;
  744. }
  745. /**
  746. * Exclude products.
  747. *
  748. * @since 3.2.0
  749. * @throws Exception Error message.
  750. * @param WC_Coupon $coupon Coupon data.
  751. * @return bool
  752. */
  753. protected function validate_coupon_excluded_product_ids( $coupon ) {
  754. // Exclude Products.
  755. if ( count( $coupon->get_excluded_product_ids() ) > 0 ) {
  756. $products = array();
  757. foreach ( $this->get_items_to_validate() as $item ) {
  758. if ( $item->product && in_array( $item->product->get_id(), $coupon->get_excluded_product_ids(), true ) || in_array( $item->product->get_parent_id(), $coupon->get_excluded_product_ids(), true ) ) {
  759. $products[] = $item->product->get_name();
  760. }
  761. }
  762. if ( ! empty( $products ) ) {
  763. /* translators: %s: products list */
  764. throw new Exception( sprintf( __( 'Sorry, this coupon is not applicable to the products: %s.', 'woocommerce' ), implode( ', ', $products ) ), 113 );
  765. }
  766. }
  767. return true;
  768. }
  769. /**
  770. * Exclude categories from product list.
  771. *
  772. * @since 3.2.0
  773. * @throws Exception Error message.
  774. * @param WC_Coupon $coupon Coupon data.
  775. * @return bool
  776. */
  777. protected function validate_coupon_excluded_product_categories( $coupon ) {
  778. if ( count( $coupon->get_excluded_product_categories() ) > 0 ) {
  779. $categories = array();
  780. foreach ( $this->get_items_to_validate() as $item ) {
  781. if ( ! $item->product ) {
  782. continue;
  783. }
  784. $product_cats = wc_get_product_cat_ids( $item->product->get_id() );
  785. if ( $item->product->get_parent_id() ) {
  786. $product_cats = array_merge( $product_cats, wc_get_product_cat_ids( $item->product->get_parent_id() ) );
  787. }
  788. $cat_id_list = array_intersect( $product_cats, $coupon->get_excluded_product_categories() );
  789. if ( count( $cat_id_list ) > 0 ) {
  790. foreach ( $cat_id_list as $cat_id ) {
  791. $cat = get_term( $cat_id, 'product_cat' );
  792. $categories[] = $cat->name;
  793. }
  794. }
  795. }
  796. if ( ! empty( $categories ) ) {
  797. /* translators: %s: categories list */
  798. throw new Exception( sprintf( __( 'Sorry, this coupon is not applicable to the categories: %s.', 'woocommerce' ), implode( ', ', array_unique( $categories ) ) ), 114 );
  799. }
  800. }
  801. return true;
  802. }
  803. /**
  804. * Get the object subtotal
  805. *
  806. * @return int
  807. */
  808. protected function get_object_subtotal() {
  809. if ( is_a( $this->object, 'WC_Cart' ) ) {
  810. return wc_add_number_precision( $this->object->get_displayed_subtotal() );
  811. } elseif ( is_a( $this->object, 'WC_Order' ) ) {
  812. $subtotal = wc_add_number_precision( $this->object->get_subtotal() );
  813. if ( $this->object->get_prices_include_tax() ) {
  814. // Add tax to tax-exclusive subtotal.
  815. $subtotal = $subtotal + wc_add_number_precision( NumberUtil::round( $this->object->get_total_tax(), wc_get_price_decimals() ) );
  816. }
  817. return $subtotal;
  818. } else {
  819. return array_sum( wp_list_pluck( $this->items, 'price' ) );
  820. }
  821. }
  822. /**
  823. * Check if a coupon is valid.
  824. *
  825. * Error Codes:
  826. * - 100: Invalid filtered.
  827. * - 101: Invalid removed.
  828. * - 102: Not yours removed.
  829. * - 103: Already applied.
  830. * - 104: Individual use only.
  831. * - 105: Not exists.
  832. * - 106: Usage limit reached.
  833. * - 107: Expired.
  834. * - 108: Minimum spend limit not met.
  835. * - 109: Not applicable.
  836. * - 110: Not valid for sale items.
  837. * - 111: Missing coupon code.
  838. * - 112: Maximum spend limit met.
  839. * - 113: Excluded products.
  840. * - 114: Excluded categories.
  841. *
  842. * @since 3.2.0
  843. * @throws Exception Error message.
  844. * @param WC_Coupon $coupon Coupon data.
  845. * @return bool|WP_Error
  846. */
  847. public function is_coupon_valid( $coupon ) {
  848. try {
  849. $this->validate_coupon_exists( $coupon );
  850. $this->validate_coupon_usage_limit( $coupon );
  851. $this->validate_coupon_user_usage_limit( $coupon );
  852. $this->validate_coupon_expiry_date( $coupon );
  853. $this->validate_coupon_minimum_amount( $coupon );
  854. $this->validate_coupon_maximum_amount( $coupon );
  855. $this->validate_coupon_product_ids( $coupon );
  856. $this->validate_coupon_product_categories( $coupon );
  857. $this->validate_coupon_excluded_items( $coupon );
  858. $this->validate_coupon_eligible_items( $coupon );
  859. if ( ! apply_filters( 'woocommerce_coupon_is_valid', true, $coupon, $this ) ) {
  860. throw new Exception( __( 'Coupon is not valid.', 'woocommerce' ), 100 );
  861. }
  862. } catch ( Exception $e ) {
  863. /**
  864. * Filter the coupon error message.
  865. *
  866. * @param string $error_message Error message.
  867. * @param int $error_code Error code.
  868. * @param WC_Coupon $coupon Coupon data.
  869. */
  870. $message = apply_filters( 'woocommerce_coupon_error', is_numeric( $e->getMessage() ) ? $coupon->get_coupon_error( $e->getMessage() ) : $e->getMessage(), $e->getCode(), $coupon );
  871. return new WP_Error(
  872. 'invalid_coupon',
  873. $message,
  874. array(
  875. 'status' => 400,
  876. )
  877. );
  878. }
  879. return true;
  880. }
  881. }