PluginProbe
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO / 1.4.14
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO v1.4.14
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 1.3.46 1.3.45 All 177 releases
disco / app / Utility / Helper.php

Helper.php in Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO 1.4.14, at app/Utility/Helper.php

285 lines 8.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Search Utility
4 *
5 * @package Disco
6 * @subpackage App\Utility
7 * @since 1.0.0
8 * @category Utility
9 */
10
11 namespace Disco\App\Utility;
12
13 use WC_Cart;
14
15 /**
16 * Class Helper
17 *
18 * @package Disco
19 * @subpackage App\Utility
20 * @author Ohidul Islam <wahid0003@gmail.com>
21 * @link https://webappick.com
22 * @license https://opensource.org/licenses/gpl-license.php GNU Public License
23 * @category Utility
24 */
25 class Helper {
26
27 /**
28 * Get Cart Info.
29 *
30 * @param \WC_Cart $cart Cart Object.
31 * @param string $item_id Item ID.
32 * @return array
33 */
34 public static function cart_info( $cart, $item_id = '' ) {
35 $cart_info = array();
36
37 if ( $cart instanceof WC_Cart ) {
38 $cart_info['total']['line_items_count'] = $cart->get_cart_contents_count();
39 $cart_info['total']['cart_items_quantity'] = array_sum( $cart->get_cart_item_quantities() );
40 $cart_info['total']['cart_total_weight'] = $cart->get_cart_contents_weight();
41 $cart_info['total']['cart_subtotal'] = $cart->get_subtotal();
42
43 foreach ( $cart->get_cart() as $item ) {
44 $id = $item['product_id'];
45
46 if ( $item['variation_id'] ) {
47 $id = $item['variation_id'];
48 }
49
50 $item_info = array(
51 'product_id' => $item['product_id'],
52 'variation_id' => $item['variation_id'],
53 'cart_line_item_quantity' => $item['quantity'],
54 'cart_line_item_subtotal' => $item['line_subtotal'],
55 'cart_line_item_id' => $item['data']->get_id(),
56 'cart_line_item_sku' => $item['data']->get_sku(),
57 'cart_line_item_title' => $item['data']->get_name(),
58 'cart_line_item_attributes' => $item['data']->get_attributes(),
59 'cart_line_item_category' => wp_strip_all_tags( $item['data']->get_categories() ),
60 'cart_line_item_tags' => $item['data']->get_tags(),
61 'cart_line_item_weight_unit' => $item['data']->get_meta( '_unit' ),
62 'cart_line_item_weight' => $item['data']->get_weight(),
63 'cart_line_item_width' => $item['data']->get_width(),
64 'cart_line_item_height' => $item['data']->get_height(),
65 'cart_line_item_length' => $item['data']->get_length(),
66 'cart_line_item_type' => $item['data']->get_type(),
67 'product' => $item['data'],
68 );
69
70 if ( $item_id && $item_id === $id ) {
71 return $item_info;
72 }
73
74 $cart_info['items'][ $id ] = $item_info;
75 }//end foreach
76 }//end if
77
78 return $cart_info;
79 }
80
81 /**
82 * Get Matched Conditions Cart Products.
83 *
84 * Checks all products in the WooCommerce cart against the provided conditions and returns those that match.
85 * Iterates through each cart item and applies all base filters from the conditions, using AttributeFactory to
86 * retrieve attribute values for comparison. Skips filters that compare with 'item_quantity' or 'item_count'.
87 *
88 * @param array $all_conditions Array of condition groups, each containing base filters.
89 * @param array $reference_product Reference product data used for attribute comparison.
90 * @return array Array of cart items that match all base filters.
91 */
92 public static function get_matched_conditions_cart_products( array $all_conditions, array $reference_product ): array { // phpcs:ignore
93
94 $matched_products = array();
95
96 $current_base_filters = array();
97
98 foreach ( $all_conditions as $group ) {
99 if ( is_array( $group ) ) {
100 $group = (object) $group;
101 }
102
103 if ( ! isset( $group->base_filters ) || ! is_array( $group->base_filters ) ) {
104 continue;
105 }
106
107 $current_base_filters = array_merge( $current_base_filters, $group->base_filters );
108 }
109
110 $current_base_filters = array_map(
111 static function( $f ) {
112 if ( is_array( $f ) ) {
113 return (object) $f;
114 }
115
116 return $f;
117 },
118 $current_base_filters
119 );
120
121 foreach ( WC()->cart->get_cart() as $cart_item ) {
122 $product = $cart_item['data'];
123
124 if ( ! $product instanceof \WC_Product ) {
125 continue;
126 }
127
128 $is_matched = true;
129
130 foreach ( $current_base_filters as $inner_filter ) {
131 if ( in_array( $inner_filter->compare_with, array( 'item_quantity', 'item_count' ), true ) ) {
132 continue;
133 }
134
135 $temp_product = array(
136 'product' => $product,
137 'cart_item' => $cart_item,
138 );
139
140 // $inner_filter->compare = AttributeFactory::get_value( $inner_filter->compare_with, $reference_product );
141
142
143 $checker = new Conditions(
144 new Config(
145 array(
146 'conditions' => array(
147 array(
148 'base_operator' => 'and',
149 'base_filters' => array( $inner_filter ),
150 ),
151 ),
152 )
153 ),
154 $temp_product
155 );
156
157 if ( ! $checker->is_passed() ) {
158 $is_matched = false;
159
160 break;
161 }
162 }
163
164 if ( ! $is_matched ) {
165 continue;
166 }
167
168 $matched_products[] = $cart_item;
169 }
170
171 return $matched_products;
172 }
173
174
175 /**
176 * Check product discount has any date limit.
177 *
178 * @param \Disco\App\Utility\Config $config Campaign Configuration.
179 * @return bool
180 */
181 public static function is_in_valid_date( $config ) { // phpcs:ignore
182 if ( empty( $config->discount_valid_from ) && empty( $config->discount_valid_to ) ) {
183 return true;
184 }
185
186 if (
187 isset( $config->discount_valid_from )
188 && strtotime( $config->discount_valid_from )
189 && empty( $config->discount_valid_to )
190 ) {
191 $today = gmdate( 'Y-m-d H:i:s' );
192 $start_date = gmdate( 'Y-m-d H:i:s', strtotime( $config->discount_valid_from ) );
193
194 return $today >= $start_date;
195 }
196
197 if (
198 isset( $config->discount_valid_to )
199 && strtotime( $config->discount_valid_to )
200 && empty( $config->discount_valid_from )
201 ) {
202 $today = gmdate( 'Y-m-d H:i:s' );
203 $end_date = gmdate( 'Y-m-d H:i:s', strtotime( $config->discount_valid_to ) );
204
205 return $today <= $end_date;
206 }
207
208 if ( isset( $config->discount_valid_from, $config->discount_valid_to )
209 && strtotime( $config->discount_valid_from )
210 && strtotime( $config->discount_valid_to )
211 ) {
212 $today = gmdate( 'Y-m-d H:i:s' );
213 $start_date = gmdate( 'Y-m-d H:i:s', strtotime( $config->discount_valid_from ) );
214 $end_date = gmdate( 'Y-m-d H:i:s', strtotime( $config->discount_valid_to ) );
215
216 return ( $today >= $start_date ) && ( $today <= $end_date );
217 }
218
219 return false;
220 }
221
222 /**
223 * Check if a product is in a category.
224 *
225 * @param int $product_id Product ID.
226 * @param array $categories Categories.
227 * @return bool
228 */
229 public static function is_in_category( $product_id, $categories ) {
230 // Ensure $categories is an array
231 if ( ! is_array( $categories ) ) {
232 $categories = array( $categories );
233 }
234
235 $get_product = wc_get_product( $product_id );
236
237 if ( ! $get_product ) {
238 return false;
239 }
240
241 if ( $get_product->get_type() === 'variation' ) {
242 $product_id = $get_product->get_parent_id();
243 }
244
245 $terms = get_the_terms( $product_id, 'product_cat' );
246
247 if ( ! $terms || is_wp_error( $terms ) ) {
248 return false;
249 }
250
251 $term_ids = array();
252
253 foreach ( $terms as $term ) {
254 $term_ids[] = $term->term_id;
255
256 // Include all parent category IDs
257 $parent_ids = get_ancestors( $term->term_id, 'product_cat' );
258
259 if ( empty( $parent_ids ) ) {
260 continue;
261 }
262
263 $term_ids = array_merge( $term_ids, $parent_ids );
264 }
265
266 // Remove duplicates just in case
267 $term_ids = array_unique( $term_ids );
268 $common_values = array_intersect( $term_ids, $categories );
269
270 return ! empty( $common_values );
271 }
272
273 /**
274 * Check if the product is passed the filter
275 *
276 * @param \Disco\App\Utility\Config $config Campaign Configuration.
277 * @param array $product Product Info.
278 * @return bool
279 */
280 public static function is_filter_passed( $config, $product ) {
281 return ( new Conditions( $config, $product ) )->is_passed();
282 }
283
284 }
285