| @@ -4,9 +4,9 @@ | ||
| 4 | 4 | |
| 5 | 5 | class WCCampaign |
| 6 | 6 | { |
| 7 | 7 | /** |
| 8 | - * Returns array of campaigns with WC Enabled. | |
| 8 | + * Returns campaigns with WooCommerce enabled. | |
| 9 | 9 | * |
| 10 | 10 | * @return array |
| 11 | 11 | */ |
| 12 | 12 | public function findWCCampaigns() |
| @@ -11,13 +11,13 @@ | ||
| 11 | 11 | */ |
| 12 | 12 | public function findWCCampaigns() |
| 13 | 13 | { |
| 14 | 14 | $posts = get_posts([ |
| 15 | - 'post_type' => HURRYT_POST_TYPE, | |
| 16 | - 'numberposts' => -1, | |
| 17 | - 'post_status' => 'publish', | |
| 18 | - 'meta_key' => 'wc_enable', | |
| 19 | - 'meta_value' => C::YES, | |
| 15 | + 'post_type' => HURRYT_POST_TYPE, | |
| 16 | + 'numberposts' => -1, | |
| 17 | + 'post_status' => 'publish', | |
| 18 | + 'meta_key' => 'wc_enable', | |
| 19 | + 'meta_value' => C::YES, | |
| 20 | 20 | 'suppress_filters' => false, |
| 21 | 21 | ]); |
| 22 | 22 | |
| 23 | 23 | return array_map(function ($post) { |
| @@ -24,9 +24,8 @@ | ||
| 24 | 24 | return new Campaign($post->ID); |
| 25 | 25 | }, $posts); |
| 26 | 26 | } |
| 27 | 27 | |
| 28 | - | |
| 29 | 28 | /** |
| 30 | 29 | * Run campaign for then given product page. |
| 31 | 30 | * |
| 32 | 31 | * @param $productId |
| @@ -34,15 +33,18 @@ | ||
| 34 | 33 | public function run($productId) |
| 35 | 34 | { |
| 36 | 35 | |
| 37 | 36 | $compaigns = $this->findWCCampaigns(); |
| 38 | - | |
| 39 | 37 | foreach ($compaigns as $compaign) { |
| 38 | + $compaign->loadSettings(); | |
| 39 | + if($compaign->enableSticky === C::YES) { | |
| 40 | + continue; | |
| 41 | + }; | |
| 40 | 42 | |
| 41 | - if ($this->hasActiveCampaign($compaign, $productId)) { | |
| 43 | + if ($this->hasActiveCampaign($compaign, $productId) && $this->canApply($compaign,$productId)) { | |
| 42 | 44 | add_action( |
| 43 | 45 | 'woocommerce_single_product_summary', |
| 44 | - function () use ($compaign) { $this->renderShortcode($compaign); }, | |
| 46 | + function () use ($compaign) {$this->renderShortcode($compaign);}, | |
| 45 | 47 | $compaign->getWcPosition(), |
| 46 | 48 | [$compaign->getId()] |
| 47 | 49 | ); |
| 48 | 50 | } |
| @@ -48,9 +50,8 @@ | ||
| 48 | 50 | } |
| 49 | 51 | } |
| 50 | 52 | } |
| 51 | 53 | |
| 52 | - | |
| 53 | 54 | /** |
| 54 | 55 | * Check if given product has an active campaign. |
| 55 | 56 | * |
| 56 | 57 | * @param Campaign $campaign |
| @@ -61,9 +62,9 @@ | ||
| 61 | 62 | public function hasActiveCampaign( |
| 62 | 63 | $campaign, |
| 63 | 64 | $productId |
| 64 | 65 | ) { |
| 65 | - if ( ! $campaign->isWcEnabled()) { | |
| 66 | + if (!$campaign->isWcEnabled()) { | |
| 66 | 67 | return false; |
| 67 | 68 | } |
| 68 | 69 | $categories = $this->getProductsCategories($productId); |
| 69 | 70 | |
| @@ -76,26 +77,26 @@ | ||
| 76 | 77 | case C::WC_PS_TYPE_INCLUDE_PRODUCTS: |
| 77 | 78 | return in_array($productId, $campaign->getWcProductsSelection()); |
| 78 | 79 | |
| 79 | 80 | case C::WC_PS_TYPE_EXCLUDE_PRODUCTS: |
| 80 | - return ! in_array($productId, $campaign->getWcProductsSelection()); | |
| 81 | + return !in_array($productId, $campaign->getWcProductsSelection()); | |
| 81 | 82 | |
| 82 | 83 | case C::WC_PS_TYPE_INCLUDE_CATEGORIES: |
| 83 | 84 | $included_categories = $campaign->getWcProductsSelection(); |
| 84 | - $callback = function ($id) | |
| 85 | - use ($categories) { | |
| 85 | + $callback = function ($id) | |
| 86 | + use ($categories) { | |
| 86 | 87 | return in_array($id, $categories); |
| 87 | 88 | }; |
| 88 | - $results = array_filter($included_categories, $callback); | |
| 89 | + $results = array_filter($included_categories, $callback); | |
| 89 | 90 | |
| 90 | 91 | return count($results) > 0; |
| 91 | 92 | |
| 92 | 93 | case C::WC_PS_TYPE_EXCLUDE_CATEGORIES: |
| 93 | 94 | $excluded_categories = $campaign->getWcProductsSelection(); |
| 94 | - $results = array_filter($excluded_categories, function ( | |
| 95 | + $results = array_filter($excluded_categories, function ( | |
| 95 | 96 | $id |
| 96 | 97 | ) use ($categories) { |
| 97 | - return ! in_array($id, $categories); | |
| 98 | + return !in_array($id, $categories); | |
| 98 | 99 | }); |
| 99 | 100 | |
| 100 | 101 | return count($results) > 0; |
| 101 | 102 | |
| @@ -103,9 +104,8 @@ | ||
| 103 | 104 | return false; |
| 104 | 105 | } |
| 105 | 106 | } |
| 106 | 107 | |
| 107 | - | |
| 108 | 108 | /** |
| 109 | 109 | * Returns categories ids for the given product. |
| 110 | 110 | * |
| 111 | 111 | * @param $product_id |
| @@ -132,22 +132,22 @@ | ||
| 132 | 132 | |
| 133 | 133 | /** |
| 134 | 134 | * Change stock status. |
| 135 | 135 | * |
| 136 | - * @param $campaign \Hurrytimer\Campaign | |
| 137 | - * @param $stockStatus | |
| 136 | + * @param Campaign $campaign | |
| 137 | + * @param int $stockStatus | |
| 138 | 138 | */ |
| 139 | - function changeStockStatus($campaign, $stockStatus) | |
| 139 | + public function changeStockStatus($campaign, $stockStatus) | |
| 140 | 140 | { |
| 141 | 141 | |
| 142 | - if ( ! $campaign->isWcEnabled()) { | |
| 142 | + if (!$campaign->isWcEnabled()) { | |
| 143 | 143 | return; |
| 144 | 144 | } |
| 145 | 145 | |
| 146 | 146 | $products = $this->getProductsIdsByCampaign($campaign); |
| 147 | 147 | foreach ($products as $product) { |
| 148 | - $product->set_stock_status($stockStatus); | |
| 149 | - $product->save(); | |
| 148 | + @$product->set_stock_status($stockStatus); | |
| 149 | + @$product->save(); | |
| 150 | 150 | } |
| 151 | 151 | } |
| 152 | 152 | |
| 153 | 153 | /** |
| @@ -152,17 +152,17 @@ | ||
| 152 | 152 | |
| 153 | 153 | /** |
| 154 | 154 | * Get campaign's products ID. |
| 155 | 155 | * |
| 156 | - * @param \Hurrytimer\Campaign $campaign | |
| 156 | + * @param Campaign $campaign | |
| 157 | 157 | * |
| 158 | - * @return \WC_Product[] | |
| 158 | + * @return array | |
| 159 | 159 | */ |
| 160 | 160 | private function getProductsIdsByCampaign($campaign) |
| 161 | 161 | { |
| 162 | - $products = []; | |
| 162 | + $products = []; | |
| 163 | 163 | $selection = $campaign->getWcProductsSelection(); |
| 164 | - $type = $campaign->getWcProductsSelectionType(); | |
| 164 | + $type = $campaign->getWcProductsSelectionType(); | |
| 165 | 165 | switch ($type) { |
| 166 | 166 | case C::WC_PS_TYPE_ALL: |
| 167 | 167 | $products = wc_get_products(['status' => 'publish']); |
| 168 | 168 | break; |
| @@ -167,9 +167,9 @@ | ||
| 167 | 167 | $products = wc_get_products(['status' => 'publish']); |
| 168 | 168 | break; |
| 169 | 169 | case C::WC_PS_TYPE_EXCLUDE_PRODUCTS: |
| 170 | 170 | $products = wc_get_products([ |
| 171 | - 'status' => 'publish', | |
| 171 | + 'status' => 'publish', | |
| 172 | 172 | 'exclude' => $selection, |
| 173 | 173 | ]); |
| 174 | 174 | break; |
| 175 | 175 | case C::WC_PS_TYPE_INCLUDE_PRODUCTS: |
| @@ -177,22 +177,76 @@ | ||
| 177 | 177 | break; |
| 178 | 178 | case C::WC_PS_TYPE_INCLUDE_CATEGORIES: |
| 179 | 179 | $categories = get_terms([ |
| 180 | 180 | 'taxonomy' => 'product_cat', |
| 181 | - 'include' => implode(',', $selection), | |
| 182 | - 'fields' => 'slugs', | |
| 181 | + 'include' => implode(',', $selection), | |
| 182 | + 'fields' => 'slugs', | |
| 183 | 183 | ]); |
| 184 | - $products = wc_get_products(['status' => 'publish', 'category' => $categories]); | |
| 184 | + $products = wc_get_products(['status' => 'publish', 'category' => $categories]); | |
| 185 | 185 | break; |
| 186 | 186 | case C::WC_PS_TYPE_EXCLUDE_CATEGORIES: |
| 187 | 187 | $categories = get_terms([ |
| 188 | 188 | 'taxonomy' => 'product_cat', |
| 189 | - 'exclude' => implode(',', $selection), | |
| 190 | - 'fields' => 'slugs', | |
| 189 | + 'exclude' => implode(',', $selection), | |
| 190 | + 'fields' => 'slugs', | |
| 191 | 191 | ]); |
| 192 | - $products = wc_get_products(['status' => 'publish', 'category' => $categories]); | |
| 192 | + $products = wc_get_products(['status' => 'publish', 'category' => $categories]); | |
| 193 | 193 | break; |
| 194 | 194 | } |
| 195 | 195 | |
| 196 | 196 | return $products; |
| 197 | + } | |
| 198 | + | |
| 199 | + public function canApply($campaign, $objectId) | |
| 200 | + { | |
| 201 | + $groups = $campaign->getWcConditions(); | |
| 202 | + if(empty($groups)) return true; | |
| 203 | + | |
| 204 | + $product = wc_get_product($objectId); | |
| 205 | + $trueConditions = []; | |
| 206 | + $trueGroups = []; | |
| 207 | + foreach ($groups as $conditions) { | |
| 208 | + foreach ($conditions as $condition) { | |
| 209 | + switch ($condition['key']) { | |
| 210 | + case 'stock_status': | |
| 211 | + if ($condition['operator'] === '==') { | |
| 212 | + $trueConditions[] = $product->get_stock_status() == $condition['value']; | |
| 213 | + } | |
| 214 | + if ($condition['operator'] === '!=') { | |
| 215 | + $trueConditions[] = $product->get_stock_status() != $condition['value']; | |
| 216 | + } | |
| 217 | + break; | |
| 218 | + case 'stock_quantity': | |
| 219 | + if ($condition['operator'] === '==') { | |
| 220 | + $trueConditions[] = $product->get_stock_quantity() == $condition['value']; | |
| 221 | + } | |
| 222 | + if ($condition['operator'] === '!=') { | |
| 223 | + $trueConditions[] = $product->get_stock_quantity() != $condition['value']; | |
| 224 | + } | |
| 225 | + if ($condition['operator'] === '>') { | |
| 226 | + $trueConditions[] = $product->get_stock_quantity() > $condition['value']; | |
| 227 | + } | |
| 228 | + if ($condition['operator'] === '<') { | |
| 229 | + $trueConditions[] = $product->get_stock_quantity() < $condition['value']; | |
| 230 | + } | |
| 231 | + break; | |
| 232 | + case 'shipping_class': | |
| 233 | + if ($condition['operator'] === '==') { | |
| 234 | + $trueConditions[] = $product->get_shipping_class_id() == $condition['value']; | |
| 235 | + } | |
| 236 | + if ($condition['operator'] === '!=') { | |
| 237 | + $trueConditions[] = $product->get_shipping_class_id() != $condition['value']; | |
| 238 | + } | |
| 239 | + break; | |
| 240 | + } | |
| 241 | + } | |
| 242 | + $_true = array_filter($trueConditions, function ($bool) { | |
| 243 | + return $bool === true; | |
| 244 | + }); | |
| 245 | + | |
| 246 | + $trueGroups[] = count($_true) === count($conditions); | |
| 247 | + } | |
| 248 | + return !empty(array_filter($trueGroups, function ($bool) { | |
| 249 | + return $bool === true; | |
| 250 | + })); | |
| 197 | 251 | } |
| 198 | 252 | } |