| 1 |
<?php |
| 2 |
|
| 3 |
namespace Hurrytimer; |
| 4 |
|
| 5 |
class WCCampaign { |
| 6 |
/** |
| 7 |
* Returns campaigns with WooCommerce enabled. |
| 8 |
* |
| 9 |
* @return array |
| 10 |
*/ |
| 11 |
public function get_wc_campaigns() { |
| 12 |
$posts = get_posts( [ |
| 13 |
'post_type' => HURRYT_POST_TYPE, |
| 14 |
'numberposts' => -1, |
| 15 |
'post_status' => 'publish', |
| 16 |
'meta_query' => [ |
| 17 |
[ |
| 18 |
'key' => 'wc_enable', |
| 19 |
'value' => C::YES, |
| 20 |
] |
| 21 |
], |
| 22 |
] ); |
| 23 |
|
| 24 |
return array_map( function ( $post ) { |
| 25 |
return hurryt_get_campaign( $post->ID ); |
| 26 |
}, $posts ); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Run campaign for then given product page. |
| 31 |
* |
| 32 |
* @param $product_id |
| 33 |
*/ |
| 34 |
public function run( $product_id ) { |
| 35 |
$compaigns = $this->get_wc_campaigns(); |
| 36 |
|
| 37 |
/** |
| 38 |
* @var Campaign $campaign |
| 39 |
*/ |
| 40 |
foreach ( $compaigns as $campaign ) { |
| 41 |
if( ! $campaign->is_wc_enabled() ) { |
| 42 |
continue; |
| 43 |
} |
| 44 |
if ( $this->has_campaign( $campaign, $product_id ) |
| 45 |
&& $this->met_conditions( $campaign, $product_id ) |
| 46 |
) { |
| 47 |
do_action( 'hurryt_pre_render', $campaign ); |
| 48 |
$callback = function () use ( $campaign ) { $this->render_shortcode( $campaign ); }; |
| 49 |
add_action( |
| 50 |
'woocommerce_single_product_summary', |
| 51 |
$callback, |
| 52 |
$campaign->getWcPosition() |
| 53 |
); |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Check if given product has an active campaign. |
| 60 |
* |
| 61 |
* @param Campaign $campaign |
| 62 |
* @param $product_id |
| 63 |
* |
| 64 |
* @return bool |
| 65 |
*/ |
| 66 |
public function has_campaign( $campaign, $product_id ) { |
| 67 |
|
| 68 |
$cat_ids = $this->get_product_categories( $product_id ); |
| 69 |
$type = $campaign->getWcProductsSelectionType(); |
| 70 |
$selection = $campaign->getWcProductsSelection(); |
| 71 |
switch ( $type ) { |
| 72 |
case C::WC_PS_TYPE_ALL: |
| 73 |
return true; |
| 74 |
|
| 75 |
case C::WC_PS_TYPE_INCLUDE_PRODUCTS: |
| 76 |
return in_array( $product_id, $selection ); |
| 77 |
|
| 78 |
case C::WC_PS_TYPE_EXCLUDE_PRODUCTS: |
| 79 |
return ! in_array( $product_id, $selection ); |
| 80 |
|
| 81 |
case C::WC_PS_TYPE_INCLUDE_CATEGORIES: |
| 82 |
$callback = function ( $id ) use ( $cat_ids ) { |
| 83 |
return in_array( $id, $cat_ids ); |
| 84 |
}; |
| 85 |
$results = array_filter( $selection, $callback ); |
| 86 |
|
| 87 |
return count( $results ) > 0; |
| 88 |
|
| 89 |
case C::WC_PS_TYPE_EXCLUDE_CATEGORIES: |
| 90 |
$results = array_filter( $selection, function ( $id ) use ( $cat_ids ) { |
| 91 |
return ! in_array( $id, $cat_ids ); |
| 92 |
} ); |
| 93 |
|
| 94 |
return count( $results ) > 0; |
| 95 |
|
| 96 |
default: |
| 97 |
return false; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Returns categories ids for the given product. |
| 103 |
* |
| 104 |
* @param $product_id |
| 105 |
* |
| 106 |
* @return array|\WP_Error |
| 107 |
*/ |
| 108 |
public function get_product_categories( $product_id ) { |
| 109 |
return wp_get_object_terms( $product_id, 'product_cat', [ |
| 110 |
'fields' => 'ids', |
| 111 |
] ); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Render shortcode for the given campaign ID. |
| 116 |
* |
| 117 |
* @param Campaign $campaign |
| 118 |
*/ |
| 119 |
public function render_shortcode( $campaign ) { |
| 120 |
$shortcode = '[hurrytimer id=' . $campaign->get_id() . ' ]'; |
| 121 |
echo do_shortcode( $shortcode ); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Change stock status. |
| 126 |
* |
| 127 |
* @param Campaign $campaign |
| 128 |
* @param int $stock_status |
| 129 |
*/ |
| 130 |
public function change_stock_status( $campaign, $stock_status ) { |
| 131 |
if ( ! $campaign->is_wc_enabled() ) { |
| 132 |
return; |
| 133 |
} |
| 134 |
|
| 135 |
$products = $this->get_campaign_products( $campaign ); |
| 136 |
foreach ( $products as $product ) { |
| 137 |
if ( $stock_status === C::WC_OUT_OF_STOCK |
| 138 |
&& apply_filters( 'hurryt_empty_stock', true, $product->get_id(), |
| 139 |
$campaign->get_id() ) |
| 140 |
) { |
| 141 |
wc_update_product_stock( 0 ); |
| 142 |
} |
| 143 |
wc_update_product_stock_status( $product->get_id(), $stock_status ); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Get campaign's products ID. |
| 149 |
* |
| 150 |
* @param Campaign $campaign |
| 151 |
* |
| 152 |
* @return array |
| 153 |
*/ |
| 154 |
function get_campaign_products( $campaign ) { |
| 155 |
/** |
| 156 |
* Selected products/categories ids. |
| 157 |
*/ |
| 158 |
$object_ids = $campaign->getWcProductsSelection(); |
| 159 |
/** |
| 160 |
* @see C |
| 161 |
*/ |
| 162 |
$type = $campaign->getWcProductsSelectionType(); |
| 163 |
/** |
| 164 |
* @var array $args Products query default args. |
| 165 |
*/ |
| 166 |
$args = [ 'limit' => -1, 'status' => 'publish' ]; |
| 167 |
switch ( $type ) { |
| 168 |
case C::WC_PS_TYPE_ALL: |
| 169 |
return wc_get_products( $args ); |
| 170 |
case C::WC_PS_TYPE_EXCLUDE_PRODUCTS: |
| 171 |
return wc_get_products( array_merge( $args, [ 'exclude' => $object_ids ] ) ); |
| 172 |
case C::WC_PS_TYPE_INCLUDE_PRODUCTS: |
| 173 |
return wc_get_products( array_merge( $args, [ 'include' => $object_ids ] ) ); |
| 174 |
case C::WC_PS_TYPE_INCLUDE_CATEGORIES: |
| 175 |
$cat_slugs = get_terms( [ |
| 176 |
'taxonomy' => 'product_cat', |
| 177 |
'include' => $object_ids, |
| 178 |
'fields' => 'slugs', |
| 179 |
] ); |
| 180 |
|
| 181 |
return wc_get_products( array_merge( $args, [ 'category' => $cat_slugs ] ) ); |
| 182 |
case C::WC_PS_TYPE_EXCLUDE_CATEGORIES: |
| 183 |
$cat_slugs = get_terms( [ |
| 184 |
'taxonomy' => 'product_cat', |
| 185 |
'exclude' => $object_ids, |
| 186 |
'fields' => 'slugs', |
| 187 |
] ); |
| 188 |
|
| 189 |
return wc_get_products( array_merge( $args, [ 'category' => $cat_slugs ] ) ); |
| 190 |
default: |
| 191 |
return []; |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
public function met_conditions( $campaign, $object_id ) { |
| 196 |
$groups = $campaign->getWcConditions(); |
| 197 |
|
| 198 |
if ( empty( $groups ) ) { |
| 199 |
return true; |
| 200 |
} |
| 201 |
|
| 202 |
$product = wc_get_product( $object_id ); |
| 203 |
$truthy_conditions = []; |
| 204 |
$truthy_groups = []; |
| 205 |
foreach ( $groups as $conditions ) { |
| 206 |
foreach ( $conditions as $condition ) { |
| 207 |
|
| 208 |
switch ( $condition[ 'key' ] ) { |
| 209 |
case 'stock_status': |
| 210 |
if ( $condition[ 'operator' ] === '==' ) { |
| 211 |
$truthy_conditions[] = $product->get_stock_status() |
| 212 |
== $condition[ 'value' ]; |
| 213 |
} |
| 214 |
if ( $condition[ 'operator' ] === '!=' ) { |
| 215 |
$truthy_conditions[] = $product->get_stock_status() |
| 216 |
!= $condition[ 'value' ]; |
| 217 |
} |
| 218 |
break; |
| 219 |
case 'stock_quantity': |
| 220 |
if ( $condition[ 'operator' ] === '==' ) { |
| 221 |
$truthy_conditions[] = $product->get_stock_quantity() |
| 222 |
== $condition[ 'value' ]; |
| 223 |
} |
| 224 |
if ( $condition[ 'operator' ] === '!=' ) { |
| 225 |
$truthy_conditions[] = $product->get_stock_quantity() |
| 226 |
!= $condition[ 'value' ]; |
| 227 |
} |
| 228 |
if ( $condition[ 'operator' ] === '>' ) { |
| 229 |
$truthy_conditions[] = $product->get_stock_quantity() |
| 230 |
> $condition[ 'value' ]; |
| 231 |
} |
| 232 |
if ( $condition[ 'operator' ] === '<' ) { |
| 233 |
$truthy_conditions[] = $product->get_stock_quantity() |
| 234 |
< $condition[ 'value' ]; |
| 235 |
} |
| 236 |
break; |
| 237 |
case 'shipping_class': |
| 238 |
if ( $condition[ 'operator' ] === '==' ) { |
| 239 |
$truthy_conditions[] = $product->get_shipping_class_id() |
| 240 |
== $condition[ 'value' ]; |
| 241 |
} |
| 242 |
if ( $condition[ 'operator' ] === '!=' ) { |
| 243 |
$truthy_conditions[] = $product->get_shipping_class_id() |
| 244 |
!= $condition[ 'value' ]; |
| 245 |
} |
| 246 |
break; |
| 247 |
case 'on_sale': |
| 248 |
$truthy_conditions[] = filter_var($condition[ 'value' ], FILTER_VALIDATE_BOOLEAN) === $product->is_on_sale(); |
| 249 |
|
| 250 |
break; |
| 251 |
|
| 252 |
default: |
| 253 |
|
| 254 |
throw new \Exception('Rule [' . $condition[ 'key' ] .'] not supported.'); |
| 255 |
|
| 256 |
} |
| 257 |
} |
| 258 |
$is_met = array_filter( $truthy_conditions, function ( $bool ) { |
| 259 |
return $bool === true; |
| 260 |
} ); |
| 261 |
|
| 262 |
$truthy_groups[] = count( $is_met ) === count( $conditions ); |
| 263 |
} |
| 264 |
$met = array_filter( $truthy_groups, function ( $bool ) { |
| 265 |
return $bool === true; |
| 266 |
} );; |
| 267 |
|
| 268 |
return ! empty( $met ); |
| 269 |
} |
| 270 |
} |
| 271 |
|