/** * Automatically apply coupons. * * Also removes auto-apply coupons (although that should happen * automatically, it seems we're on the safer side doing that as well * here). */ public static function wp_init() { global $wpdb, $woocommerce; if (isset($woocommerce) && isset($woocommerce->cart) && $woocommerce->cart->coupons_enabled()) { $coupons = $wpdb->get_results("SELECT DISTINCT ID, post_title FROM {$wpdb->posts} LEFT JOIN {$wpdb->postmeta} ON {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id WHERE {$wpdb->posts}.post_status = 'publish' AND {$wpdb->postmeta}.meta_key = '_vd_auto'"); if ($coupons && count($coupons) > 0) { foreach ($coupons as $coupon) { $coupon_code = $coupon->post_title; $coupon = new WC_Coupon($coupon_code); if ($coupon->id) { if ($coupon->is_valid()) { if (!$woocommerce->cart->has_discount($coupon_code)) { $woocommerce->cart->add_discount($coupon_code); $msg = ''; $message = get_post_meta($coupon->id, '_vd_auto_message_display', true); $show_description = get_post_meta($coupon->id, '_vd_auto_description_display', true) == 'yes'; $show_info = get_post_meta($coupon->id, '_vd_auto_info_display', true) == 'yes'; if (!empty($message)) { $msg .= sprintf('<div class="coupon display message %s">', wp_strip_all_tags($coupon->code)); $msg .= stripslashes(wp_filter_kses($message)); $msg .= '</div>'; } if ($show_description) { if ($post = get_post($coupon->id)) { if (!empty($post->post_excerpt)) { $msg .= sprintf('<div class="coupon display description %s">', wp_strip_all_tags($coupon->code)); $msg .= stripslashes(wp_filter_kses($post->post_excerpt)); $msg .= '</div>'; } } } if ($show_info) { $msg .= sprintf('<div class="coupon display volume-discount %s">', wp_strip_all_tags($coupon->code)); $msg .= WooCommerce_Volume_Discount_Coupons_Shortcodes::get_volume_discount_info($coupon); $msg .= '</div>'; } if (!empty($msg)) { $woocommerce->add_message($msg); } } } else { if ($woocommerce->cart->has_discount($coupon_code)) { if (!empty($woocommerce->cart->applied_coupons)) { foreach ($woocommerce->cart->applied_coupons as $index => $code) { if ($coupon_code == $code) { unset($woocommerce->cart->applied_coupons[$index]); } } } } } } } } } }
/** * Render coupon description/info for product. * @param WC_Product $product * @return string */ public static function render_info($product) { global $wpdb; $output = ''; // Get all coupons that want to modify their display, // i.e. those that have _vd_description_display or // _vd_info_display set. $coupons = $wpdb->get_results("SELECT DISTINCT ID, post_title FROM {$wpdb->posts} " . "LEFT JOIN {$wpdb->postmeta} ON {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id " . "WHERE TRUE " . "AND {$wpdb->posts}.post_type = 'shop_coupon' " . "AND {$wpdb->posts}.post_status = 'publish' " . "AND " . "( " . "{$wpdb->postmeta}.meta_key = '_vd_description_display' " . "OR " . "{$wpdb->postmeta}.meta_key = '_vd_info_display' " . ") "); if ($coupons && count($coupons) > 0) { $product_term_ids = array_map('intval', wp_get_post_terms($product->id, 'product_cat', array('fields' => 'ids'))); foreach ($coupons as $coupon) { $show_description = false; $show_info = false; $coupon_product_ids = array_map('intval', get_post_meta($coupon->ID, '_vd_product_ids', false)); $coupon_term_ids = array_map('intval', get_post_meta($coupon->ID, '_vd_term_ids', false)); // Check if the coupon applies to the product or it is within // its related product categories. if (in_array(intval($product->id), $coupon_product_ids) || count(array_intersect($product_term_ids, $coupon_term_ids)) > 0) { $description_display = get_post_meta($coupon->ID, '_vd_description_display', false); $info_display = get_post_meta($coupon->ID, '_vd_info_display', false); $show_description_anywhere = in_array('anywhere', $description_display); $show_info_anywhere = in_array('anywhere', $info_display); if (is_product()) { $show_description = $show_description_anywhere || in_array('single', $description_display); $show_info = $show_info_anywhere || in_array('single', $info_display); } if (is_product_category()) { $show_description = $show_description_anywhere || in_array('product_cat', $description_display); $show_info = $show_info_anywhere || in_array('product_cat', $info_display); } if (is_product_tag()) { $show_description = $show_description_anywhere || in_array('product_tag', $description_display); $show_info = $show_info_anywhere || in_array('product_tag', $info_display); } if (is_post_type_archive('product')) { $show_description = $show_description_anywhere || in_array('archive', $description_display); $show_info = $show_info_anywhere || in_array('archive', $info_display); } if (is_shop()) { $show_description = $show_description_anywhere || in_array('shop', $description_display); $show_info = $show_info_anywhere || in_array('shop', $info_display); } if ($show_description || $show_info) { $output .= self::inline_styles(); $coupon = new WC_Coupon($coupon->post_title); if ($coupon->id) { if ($show_description) { if ($post = get_post($coupon->id)) { if (!empty($post->post_excerpt)) { $output .= sprintf('<div class="coupon display description %s">', wp_strip_all_tags($coupon->code)); $output .= stripslashes(wp_filter_kses($post->post_excerpt)); $output .= '</div>'; } } } if ($show_info) { $output .= sprintf('<div class="coupon display volume-discount %s">', wp_strip_all_tags($coupon->code)); $output .= WooCommerce_Volume_Discount_Coupons_Shortcodes::get_volume_discount_info($coupon); $output .= '</div>'; } } } } } } return $output; }
// .volume-discount $output .= '</div>'; // .volume-discount-container } return $output; } /** * Coupon comparison by id. * * @param WC_Coupon $a * @param WC_Coupon $b * @return int */ public static function by_id($a, $b) { return $a->id - $b->id; } /** * Coupon comparison by code. * * @param WC_Coupon $a * @param WC_Coupon $b * @return int */ public static function by_code($a, $b) { return strcmp($a->code, $b->code); } } WooCommerce_Volume_Discount_Coupons_Shortcodes::init();
/** * Renders information about the discount for coupon(s). * * @param array $atts * @param string $content not used * @return string */ public static function coupon_discount($atts, $content = null) { $output = ''; $options = shortcode_atts(array('coupon' => null, 'code' => null, 'separator' => ' ', 'element_tag' => 'span', 'renderer' => 'auto', 'prefix' => null, 'prefix_separator' => ' '), $atts); switch ($options['element_tag']) { case 'li': case 'span': case 'div': case 'p': $element_tag = $options['element_tag']; break; default: $element_tag = 'span'; } $prefix_code = false; if ($options['prefix'] == 'code') { $prefix_code = true; } $elements = array(); $codes = self::get_codes($options); foreach ($codes as $code) { $element_output = ''; $coupon = new WC_Coupon($code); if ($coupon->id) { $element_output .= sprintf('<%s class="coupon discount %s">', stripslashes(wp_strip_all_tags($element_tag)), stripslashes(wp_strip_all_tags($coupon->code))); $renderer = null; if ($options['renderer'] == 'auto') { // WooCommerce_Coupons_Countdown_Shortcodes // does not differ // WooCommerce_Groupons_Shortcodes // does not differ // WooCommerce_Volume_Discount_Coupons_Shortcodes if (class_exists('WooCommerce_Volume_Discount_Coupons_Shortcodes')) { $min = get_post_meta($coupon->id, '_vd_min', true); $max = get_post_meta($coupon->id, '_vd_max', true); if ($min > 0 || $max > 0) { $renderer = 'WooCommerce_Volume_Discount_Coupons_Shortcodes'; } } } if ($prefix_code) { $element_output .= sprintf('<span class="coupon code %s">', stripslashes(wp_strip_all_tags($coupon->code))); $element_output .= stripslashes(wp_strip_all_tags($coupon->code)); $element_output .= '</span>'; $element_output .= stripslashes(wp_filter_kses($options['prefix_separator'])); } if ($renderer === null) { $element_output .= self::get_discount_info($coupon, $atts); } else { switch ($renderer) { case 'WooCommerce_Volume_Discount_Coupons_Shortcodes': $element_output .= WooCommerce_Volume_Discount_Coupons_Shortcodes::get_volume_discount_info($coupon); break; } } $element_output .= sprintf('</%s>', stripslashes(wp_strip_all_tags($element_tag))); } if (!empty($element_output)) { $elements[] = $element_output; } } if ($element_tag == 'li') { $output .= '<ul>'; } $output .= implode(stripslashes(wp_filter_kses($options['separator'])), $elements); if ($element_tag == 'li') { $output .= '</ul>'; } return $output; }