WJECF_AutoCoupon

The WooCommerce Extended Coupon Features WJECF AutoCoupon class.

Defined (1)

The class is defined in the following location(s).

/includes/wjecf-autocoupon.php  
  1. class WJECF_AutoCoupon extends Abstract_WJECF_Plugin { 
  2.  
  3. private $_autocoupons = null; 
  4.  
  5. private $_user_emails = null; 
  6.  
  7. protected $_executed_coupon_by_url = false; 
  8.  
  9. public function __construct() {  
  10. $this->set_plugin_data( array( 
  11. 'description' => __( 'Allow coupons to be automatically applied to the cart when restrictions are met or by url.', 'woocommerce-jos-autocoupon' ),  
  12. 'dependencies' => array(),  
  13. 'can_be_disabled' => true 
  14. ) ); 
  15.  
  16. public function init_hook() { 
  17. if ( ! class_exists('WC_Coupon') ) { 
  18. return; 
  19.  
  20. //Frontend hooks - logic 
  21. if ( WJECF_WC()->check_woocommerce_version('2.3.0')) { 
  22. add_action( 'woocommerce_after_calculate_totals', array( &$this, 'update_matched_autocoupons' ) );  
  23. } else { 
  24. //WC Versions prior to 2.3.0 don't have after_calculate_totals hook, this is a fallback 
  25. add_action( 'woocommerce_cart_updated', array( &$this, 'update_matched_autocoupons' ) );  
  26. add_action( 'woocommerce_check_cart_items', array( &$this, 'remove_unmatched_autocoupons' ) , 0, 0 ); //Remove coupon before WC does it and shows a message 
  27. //Last check for coupons with restricted_emails 
  28. add_action( 'woocommerce_checkout_update_order_review', array( &$this, 'fetch_billing_email' ), 10 ); // AJAX One page checkout  
  29.  
  30. //Frontend hooks - visualisation 
  31. add_filter('woocommerce_cart_totals_coupon_label', array( &$this, 'coupon_label' ), 10, 2 ); 
  32. add_filter('woocommerce_cart_totals_coupon_html', array( &$this, 'coupon_html' ), 10, 2 );  
  33.  
  34. //Inhibit redirect to cart when apply_coupon supplied 
  35. add_filter('option_woocommerce_cart_redirect_after_add', array ( &$this, 'option_woocommerce_cart_redirect_after_add') ); 
  36.  
  37. if ( ! is_ajax() ) { 
  38. //Get cart should not be called before the wp_loaded action nor the add_to_cart_action (class-wc-form-handler) 
  39. add_action( 'wp_loaded', array( &$this, 'coupon_by_url' ), 90 ); //Coupon through url 
  40.  
  41. /** ADMIN HOOKS */ 
  42. public function init_admin_hook() { 
  43. add_action( 'wjecf_woocommerce_coupon_options_extended_features', array( $this, 'admin_coupon_options_extended_features' ), 20, 2 ); 
  44. add_action( 'woocommerce_process_shop_coupon_meta', array( $this, 'process_shop_coupon_meta' ), 10, 2 ); 
  45.  
  46. //Inject columns 
  47. if ( WJECF()->is_pro() ) { 
  48. WJECF()->inject_coupon_column(  
  49. '_wjecf_auto_coupon',  
  50. __( 'Auto coupon', 'woocommerce-jos-autocoupon' ),  
  51. array( $this, 'admin_render_shop_coupon_columns' ), 'coupon_code' 
  52. );  
  53. WJECF()->inject_coupon_column(  
  54. '_wjecf_individual_use',  
  55. __( 'Individual use', 'woocommerce-jos-autocoupon' ),  
  56. array( $this, 'admin_render_shop_coupon_columns' ), 'coupon_code' 
  57. );  
  58.  
  59. add_filter( 'views_edit-shop_coupon', array( $this, 'admin_views_edit_coupon' ) ); 
  60. add_filter( 'request', array( $this, 'admin_request_query' ) ); 
  61.  
  62. /** 
  63. * Output a coupon custom column value 
  64. * @param string $column 
  65. * @param WP_Post The coupon post object 
  66. */ 
  67. public function admin_render_shop_coupon_columns( $column, $post ) { 
  68. $wrap_coupon = WJECF_Wrap( $post->ID ); 
  69.  
  70. switch ( $column ) { 
  71. case '_wjecf_auto_coupon' : 
  72. $is_auto_coupon = $wrap_coupon->get_meta( '_wjecf_is_auto_coupon', true ) == 'yes'; 
  73. echo $is_auto_coupon ? __( 'Yes', 'woocommerce' ) : __( 'No', 'woocommerce' ); 
  74. if ( $is_auto_coupon ) { 
  75. $prio = $wrap_coupon->get_meta( '_wjecf_coupon_priority', true ); 
  76. if ( $prio ) echo " (" . intval( $prio ) . ")"; 
  77. break; 
  78. case '_wjecf_individual_use' : 
  79. $individual = $wrap_coupon->get_individual_use(); 
  80. echo $individual ? __( 'Yes', 'woocommerce' ) : __( 'No', 'woocommerce' ); 
  81. break; 
  82.  
  83. public function admin_views_edit_coupon( $views ) { 
  84. global $post_type, $wp_query; 
  85.  
  86. $class = ( isset( $wp_query->query['meta_key'] ) && $wp_query->query['meta_key'] == '_wjecf_is_auto_coupon' ) ? 'current' : ''; 
  87. $query_string = remove_query_arg(array( 'wjecf_is_auto_coupon' )); 
  88. $query_string = add_query_arg( 'wjecf_is_auto_coupon', '1', $query_string ); 
  89. $views['wjecf_is_auto_coupon'] = '<a href="' . esc_url( $query_string ) . '" class="' . esc_attr( $class ) . '">' . __( 'Auto coupons', 'woocommerce' ) . '</a>'; 
  90.  
  91. return $views; 
  92.  
  93. /** 
  94. * Filters and sorting handler 
  95. * @param array $vars 
  96. * @return array 
  97. */ 
  98. public function admin_request_query( $vars ) { 
  99. global $typenow, $wp_query, $wp_post_statuses; 
  100.  
  101. if ( 'shop_coupon' === $typenow ) { 
  102. if ( isset( $_GET['wjecf_is_auto_coupon'] ) ) { 
  103. $vars['meta_key'] = '_wjecf_is_auto_coupon'; 
  104. $vars['meta_value'] = $_GET['wjecf_is_auto_coupon'] == '1' ? 'yes' : 'no'; 
  105.  
  106. return $vars; 
  107.  
  108. public function admin_coupon_options_extended_features( $thepostid, $post ) { 
  109.  
  110. //============================= 
  111. //Title 
  112. echo "<h3>" . esc_html( __( 'Auto coupon', 'woocommerce-jos-autocoupon' ) ). "</h3>\n"; 
  113.  
  114.  
  115. //============================= 
  116. // Auto coupon checkbox 
  117. woocommerce_wp_checkbox( array( 
  118. 'id' => '_wjecf_is_auto_coupon',  
  119. 'label' => __( 'Auto coupon', 'woocommerce-jos-autocoupon' ),  
  120. 'description' => __( "Automatically add the coupon to the cart if the restrictions are met. Please enter a description when you check this box, the description will be shown in the customer's cart if the coupon is applied.", 'woocommerce-jos-autocoupon' ) 
  121. ) ); 
  122.  
  123. echo '<div class="_wjecf_show_if_autocoupon">'; 
  124. if ( WJECF()->is_pro() ) {  
  125. // Maximum quantity of matching products (product/category) 
  126. woocommerce_wp_text_input( array(  
  127. 'id' => '_wjecf_coupon_priority',  
  128. 'label' => __( 'Priority', 'woocommerce-jos-autocoupon' ),  
  129. 'placeholder' => __( 'No priority', 'woocommerce' ),  
  130. 'description' => __( 'When \'individual use\' is checked, auto coupons with a higher value will have priority over other auto coupons.', 'woocommerce-jos-autocoupon' ),  
  131. 'data_type' => 'decimal',  
  132. 'desc_tip' => true 
  133. ) );  
  134. }  
  135.  
  136. //============================= 
  137. // Apply without notice 
  138. woocommerce_wp_checkbox( array( 
  139. 'id' => '_wjecf_apply_silently',  
  140. 'label' => __( 'Apply silently', 'woocommerce-jos-autocoupon' ),  
  141. 'description' => __( "Don't display a message when this coupon is automatically applied.", 'woocommerce-jos-autocoupon' ),  
  142. ) ); 
  143. echo '</div>'; 
  144.  
  145. ?>  
  146. <script type="text/javascript"> 
  147. //Hide/show when AUTO-COUPON value changes 
  148. function update_wjecf_apply_silently_field( animation ) {  
  149. if ( animation === undefined ) animation = 'slow'; 
  150.  
  151. if (jQuery("#_wjecf_is_auto_coupon").prop('checked')) { 
  152. jQuery("._wjecf_show_if_autocoupon").show( animation ); 
  153. } else { 
  154. jQuery("._wjecf_show_if_autocoupon").hide( animation ); 
  155. update_wjecf_apply_silently_field( 0 );  
  156.  
  157. jQuery("#_wjecf_is_auto_coupon").click( update_wjecf_apply_silently_field ); 
  158. </script> 
  159. <?php 
  160.  
  161.  
  162. public function process_shop_coupon_meta( $post_id, $post ) { 
  163. $wrap_coupon = WJECF_Wrap( $post_id ); 
  164.  
  165. $autocoupon = isset( $_POST['_wjecf_is_auto_coupon'] ); 
  166. $wrap_coupon->set_meta( '_wjecf_is_auto_coupon', $autocoupon ? 'yes' : 'no' ); 
  167. $wrap_coupon->set_meta( '_wjecf_apply_silently', isset( $_POST['_wjecf_apply_silently'] ) ? 'yes' : 'no' ); 
  168. if ( WJECF()->is_pro() ) { 
  169. $wrap_coupon->set_meta( '_wjecf_coupon_priority', intval( $_POST['_wjecf_coupon_priority'] ) ); 
  170.  
  171. $wrap_coupon->save(); 
  172. }  
  173.  
  174. /** FRONTEND HOOKS */ 
  175.  
  176. /** 
  177. ** Inhibit redirect to cart when apply_coupon supplied 
  178. */ 
  179. public function option_woocommerce_cart_redirect_after_add ( $value ) { 
  180. if ( ! $this->_executed_coupon_by_url && isset( $_GET['apply_coupon'] ) ) { 
  181. $value = 'no'; 
  182. return $value; 
  183.  
  184. /** 
  185. * Add coupon through url 
  186. */ 
  187. public function coupon_by_url() { 
  188. $must_redirect = false; 
  189.  
  190. //Apply coupon by url 
  191. if ( isset( $_GET['apply_coupon'] ) ) { 
  192. $must_redirect = true; 
  193. $this->_executed_coupon_by_url = true; 
  194. $split = explode( ", ", wc_clean( $_GET['apply_coupon'] ) ); 
  195. //2.2.2 Make sure a session cookie is set 
  196. if( ! WC()->session->has_session() ) 
  197. WC()->session->set_customer_session_cookie( true ); 
  198.  
  199. $cart = WC()->cart; 
  200. foreach ( $split as $coupon_code ) { 
  201. $coupon = WJECF_WC()->get_coupon( $coupon_code ); 
  202. if ( WJECF_WC()->check_woocommerce_version('2.3.0') && ! WJECF_Wrap( $coupon )->exists() ) { 
  203. wc_add_notice( $coupon->get_coupon_error( WC_Coupon::E_WC_COUPON_NOT_EXIST ), 'error' ); 
  204. } else { 
  205. $valid = $coupon->is_valid(); 
  206. if ( $valid ) { 
  207. $cart->add_discount( $coupon_code ); 
  208.  
  209. //Redirect to page without autocoupon query args 
  210. if ( $must_redirect ) { 
  211. $requested_url = is_ssl() ? 'https://' : 'http://'; 
  212. $requested_url .= $_SERVER['HTTP_HOST'];  
  213. $requested_url .= $_SERVER['REQUEST_URI']; 
  214.  
  215. wp_safe_redirect( remove_query_arg( array( 'apply_coupon', 'add-to-cart' ), ( $requested_url ) ) ); 
  216. exit; 
  217.  
  218. /** 
  219. * Overwrite the html created by wc_cart_totals_coupon_label() so a descriptive text will be shown for the discount. 
  220. * @param string $originaltext The default text created by wc_cart_totals_coupon_label() 
  221. * @param WC_Coupon $coupon The coupon data 
  222. * @return string The overwritten text 
  223. */  
  224. function coupon_label( $originaltext, $coupon ) { 
  225.  
  226. if ( $this->is_auto_coupon($coupon) ) { 
  227.  
  228. return $this->coupon_excerpt($coupon); //__($this->autocoupons[$coupon->code], 'woocommerce-jos-autocoupon'); 
  229. } else { 
  230. return $originaltext; 
  231.  
  232. /** 
  233. * Overwrite the html created by wc_cart_totals_coupon_html(). This function is required to remove the "Remove" link. 
  234. * @param string $originaltext The html created by wc_cart_totals_coupon_html() 
  235. * @param WC_Coupon $coupon The coupon data 
  236. * @return string The overwritten html 
  237. */ 
  238. function coupon_html( $originaltext, $coupon ) { 
  239. if ( $this->is_auto_coupon($coupon) ) { 
  240. $value = array(); 
  241.  
  242. if ( $amount = WC()->cart->get_coupon_discount_amount( WJECF_Wrap( $coupon )->get_code(), WC()->cart->display_cart_ex_tax ) ) { 
  243. $discount_html = '-' . wc_price( $amount ); 
  244. } else { 
  245. $discount_html = ''; 
  246.  
  247. $value[] = apply_filters( 'woocommerce_coupon_discount_amount_html', $discount_html, $coupon ); 
  248.  
  249. if ( WJECF_Wrap( $coupon )->get_free_shipping() ) { 
  250. $value[] = __( 'Free shipping coupon', 'woocommerce' ); 
  251.  
  252. return implode(', ', array_filter($value)); //Remove empty array elements 
  253. } else { 
  254. return $originaltext; 
  255. }  
  256.  
  257.  
  258. function remove_unmatched_autocoupons( $valid_coupon_codes = null ) { 
  259. if ( $valid_coupon_codes === null ) { 
  260. //Get the coupons that should be in the cart 
  261. $valid_coupons = $this->get_valid_auto_coupons(); 
  262. $valid_coupons = $this->individual_use_filter( $valid_coupons ); 
  263. $valid_coupon_codes = array();  
  264. foreach ( $valid_coupons as $coupon ) { 
  265. $valid_coupon_codes[] = WJECF_Wrap( $coupon )->get_code(); 
  266.  
  267. //Remove invalids 
  268. $calc_needed = false;  
  269. foreach ( $this->get_all_auto_coupons() as $coupon ) { 
  270. $coupon_code = WJECF_Wrap( $coupon )->get_code(); 
  271. if ( WC()->cart->has_discount( $coupon_code ) && ! in_array( $coupon_code, $valid_coupon_codes ) ) { 
  272. $this->log( sprintf( "Removing %s", $coupon_code ) ); 
  273. WC()->cart->remove_coupon( $coupon_code );  
  274. $calc_needed = true; 
  275. return $calc_needed; 
  276.  
  277. private $update_matched_autocoupons_executed = false; 
  278.  
  279. /** 
  280. * Apply matched autocoupons and remove unmatched autocoupons. 
  281. * @return void 
  282. */  
  283. function update_matched_autocoupons() { 
  284. if ( $this->update_matched_autocoupons_executed ) { 
  285. return; 
  286. $this->update_matched_autocoupons_executed = true; 
  287. $this->log( "()" ); 
  288.  
  289. //2.3.3 Keep track of queued coupons and apply when they validate 
  290. $queuer = WJECF()->get_plugin('WJECF_Pro_Coupon_Queueing'); 
  291. if ( $queuer !== false ) { 
  292. $queuer->apply_valid_queued_coupons(); 
  293.  
  294. //Get the coupons that should be in the cart 
  295. $valid_coupons = $this->get_valid_auto_coupons(); 
  296. $valid_coupons = $this->individual_use_filter( $valid_coupons ); 
  297.  
  298. $valid_coupon_codes = array();  
  299. foreach ( $valid_coupons as $coupon ) { 
  300. $valid_coupon_codes[] = WJECF_Wrap( $coupon )->get_code(); 
  301.  
  302. $this->log( sprintf( "Auto coupons that should be in cart: %s", implode( ', ', $valid_coupon_codes ) ) ); 
  303.  
  304. $calc_needed = $this->remove_unmatched_autocoupons( $valid_coupon_codes ); 
  305.  
  306. //Add valids 
  307. foreach( $valid_coupons as $coupon ) { 
  308. $coupon_code = WJECF_Wrap( $coupon )->get_code(); 
  309. if ( ! WC()->cart->has_discount( $coupon_code ) ) { 
  310. $this->log( sprintf( "Applying auto coupon %s", $coupon_code ) ); 
  311.  
  312. $apply_silently = WJECF_Wrap( $coupon )->get_meta( '_wjecf_apply_silently' ) == 'yes'; 
  313.  
  314. if ( $apply_silently ) { 
  315. $new_succss_msg = ''; // no message 
  316. } else { 
  317. $coupon_excerpt = $this->coupon_excerpt($coupon); 
  318. $new_succss_msg = sprintf( 
  319. __("Discount applied: %s", 'woocommerce-jos-autocoupon'),  
  320. __( empty( $coupon_excerpt ) ? $coupon_code : $coupon_excerpt, 'woocommerce-jos-autocoupon') 
  321. ); 
  322.  
  323. WJECF()->start_overwrite_success_message( $coupon, $new_succss_msg ); 
  324. WC()->cart->add_discount( $coupon_code ); //Causes calculation and will remove other coupons if it's a individual coupon 
  325. WJECF()->stop_overwrite_success_message(); 
  326.  
  327. $calc_needed = false; //Already done by adding the discount 
  328.  
  329.  
  330. $this->log( 'Coupons in cart: ' . implode( ', ', WC()->cart->applied_coupons ) . ($calc_needed ? ". RECALC" : "") ); 
  331.  
  332. if ( $calc_needed ) { 
  333. WC()->cart->calculate_totals(); 
  334.  
  335.  
  336. private function get_valid_auto_coupons( ) { 
  337. $valid_coupons = array(); 
  338. foreach ( $this->get_all_auto_coupons() as $coupon ) { 
  339. if ( $this->coupon_can_be_applied( $coupon ) && $this->coupon_has_a_value( $coupon ) ) { 
  340. $valid_coupons[] = $coupon; 
  341. return $valid_coupons; 
  342. }  
  343.  
  344. /** 
  345. * Test whether the coupon is valid and has a discount > 0  
  346. * @return bool 
  347. */ 
  348. function coupon_can_be_applied( $coupon ) { 
  349. $wrap_coupon = WJECF_Wrap( $coupon ); 
  350. $can_be_applied = true; 
  351.  
  352. //Test validity 
  353. if ( ! $coupon->is_valid() ) { 
  354. $can_be_applied = false; 
  355. //Test restricted emails 
  356. //See WooCommerce: class-wc-cart.php function check_customer_coupons 
  357. else if ( $can_be_applied && is_array( $wrap_coupon->get_email_restrictions() ) && sizeof( $wrap_coupon->get_email_restrictions() ) > 0 ) { 
  358. $user_emails = array_map( 'sanitize_email', array_map( 'strtolower', $this->get_user_emails() ) ); 
  359. $coupon_emails = array_map( 'sanitize_email', array_map( 'strtolower', $wrap_coupon->get_email_restrictions() ) ); 
  360.  
  361. if ( 0 == sizeof( array_intersect( $user_emails, $coupon_emails ) ) ) { 
  362. $can_be_applied = false; 
  363. return apply_filters( 'wjecf_coupon_can_be_applied', $can_be_applied, $coupon ); 
  364.  
  365.  
  366. /** 
  367. * Does the coupon have a value? (autocoupon should not be applied if it has no value) 
  368. * @param WC_Coupon $coupon The coupon data 
  369. * @return bool True if it has a value (discount, free shipping, whatever) otherwise false) 
  370. **/ 
  371. function coupon_has_a_value( $coupon ) { 
  372.  
  373. $has_a_value = false; 
  374.  
  375. if ( WJECF_Wrap( $coupon )->get_free_shipping() ) { 
  376. $has_a_value = true; 
  377. } else { 
  378. //Test whether discount > 0 
  379. //See WooCommerce: class-wc-cart.php function get_discounted_price 
  380. global $woocommerce; 
  381. foreach ( $woocommerce->cart->get_cart() as $cart_item ) { 
  382. if ( $coupon->is_valid_for_cart() || $coupon->is_valid_for_product( $cart_item['data'], $cart_item ) ) { 
  383. $has_a_value = true; 
  384. break; 
  385.  
  386. return apply_filters( 'wjecf_coupon_has_a_value', $has_a_value, $coupon ); 
  387.  
  388.  
  389.  
  390.  
  391. /** 
  392. * Check wether the coupon is an "Auto coupon". 
  393. * @param WC_Coupon $coupon The coupon data 
  394. * @return bool true if it is an "Auto coupon" 
  395. */  
  396. public function is_auto_coupon($coupon) { 
  397. return WJECF_Wrap( $coupon )->get_meta( '_wjecf_is_auto_coupon' ) == 'yes'; 
  398.  
  399. private function get_coupon_priority($coupon) { 
  400. if ( WJECF()->is_pro() ) {  
  401. $prio = WJECF_Wrap( $coupon )->get_meta( '_wjecf_coupon_priority' ); 
  402. if ( ! empty( $prio ) ) { 
  403. return intval( $prio ); 
  404. return 0; 
  405. }  
  406.  
  407.  
  408. /** 
  409. * Get the coupon excerpt (description) 
  410. * @param WC_Coupon $coupon The coupon data 
  411. * @return string The excerpt (translated) 
  412. */  
  413. private function coupon_excerpt($coupon) { 
  414. $my_post = get_post( WJECF_Wrap( $coupon )->get_id() ); 
  415. return __( $my_post->post_excerpt, 'woocommerce-jos-autocoupon' ); 
  416. }  
  417.  
  418. /** 
  419. * Get a list of the users' known email addresses 
  420. */ 
  421. private function get_user_emails() { 
  422. if ( ! is_array($this->_user_emails) ) { 
  423. $this->_user_emails = array(); 
  424. //Email of the logged in user 
  425. if ( is_user_logged_in() ) { 
  426. $current_user = wp_get_current_user(); 
  427. $this->_user_emails[] = $current_user->user_email; 
  428.  
  429. if ( isset( $_POST['billing_email'] ) ) 
  430. $this->_user_emails[] = $_POST['billing_email']; 
  431. //$this->log( "User emails: " . implode( ", ", $this->_user_emails ) ); 
  432. return $this->_user_emails;  
  433.  
  434. /** 
  435. * Append a single or an array of email addresses. 
  436. * @param array|string $append_emails The email address(es) to be added 
  437. * @return void 
  438. */ 
  439. private function append_user_emails($append_emails) { 
  440. //$append_emails must be an array 
  441. if ( ! is_array( $append_emails ) ) { 
  442. $append_emails = array( $append_emails ); 
  443. $this->_user_emails = array_unique( array_merge( $this->get_user_emails(), $append_emails ) ); 
  444. //$this->log('Append emails: ' . implode( ', ', $append_emails ) ); 
  445.  
  446. public function fetch_billing_email( $post_data ) { 
  447. //post_data can be an array, or a query=string&like=this 
  448. if ( ! is_array( $post_data ) ) { 
  449. parse_str( $post_data, $posted ); 
  450. } else { 
  451. $posted = $post_data; 
  452.  
  453. if ( isset ( $posted['billing_email'] ) ) { 
  454. $this->append_user_emails( $posted['billing_email'] ); 
  455.  
  456.  
  457. /** 
  458. * Return an array of WC_Coupons with coupons that shouldn't cause individual use conflicts 
  459. */ 
  460. private function individual_use_filter( $valid_auto_coupons ) { 
  461. $filtered = array(); 
  462.  
  463. //Any individual use non-autocoupons in the cart? 
  464. foreach ( WC()->cart->get_applied_coupons() as $coupon_code ) { 
  465. $coupon = new WC_Coupon( $coupon_code ); 
  466. if ( WJECF_Wrap( $coupon )->get_individual_use() && ! $this->is_auto_coupon( $coupon ) ) { 
  467. return $filtered; //Don't allow any auto coupon 
  468. foreach ( $valid_auto_coupons as $coupon ) { 
  469. if ( ! WJECF_Wrap( $coupon )->get_individual_use() || empty( $filtered ) ) { 
  470. $filtered[] = $coupon; 
  471. if ( WJECF_Wrap( $coupon )->get_individual_use() ) { 
  472. break; 
  473. return $filtered; 
  474. }  
  475.  
  476. /** 
  477. * Get a list of all auto coupon codes 
  478. * @return array All auto coupon codes 
  479. */  
  480. public function get_all_auto_coupons() { 
  481. if ( ! is_array( $this->_autocoupons ) ) { 
  482. $this->_autocoupons = array(); 
  483.  
  484. $query_args = array( 
  485. 'posts_per_page' => -1,  
  486. 'post_type' => 'shop_coupon',  
  487. 'post_status' => 'publish',  
  488. 'orderby' => array( 'title' => 'ASC' ),  
  489. 'meta_query' => array( 
  490. array( 
  491. 'key' => '_wjecf_is_auto_coupon',  
  492. 'compare' => '=',  
  493. 'value' => 'yes',  
  494. ),  
  495. ); 
  496.  
  497. $query = new WP_Query($query_args); 
  498. foreach ($query->posts as $post) { 
  499. $coupon = new WC_Coupon($post->post_title); 
  500. if ( $this->is_auto_coupon($coupon) ) { 
  501. $this->_autocoupons[ WJECF_Wrap( $coupon )->get_code() ] = $coupon; 
  502.  
  503. //Sort by priority 
  504. @uasort( $this->_autocoupons , array( $this, 'sort_auto_coupons' ) ); //Ignore error PHP Bug #50688 
  505.  
  506. $coupon_codes = array(); 
  507. foreach( $this->_autocoupons as $coupon ) { 
  508. $coupon_codes[] = WJECF_Wrap( $coupon )->get_code(); 
  509.  
  510. $this->log( "Autocoupons: " . implode(", ", $coupon_codes ) ); 
  511.  
  512. return $this->_autocoupons; 
  513.  
  514. /** 
  515. * Compare function to sort coupons by priority 
  516. * @param type $a  
  517. * @param type $b  
  518. * @return type 
  519. */ 
  520. private function sort_auto_coupons( $coupon_a, $coupon_b ) { 
  521. $prio_a = $this->get_coupon_priority( $coupon_a ); 
  522. $prio_b = $this->get_coupon_priority( $coupon_b ); 
  523. $this->log("A: $prio_a B: $prio_b "); 
  524. if ( $prio_a == $prio_b ) { 
  525. return $a->code < $b->code ? -1 : 1; //By title ASC 
  526. } else { 
  527. return $prio_a > $prio_b ? -1 : 1; //By prio DESC