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

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

274 lines 8.0 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 ) {
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 ( isset( $config->discount_valid_from, $config->discount_valid_to )
198 && strtotime( $config->discount_valid_from )
199 && strtotime( $config->discount_valid_to )
200 ) {
201 $today = gmdate( 'Y-m-d H:i:s' );
202 $start_date = gmdate( 'Y-m-d H:i:s', strtotime( $config->discount_valid_from ) );
203 $end_date = gmdate( 'Y-m-d H:i:s', strtotime( $config->discount_valid_to ) );
204
205 return ( $today >= $start_date ) && ( $today <= $end_date );
206 }
207
208 return false;
209 }
210
211 /**
212 * Check if a product is in a category.
213 *
214 * @param int $product_id Product ID.
215 * @param array $categories Categories.
216 * @return bool
217 */
218 public static function is_in_category( $product_id, $categories ) {
219 // Ensure $categories is an array
220 if ( ! is_array( $categories ) ) {
221 $categories = array( $categories );
222 }
223
224 $get_product = wc_get_product( $product_id );
225
226 if ( ! $get_product ) {
227 return false;
228 }
229
230 if ( $get_product->get_type() === 'variation' ) {
231 $product_id = $get_product->get_parent_id();
232 }
233
234 $terms = get_the_terms( $product_id, 'product_cat' );
235
236 if ( ! $terms || is_wp_error( $terms ) ) {
237 return false;
238 }
239
240 $term_ids = array();
241
242 foreach ( $terms as $term ) {
243 $term_ids[] = $term->term_id;
244
245 // Include all parent category IDs
246 $parent_ids = get_ancestors( $term->term_id, 'product_cat' );
247
248 if ( empty( $parent_ids ) ) {
249 continue;
250 }
251
252 $term_ids = array_merge( $term_ids, $parent_ids );
253 }
254
255 // Remove duplicates just in case
256 $term_ids = array_unique( $term_ids );
257 $common_values = array_intersect( $term_ids, $categories );
258
259 return ! empty( $common_values );
260 }
261
262 /**
263 * Check if the product is passed the filter
264 *
265 * @param \Disco\App\Utility\Config $config Campaign Configuration.
266 * @param array $product Product Info.
267 * @return bool
268 */
269 public static function is_filter_passed( $config, $product ) {
270 return ( new Conditions( $config, $product ) )->is_passed();
271 }
272
273 }
274