Version Description
- First version ever!
Download this release
Release Info
Developer | josk79 |
Plugin | WooCommerce Extended Coupon Features |
Version | 1.0 |
Comparing to | |
See all releases |
Version 1.0
- assets/Thumbs.db +0 -0
- assets/screenshot-1.png +0 -0
- readme.txt +54 -0
- woocommerce-jos-autocoupon.php +241 -0
assets/Thumbs.db
ADDED
Binary file
|
assets/screenshot-1.png
ADDED
Binary file
|
readme.txt
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
=== Plugin Name ===
|
2 |
+
Contributors: josk79
|
3 |
+
Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=5T9XQBCS2QHRY&lc=NL&item_name=Jos%20Koenis&item_number=wordpress%2dplugin¤cy_code=EUR&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHosted
|
4 |
+
Tags: woocommerce, coupons, discount
|
5 |
+
Requires at least: 3.0.1
|
6 |
+
Tested up to: 3.9.1
|
7 |
+
Stable tag: 1.0
|
8 |
+
License: GPLv2 or later
|
9 |
+
License URI: http://www.gnu.org/licenses/gpl-2.0.html
|
10 |
+
|
11 |
+
Allow discounts to be automatically added to the WooCommerce cart when it's restrictions are met. Coupons code will not be presented to the user.
|
12 |
+
|
13 |
+
== Description ==
|
14 |
+
|
15 |
+
"Jos - WooCommerce auto added coupons" allows you to select coupons that will automatically be added to
|
16 |
+
the users cart if it's restrictions are met. The coupon will be removed when the restrictions are not met.
|
17 |
+
|
18 |
+
The discount will be presented to the user by a descriptive text. No coupon code will be shown.
|
19 |
+
|
20 |
+
No programming required.
|
21 |
+
|
22 |
+
This plugin has been tested with WordPress 3.9.1 and WooCommerce 2.1.11. Also in combination with WPML.
|
23 |
+
|
24 |
+
= How to create an automatically added coupon? =
|
25 |
+
|
26 |
+
1. Create a coupon through the 'Coupons' menu in WooCommerce. TIP: Name it auto_'whatever' so it will be easy to recognize the auto coupons
|
27 |
+
2. Setup the coupon as you'd normally would. Make sure you enter a description for the coupon and set usage restrictions
|
28 |
+
3. In the "Usage Restriction" tab, check the box "Auto coupon"
|
29 |
+
4. Voila! That's it
|
30 |
+
|
31 |
+
== Installation ==
|
32 |
+
|
33 |
+
1. Upload the plugin in the `/wp-content/plugins/` directory, or automatically install it through the 'New Plugin' menu in WordPress
|
34 |
+
2. Activate the plugin through the 'Plugins' menu in WordPress
|
35 |
+
|
36 |
+
== Frequently Asked Questions ==
|
37 |
+
|
38 |
+
= Is the plugin translatable? =
|
39 |
+
|
40 |
+
Yes, all frontend string values are translatable with WPML. Translatable items appear in the context `woocommerce-jos-autocoupon` in "String Translations".
|
41 |
+
|
42 |
+
= Can I make a donation? =
|
43 |
+
|
44 |
+
Sure! [This](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=5T9XQBCS2QHRY&lc=NL&item_name=Jos%20Koenis&item_number=wordpress%2dplugin¤cy_code=EUR&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHosted) is the link.
|
45 |
+
|
46 |
+
== Screenshots ==
|
47 |
+
|
48 |
+
1. Simply use the WooCommerce Coupons menu to make a coupon an "auto coupon".
|
49 |
+
|
50 |
+
== Changelog ==
|
51 |
+
|
52 |
+
= 1.0 =
|
53 |
+
* First version ever!
|
54 |
+
|
woocommerce-jos-autocoupon.php
ADDED
@@ -0,0 +1,241 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?php
|
2 |
+
/**
|
3 |
+
* Plugin Name: WooCommerce auto added coupons
|
4 |
+
* Plugin URI: none yet
|
5 |
+
* Description: Automatically add certain coupons to the cart if it's conditions are met.
|
6 |
+
* Version: 1.0
|
7 |
+
* Author: Jos Koenis
|
8 |
+
* License: GPL2
|
9 |
+
*/
|
10 |
+
|
11 |
+
defined('ABSPATH') or die();
|
12 |
+
|
13 |
+
class WC_Jos_AutoCoupon_Controller{
|
14 |
+
|
15 |
+
private $meta_key = 'woocommerce-jos-autocoupon';
|
16 |
+
|
17 |
+
private $_autocoupon_codes = null;
|
18 |
+
|
19 |
+
public function __construct() {
|
20 |
+
add_action('init', array( &$this, 'controller_init' ));
|
21 |
+
}
|
22 |
+
|
23 |
+
public function controller_init() {
|
24 |
+
if ( ! class_exists('WC_Coupon') ) {
|
25 |
+
return;
|
26 |
+
}
|
27 |
+
|
28 |
+
//Admin hooks
|
29 |
+
add_action( 'woocommerce_coupon_options_usage_restriction', array( $this, 'coupon_options' ), 10, 0 );
|
30 |
+
add_action( 'woocommerce_process_shop_coupon_meta', array( $this, 'process_shop_coupon_meta' ), 10, 2 );
|
31 |
+
|
32 |
+
//Frontend hooks
|
33 |
+
add_action( 'woocommerce_check_cart_items', array( &$this, 'update_matched_autocoupons' ) , 0 ); //Remove coupon before WC does it and shows a message
|
34 |
+
add_filter('woocommerce_cart_totals_coupon_label', array( &$this, 'coupon_label' ), 10, 2 );
|
35 |
+
add_filter('woocommerce_cart_totals_coupon_html', array( &$this, 'coupon_html' ), 10, 2 );
|
36 |
+
// 'woocommerce_before_cart'
|
37 |
+
// 'woocommerce_checkout_init'
|
38 |
+
}
|
39 |
+
|
40 |
+
/* ADMIN HOOKS */
|
41 |
+
|
42 |
+
public function coupon_options() {
|
43 |
+
woocommerce_wp_checkbox( array(
|
44 |
+
'id' => $this->meta_key,
|
45 |
+
'label' => __( 'Auto coupon', 'woocommerce-jos-autocoupon' ),
|
46 |
+
'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 customers cart if the coupon is applied. (JOS - Woocommerce auto added coupons plugin).", 'woocommerce-jos-autocoupon' )
|
47 |
+
) );
|
48 |
+
}
|
49 |
+
|
50 |
+
public function process_shop_coupon_meta( $post_id, $post ) {
|
51 |
+
$autocoupon = isset( $_POST[$this->meta_key] ) ? 'yes' : 'no';
|
52 |
+
update_post_meta( $post_id, $this->meta_key, $autocoupon );
|
53 |
+
}
|
54 |
+
|
55 |
+
/* FRONTEND HOOKS */
|
56 |
+
|
57 |
+
/**
|
58 |
+
* Overwrite the html created by wc_cart_totals_coupon_label() so a descriptive text will be shown for the discount.
|
59 |
+
* @param string $originaltext The default text created by wc_cart_totals_coupon_label()
|
60 |
+
* @param WC_Coupon $coupon The coupon data
|
61 |
+
* @return string The overwritten text
|
62 |
+
*/
|
63 |
+
function coupon_label( $originaltext, $coupon ) {
|
64 |
+
|
65 |
+
if ( $this->is_auto_coupon($coupon) ) {
|
66 |
+
|
67 |
+
return $this->coupon_excerpt($coupon); //__($this->autocoupons[$coupon->code], 'woocommerce-jos-autocoupon');
|
68 |
+
} else {
|
69 |
+
return $originaltext;
|
70 |
+
}
|
71 |
+
}
|
72 |
+
|
73 |
+
/**
|
74 |
+
* Overwrite the html created by wc_cart_totals_coupon_html(). This function is required to remove the "Remove" link.
|
75 |
+
* @param string $originaltext The html created by wc_cart_totals_coupon_html()
|
76 |
+
* @param WC_Coupon $coupon The coupon data
|
77 |
+
* @return string The overwritten html
|
78 |
+
*/
|
79 |
+
function coupon_html( $originaltext, $coupon ) {
|
80 |
+
if ( $this->is_auto_coupon($coupon) ) {
|
81 |
+
if ( ! empty(WC()->cart->coupon_discount_amounts[ $coupon->code ]) ) {
|
82 |
+
$discount_html = '-' . wc_price( WC()->cart->coupon_discount_amounts[ $coupon->code ] );
|
83 |
+
$value[] = apply_filters( 'woocommerce_coupon_discount_amount_html', $discount_html, $coupon );
|
84 |
+
|
85 |
+
if ( $coupon->enable_free_shipping() ) {
|
86 |
+
$value[] = __( 'Free shipping coupon', 'woocommerce' );
|
87 |
+
}
|
88 |
+
|
89 |
+
return implode(', ', array_filter($value)); //Remove empty array elements
|
90 |
+
} else {
|
91 |
+
$discount_html = '';
|
92 |
+
}
|
93 |
+
return $discount_html;
|
94 |
+
} else
|
95 |
+
return $originaltext;
|
96 |
+
}
|
97 |
+
|
98 |
+
/**
|
99 |
+
* Apply matched autocoupons and remove unmatched autocoupons.
|
100 |
+
* @return void
|
101 |
+
*/
|
102 |
+
function update_matched_autocoupons() {
|
103 |
+
global $woocommerce;
|
104 |
+
|
105 |
+
foreach ( $this->get_all_auto_coupons() as $coupon_code ) {
|
106 |
+
if ( ! $woocommerce->cart->has_discount( $coupon_code ) ) {
|
107 |
+
$coupon = new WC_Coupon($coupon_code);
|
108 |
+
if ( $coupon->is_valid() ) {
|
109 |
+
$woocommerce->cart->add_discount( $coupon_code );
|
110 |
+
$this->overwrite_success_message( $coupon );
|
111 |
+
}
|
112 |
+
} else {
|
113 |
+
$this->remove_unmatched_autocoupons();
|
114 |
+
}
|
115 |
+
}
|
116 |
+
}
|
117 |
+
|
118 |
+
/**
|
119 |
+
* Remove unmatched autocoupons. No message will be shown.
|
120 |
+
* NOTE: This function must be called before WooCommerce removes the coupon, to inhibit WooCommerces "coupon not valid"-message!
|
121 |
+
* @return void
|
122 |
+
*/
|
123 |
+
function remove_unmatched_autocoupons() {
|
124 |
+
global $woocommerce;
|
125 |
+
|
126 |
+
foreach ( $this->get_all_auto_coupons() as $coupon_code ) {
|
127 |
+
if ( $woocommerce->cart->has_discount( $coupon_code ) ) {
|
128 |
+
$coupon = new WC_Coupon($coupon_code);
|
129 |
+
if ( ! $coupon->is_valid() ) {
|
130 |
+
WC()->cart->remove_coupon( $coupon_code );
|
131 |
+
}
|
132 |
+
}
|
133 |
+
}
|
134 |
+
}
|
135 |
+
|
136 |
+
/**
|
137 |
+
* Overwrite the default "Coupon added" notice with a more descriptive message.
|
138 |
+
* @param WC_Coupon $coupon The coupon data
|
139 |
+
* @return void
|
140 |
+
*/
|
141 |
+
private function overwrite_success_message( $coupon ) {
|
142 |
+
$succss_msg = $coupon->get_coupon_message( WC_Coupon::WC_COUPON_SUCCESS );
|
143 |
+
|
144 |
+
$new_succss_msg = sprintf(
|
145 |
+
__("Discount applied: %s", 'woocommerce-jos-autocoupon'),
|
146 |
+
__($this->coupon_excerpt($coupon), 'woocommerce-jos-autocoupon')
|
147 |
+
);
|
148 |
+
|
149 |
+
//Compatibility woocommerce-2-1-notice-api
|
150 |
+
if ( function_exists('wc_get_notices') ) {
|
151 |
+
$all_notices = wc_get_notices();
|
152 |
+
$messages = $all_notices['success'];
|
153 |
+
} else {
|
154 |
+
$messages = $woocommerce->messages;
|
155 |
+
}
|
156 |
+
|
157 |
+
$sizeof_messages = sizeof($messages);
|
158 |
+
for( $y=0; $y < $sizeof_messages; $y++ ) {
|
159 |
+
if ( $messages[$y] == $succss_msg ) {
|
160 |
+
if ( isset($all_notices) ) {
|
161 |
+
//unset ( $all_notices['success'][$y] );
|
162 |
+
$all_notices['success'][$y] = $new_succss_msg;
|
163 |
+
WC()->session->set( 'wc_notices', $all_notices );
|
164 |
+
} else {
|
165 |
+
//unset ( $messages[$y] );
|
166 |
+
$messages[$y] = $new_succss_msg;
|
167 |
+
}
|
168 |
+
|
169 |
+
break;
|
170 |
+
}
|
171 |
+
}
|
172 |
+
}
|
173 |
+
|
174 |
+
/**
|
175 |
+
* Check wether the coupon is an "Auto coupon".
|
176 |
+
* @param WC_Coupon $coupon The coupon data
|
177 |
+
* @return bool true if it is an "Auto coupon"
|
178 |
+
*/
|
179 |
+
private function is_auto_coupon($coupon) {
|
180 |
+
return get_post_meta( $coupon->id, $this->meta_key, true ) == 'yes';
|
181 |
+
}
|
182 |
+
|
183 |
+
/**
|
184 |
+
* Get the coupon excerpt (description)
|
185 |
+
* @param WC_Coupon $coupon The coupon data
|
186 |
+
* @return string The excerpt (translated)
|
187 |
+
*/
|
188 |
+
private function coupon_excerpt($coupon) {
|
189 |
+
$my_post = get_post($coupon->id);
|
190 |
+
return __($my_post->post_excerpt, 'woocommerce-jos-autocoupon');
|
191 |
+
}
|
192 |
+
|
193 |
+
/**
|
194 |
+
* Get a list of all auto coupon codes
|
195 |
+
* @return array All auto coupon codes
|
196 |
+
*/
|
197 |
+
private function get_all_auto_coupons() {
|
198 |
+
|
199 |
+
if ( !is_array($this->_autocoupon_codes) ) {
|
200 |
+
$this->_autocoupon_codes = array();
|
201 |
+
|
202 |
+
$query_args = array(
|
203 |
+
'posts_per_page' => -1,
|
204 |
+
'post_type' => 'shop_coupon',
|
205 |
+
'post_status' => 'publish',
|
206 |
+
);
|
207 |
+
|
208 |
+
$query = new WP_Query($query_args);
|
209 |
+
foreach ($query->posts as $post) {
|
210 |
+
$coupon = new WC_Coupon($post->post_title);
|
211 |
+
if ( $this->is_auto_coupon($coupon) ) {
|
212 |
+
$this->_autocoupon_codes[] = $coupon->post_title;
|
213 |
+
}
|
214 |
+
}
|
215 |
+
}
|
216 |
+
return $this->_autocoupon_codes;
|
217 |
+
}
|
218 |
+
|
219 |
+
|
220 |
+
|
221 |
+
}
|
222 |
+
|
223 |
+
/**
|
224 |
+
* Create the plugin if WooCommerce is active
|
225 |
+
**/
|
226 |
+
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
|
227 |
+
$jos_autocoupon = new WC_Jos_AutoCoupon_Controller();
|
228 |
+
}
|
229 |
+
|
230 |
+
/**
|
231 |
+
* Add donate-link to plugin page
|
232 |
+
*/
|
233 |
+
if ( ! function_exists( 'woocommerce_jos_autocoupon_plugin_meta' ) ) {
|
234 |
+
function woocommerce_jos_autocoupon_plugin_meta( $links, $file ) {
|
235 |
+
if ( strpos( $file, 'woocommerce-jos-autocoupon.php' ) !== false ) {
|
236 |
+
$links = array_merge( $links, array( '<a href="https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=5T9XQBCS2QHRY&lc=NL&item_name=Jos%20Koenis&item_number=wordpress%2dplugin¤cy_code=EUR&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHosted" title="Support the development">Donate</a>' ) );
|
237 |
+
}
|
238 |
+
return $links;
|
239 |
+
}
|
240 |
+
add_filter( 'plugin_row_meta', 'woocommerce_jos_autocoupon_plugin_meta', 10, 2 );
|
241 |
+
}
|