Skip to content

Instantly share code, notes, and snippets.

@kLOsk
Created February 15, 2019 00:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kLOsk/2a9355860514b98844a62ffbc4cbda6e to your computer and use it in GitHub Desktop.
Save kLOsk/2a9355860514b98844a62ffbc4cbda6e to your computer and use it in GitHub Desktop.
<?php
/**
Plugin Name: WooCommerce Extended Coupon Features PRO Daily Extension
Description: Extends WooCommerce Extended Coupon Features by adding coupons that are only valid on certain days
Author: Daniel Klose
Version: 1.0
Author URI: http://www.daniel-klose.com/
*/
add_action( 'wjecf_init_plugins', 'setup_my_wjecf_example_plugin' );
function setup_my_wjecf_example_plugin() {
class WJECF_Plugin_Example extends Abstract_WJECF_Plugin {
//This is the meta-key where the weekday will be saved
//HINT: Prepend it with _wjecf_<your plugin name> to avoid naming conflicts
const META_KEY_WEEKDAY = '_wjecf_example_plugin_weekday';
/**
* In the constructor you can supply general information about your WJECF-plugin.
*/
public function __construct() {
$this->set_plugin_data( array(
'description' => __( 'Extends WooCommerce Extended Coupon Features by adding coupons that are only valid on certain days.', 'your-text-domain' ),
'dependencies' => array(),
'minimal_wjecf_version' => '2.5.1',
'can_be_disabled' => true
) );
}
//FRONTEND
/**
* This function is called when WooCommerce and all other plugins are loaded.
* Here you can setup frontend hooks.
*/
public function init_hook() {
add_action( 'woocommerce_coupon_is_valid', array( $this, 'filter_woocommerce_coupon_is_valid' ), 10, 2 );
}
/**
* Invalidates the coupon if the weekday is not ok
* @param bool $valid
* @param WC_Coupon $coupon
* @return bool False if invalid
*/
public function filter_woocommerce_coupon_is_valid( $valid, $coupon ) {
if ( is_callable( array( $coupon, 'get_meta' ) ) ) {
//WC3.0+
$coupon_weekday = $coupon->get_meta( self::META_KEY_WEEKDAY );
} else {
//Older WC versions
$coupon_weekday = get_post_meta( $coupon->id, self::META_KEY_WEEKDAY, true );
}
$day_after = $coupon_weekday +1;
if ($day_after == 7) {
$day_after = 0;
}
//Not valid if the weekday is not same or next day
if ( is_numeric( $coupon_weekday ) && (date('w') != $coupon_weekday) && (date('w') != $day_after) ) {
return false;
}
if ( is_numeric( $coupon_weekday ) && (date('w') == $day_after) && (date('H') > 12) ) {
return false;
}
return $valid;
}
//ADMIN
/**
* This function is called when WooCommerce and all other plugins are loaded and we are on the admin section of WordPress.
* Here you can setup admin hooks.
*/
public function init_admin_hook() {
add_action( 'woocommerce_coupon_options_usage_restriction', array( $this, 'action_woocommerce_coupon_options_usage_restriction' ), 9999 ); //9999 is at the bottom of the tab
}
/**
* Automatically called at the process_shop_coupon_meta action (WJECF 2.5.1+ only).
*
* Returns an array [ meta_key => sanitizion_rule, ... ] with the meta fields that must be saved if the user clicks 'Update' on the coupon admin page. *
* Valid sanitizion rules are: 'html', 'clean', 'yesno', 'decimal', 'int', 'int[]' (array of ints), 'int,' (comma separated ints)
*
* $_POST[meta_key] will be sanitized and automatically saved.
*
* @since 2.5.1
* @param WC_Coupon $coupon The coupon that will be saved
* @return array An array with [ key => sanitizion_rule, ... ]
*/
public function admin_coupon_meta_fields( $coupon ) {
return array( self::META_KEY_WEEKDAY => 'int' );
}
/**
* This is called when the usage restrictions tab is rendered.
*/
public function action_woocommerce_coupon_options_usage_restriction() {
woocommerce_wp_select(
array(
'id' => self::META_KEY_WEEKDAY,
'label' => __( 'Weekday', 'woocommerce-jos-autocoupon' ),
'options' => array(
'' => '(Always valid)',
0 => 'Sunday',
1 => 'Monday',
2 => 'Tuesday',
3 => 'Wednesday',
4 => 'Thursday',
5 => 'Friday',
6 => 'Saturday'
),
'description' => __( 'Coupon is only valid on this weekday.', 'your-text-domain' ),
'desc_tip' => true
)
);
}
}
//Loads the plugin!
WJECF()->add_plugin( 'WJECF_Plugin_Example' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment