PluginProbe
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO / 1.2.85
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO v1.2.85
1.4.16 1.4.15 1.4.14 1.4.13 1.4.12 1.4.11 1.4.10 1.4.9 1.4.8 1.4.7 1.4.6 1.4.5 1.4.4 1.4.3 1.4.2 1.4.1 1.4.0 1.3.54 1.3.53 1.3.52 1.3.51 1.3.50 1.3.49 1.3.48 1.3.47 All 179 releases
disco / app / Intents / CartIntent.php

CartIntent.php in Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO 1.2.85, at app/Intents/CartIntent.php

201 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package Disco
4 * @subpackage \App\Intents
5 */
6
7 namespace Disco\App\Intents;
8
9 use Disco\App\Calc\CalcFactory;
10 use Disco\App\DropDown\DropDown;
11
12 /**
13 * Class Cart
14 *
15 * @package Disco
16 * @subpackage \App\Intents
17 * @author Ohidul Islam <wahid0003@gmail.com>
18 * @link https://webappick.com
19 * @license https://opensource.org/licenses/gpl-license.php GNU Public License
20 * @category Intention
21 */
22 class CartIntent extends Intent {
23
24 /**
25 * Intent constructor.
26 *
27 * @param \Disco\App\Utility\Config $campaign Campaign Config.
28 * @param array $settings Global Settings.
29 */
30 public function __construct( $campaign, $settings ) {
31 parent::__construct( $campaign, $settings );
32
33 $this->campaign = $campaign;
34 $this->settings = $settings;
35 }
36
37 /**
38 * Apply Cart intention.
39 * Cart Discount Configuration.
40 *
41 * @param array $items Discount Applicable Cart Items.
42 * @param \WC_Cart $cart Cart.
43 * @return float|int
44 * @throws \Exception If settings not found.
45 */
46 public function get_discounts( $items, $cart ) { // phpcs:ignore
47
48 $discount = 0;
49
50 if ( empty( $items ) ) {
51 return $discount;
52 }
53
54 $rules = $this->campaign->get_discount_rules();
55
56 if ( ! empty( $rules ) && isset( $rules[0] ) ) {
57 $rule = $rules[0];
58
59 if ( is_object( $rule ) ) {
60 $rule = (array) $rule;
61 }
62
63 $discount_value = ( $rule['discount_value'] ? abs( $rule['discount_value'] ) : 0 );
64 $discount_type = $rule['discount_type'];
65
66 $campaign_products = $this->campaign->get_product_ids();
67
68 // $cost = CalcFactory::get_cart_subtotal( $cart );
69 //
70 // if ( $campaign_products !== 'all' ) {
71 // $cost = $this->get_items_subtotal( $items );
72 // }
73
74 $cost = $this->get_items_subtotal( $items );
75
76 // Discount should be less than item price
77 // Discount should be less than cart subtotal
78
79 if ( $discount_type === 'fixed_per_product' ) {
80 // TODO: Check there is no free products in the cart before counting the discount
81 $item_prices = array();
82
83 foreach ( $items as $item ) {
84 $price = CalcFactory::get_price( $item );
85 $qty = $item['quantity'];
86 $min_value = min( $discount_value, $price );
87 $item_prices[] = $min_value * $qty;
88 }
89
90 $discount = array_sum( $item_prices );
91 } else {
92 $discount = $this->calculate_discount( $cost, $discount_type, $discount_value );
93 }
94 }
95
96 // @phpstan-ignore-next-line
97 return apply_filters( 'disco_final_discounted_amount', $discount, $discount_type );
98 }
99
100
101 /**
102 * Get Cart Item Quantities.
103 *
104 * @param array $items Discount Applicable Cart Items.
105 * @return int Cart Item Quantities.
106 */
107 public function get_cart_item_quantities( $items ) {
108 $quantities = 0;
109
110 foreach ( $items as $item ) {
111 $quantities += $item['quantity'];
112 }
113
114 return $quantities;
115 }
116
117 /**
118 * Get Cart Items Subtotal.
119 *
120 * @param array $items Discount Applicable Cart Items.
121 * @return int Cart Item Quantities.
122 */
123 public function get_items_subtotal( $items ) {
124 $subtotal = 0;
125
126 foreach ( $items as $item ) {
127 $subtotal += $item['line_subtotal'];
128 }
129
130 return $subtotal;
131 }
132
133 /**
134 * Get offers for Cart intention.
135 *
136 * @param array $items Discount Applicable Cart Items.
137 * @param \WC_Cart $cart Cart.
138 * @return array|bool
139 */
140 public function get_offers( $items, $cart ) { //phpcs:ignore
141 if ( $cart->is_empty() ) {
142 return false;
143 }
144
145 $offers = array();
146 $rules = $this->campaign->get_discount_rules();
147
148 if ( ! empty( $rules ) && isset( $rules[0] ) ) {
149 $rule = $rules[0];
150 $discount_value = abs( $rule->discount_value );
151 $discount_type = $rule->discount_type;
152 $conditions = $this->campaign->get_conditions();
153 $conditions_string = '';
154 $double_space = ' ';
155
156 if ( is_array( $conditions ) && ! empty( $conditions ) ) {
157 $conditions_string .= '<b>IF</b>' . "\n";
158
159 foreach ( $conditions as $condition_key => $condition ) {
160 if ( $condition_key > 0 ) {
161 $conditions_string .= '<b>' . ucfirst( $condition['base_operator'] ) . "</b>\n";
162 }
163
164 if ( empty( $condition['base_filters'] ) ) {
165 continue;
166 }
167
168 foreach ( $condition['base_filters'] as $filter_key => $filter ) {
169 $filter = (object) $filter;
170
171 if ( $filter_key > 0 ) {
172 $conditions_string .= $double_space . ' <b>' . ucfirst( $filter->operator ) . "</b>\n";
173 }
174
175 $compare_with = ucwords( str_replace( '_', ' ', $filter->compare_with ) );
176 $condition = DropDown::conditions( null, $filter->condition );
177 $compare = $filter->compare;
178 $conditions_string .= "$double_space $compare_with $compare \n";
179 }
180 }
181
182 $conditions_string .= '<b>END IF</b>' . "\n";
183 }
184
185 if ( 'percent' === $discount_type ) {
186 $discount = $discount_value . '% off';
187 } else {
188 $discount = wc_price( $discount_value ) . ' off';
189 }
190
191 $offers[] = array(
192 'condition' => $conditions_string,
193 'discount' => '<b>' . $discount . '</b>',
194 );
195 }
196
197 return $offers;
198 }
199
200 }
201