Last active
October 4, 2023 12:44
-
-
Save AnanthFlycart/019b56335aebb7c1cc280a2bdf0b320c to your computer and use it in GitHub Desktop.
CUW: Each product condition
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| add_action('wp_loaded', function() { | |
| if (class_exists('CUW\App\Modules\Conditions\Base')) { | |
| class CUWAdvancedProducts extends \CUW\App\Modules\Conditions\Base | |
| { | |
| // to check condition | |
| public function check($condition, $data) { | |
| $product_ids = []; | |
| foreach ($data['products'] as $product) { | |
| $product_ids[] = $product['id']; | |
| if ($product['variation_id']) { | |
| $product_ids[] = $product['variation_id']; | |
| } | |
| } | |
| if (self::checkLists($condition['values'], array_unique($product_ids), $condition['method'])) { | |
| foreach ($data['products'] as $product) { | |
| $product_id = $product['id']; | |
| if ($product['variation_id']) { | |
| $product_id = $product['variation_id']; | |
| } | |
| $product_qty_count = []; | |
| if (in_array($product_id, $condition['values'])) { | |
| if (isset($product_qty_count[$product_id])) { | |
| $product_qty_count[$product_id] += $product['quantity']; | |
| } else { | |
| $product_qty_count[$product_id] = $product['quantity']; | |
| } | |
| } | |
| foreach ($product_qty_count as $count) { | |
| if (self::checkValues($count, $condition['value'], $condition['operator'])) { | |
| return true; | |
| } | |
| } | |
| } | |
| } | |
| return false; | |
| } | |
| // to load condition template | |
| public function template($data = [], $print = false) { | |
| $key = isset($data['key']) ? (int) $data['key'] : '{key}'; | |
| $method = !empty($data['condition']['method']) ? $data['condition']['method'] : ''; | |
| $values = !empty($data['condition']['values']) ? array_flip($data['condition']['values']) : []; | |
| foreach ($values as $id => $index) { | |
| $values[$id] = \CUW\App\Helpers\WC::getProductTitle($id, true); | |
| } | |
| $operator = !empty($data['condition']['operator']) ? $data['condition']['operator'] : ''; | |
| $value = isset($data['condition']['value']) && !empty($data['condition']['value']) ? $data['condition']['value'] : ''; | |
| ob_start(); | |
| ?> | |
| <div class="condition-method flex-fill" style="min-width: 50px; display: none;"> | |
| <input type="hidden" name="conditions[<?php echo esc_attr($key); ?>][method]" value="in_list" /> | |
| </div> | |
| <div class="condition-values w-50"> | |
| <select multiple class="select2-list" name="conditions[<?php echo esc_attr($key); ?>][values][]" data-list="products" | |
| data-placeholder=" <?php esc_html_e("Choose products", 'checkout-upsell-woocommerce'); ?>"> | |
| <?php foreach ($values as $id => $name) { ?> | |
| <option value="<?php echo esc_attr($id); ?>" selected><?php echo esc_html($name); ?></option> | |
| <?php } ?> | |
| </select> | |
| </div> | |
| <div class="condition-operator flex-fill"> | |
| <select class="form-control" name="conditions[<?php echo esc_attr($key); ?>][operator]"> | |
| <option value="ge" <?php if ($operator == 'ge') echo "selected"; ?>><?php esc_html_e("Greater than or equal to (>=)", 'checkout-upsell-woocommerce'); ?></option> | |
| <option value="gt" <?php if ($operator == 'gt') echo "selected"; ?>><?php esc_html_e("Grater than (>)", 'checkout-upsell-woocommerce'); ?></option> | |
| <option value="le" <?php if ($operator == 'le') echo "selected"; ?>><?php esc_html_e("Less than or equal to (<=)", 'checkout-upsell-woocommerce'); ?></option> | |
| <option value="lt" <?php if ($operator == 'lt') echo "selected"; ?>><?php esc_html_e("Less than (<)", 'checkout-upsell-woocommerce'); ?></option> | |
| <option value="eq" <?php if ($operator == 'eq') echo "selected"; ?>><?php esc_html_e("Equal to (=)", 'checkout-upsell-woocommerce'); ?></option> | |
| </select> | |
| </div> | |
| <div class="condition-value w-25"> | |
| <input class="form-control" type="text" name="conditions[<?php echo esc_attr($key); ?>][value]" | |
| value="<?php echo esc_attr($value); ?>" placeholder="<?php esc_attr_e("Value", 'checkout-upsell-woocommerce'); ?>"> | |
| <?php | |
| $html = ob_get_clean(); | |
| if ($print) echo $html; | |
| return $html; | |
| } | |
| } | |
| } | |
| // to add advanced products condition | |
| add_filter('cuw_conditions', function($conditions) { | |
| if (class_exists('CUWAdvancedProducts')) { | |
| $conditions['advanced_products'] = [ | |
| 'name' => __("Each products in the Cart", 'checkout-upsell-woocommerce'), | |
| 'group' => __("Cart", 'checkout-upsell-woocommerce'), | |
| 'handler' => new CUWAdvancedProducts(), | |
| 'campaigns' => ['checkout_upsells', 'cart_upsells', 'double_order', 'upsell_popups'], | |
| ]; | |
| } | |
| return $conditions; | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment