2014-10-28 10:51:46 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Class WC_Helper_Product.
|
2014-10-28 10:51:46 +00:00
|
|
|
*
|
2015-11-03 13:31:20 +00:00
|
|
|
* This helper class should ONLY be used for unit tests!.
|
2014-10-28 10:51:46 +00:00
|
|
|
*/
|
|
|
|
class WC_Helper_Coupon {
|
|
|
|
|
2018-06-08 14:39:14 +00:00
|
|
|
protected static $custom_types = array();
|
|
|
|
|
2014-10-28 10:51:46 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Create a dummy coupon.
|
2014-10-28 10:51:46 +00:00
|
|
|
*
|
2018-06-08 14:39:14 +00:00
|
|
|
* @param string $coupon_code
|
|
|
|
* @param array $meta
|
|
|
|
*
|
2014-10-28 10:51:46 +00:00
|
|
|
* @return WC_Coupon
|
|
|
|
*/
|
2018-06-08 14:39:14 +00:00
|
|
|
public static function create_coupon( $coupon_code = 'dummycoupon', $meta = array() ) {
|
2014-10-28 10:51:46 +00:00
|
|
|
// Insert post
|
|
|
|
$coupon_id = wp_insert_post( array(
|
|
|
|
'post_title' => $coupon_code,
|
|
|
|
'post_type' => 'shop_coupon',
|
|
|
|
'post_status' => 'publish',
|
2016-08-27 01:46:45 +00:00
|
|
|
'post_excerpt' => 'This is a dummy coupon',
|
2014-10-28 10:51:46 +00:00
|
|
|
) );
|
|
|
|
|
2018-06-08 14:39:14 +00:00
|
|
|
$meta = wp_parse_args( $meta, array(
|
|
|
|
'discount_type' => 'fixed_cart',
|
|
|
|
'coupon_amount' => '1',
|
|
|
|
'individual_use' => 'no',
|
|
|
|
'product_ids' => '',
|
|
|
|
'exclude_product_ids' => '',
|
|
|
|
'usage_limit' => '',
|
|
|
|
'usage_limit_per_user' => '',
|
|
|
|
'limit_usage_to_x_items' => '',
|
|
|
|
'expiry_date' => '',
|
|
|
|
'free_shipping' => 'no',
|
|
|
|
'exclude_sale_items' => 'no',
|
|
|
|
'product_categories' => array(),
|
|
|
|
'exclude_product_categories' => array(),
|
|
|
|
'minimum_amount' => '',
|
|
|
|
'maximum_amount' => '',
|
|
|
|
'customer_email' => array(),
|
|
|
|
'usage_count' => '0',
|
|
|
|
) );
|
|
|
|
|
|
|
|
// Update meta.
|
|
|
|
foreach ( $meta as $key => $value ) {
|
|
|
|
update_post_meta( $coupon_id, $key, $value );
|
|
|
|
}
|
2014-10-28 10:51:46 +00:00
|
|
|
|
|
|
|
return new WC_Coupon( $coupon_code );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Delete a coupon.
|
2014-10-28 10:51:46 +00:00
|
|
|
*
|
|
|
|
* @param $coupon_id
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function delete_coupon( $coupon_id ) {
|
|
|
|
wp_delete_post( $coupon_id, true );
|
2018-06-08 14:39:14 +00:00
|
|
|
|
2014-10-28 10:51:46 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-06-08 14:39:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a custom coupon type.
|
|
|
|
*
|
|
|
|
* @param string $coupon_type
|
|
|
|
*/
|
|
|
|
public static function register_custom_type( $coupon_type ) {
|
|
|
|
static $filters_added = false;
|
|
|
|
if ( isset( self::$custom_types[ $coupon_type ] ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self::$custom_types[ $coupon_type ] = "Testing custom type {$coupon_type}";
|
|
|
|
|
|
|
|
if ( ! $filters_added ) {
|
|
|
|
add_filter( 'woocommerce_coupon_discount_types', array( __CLASS__, 'filter_discount_types' ) );
|
|
|
|
add_filter( 'woocommerce_coupon_get_discount_amount', array( __CLASS__, 'filter_get_discount_amount' ), 10, 5 );
|
|
|
|
add_filter( 'woocommerce_coupon_is_valid_for_product', '__return_true' );
|
|
|
|
$filters_added = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unregister custom coupon type.
|
|
|
|
*
|
|
|
|
* @param $coupon_type
|
|
|
|
*/
|
|
|
|
public static function unregister_custom_type( $coupon_type ) {
|
|
|
|
unset( self::$custom_types[ $coupon_type ] );
|
|
|
|
if ( empty( self::$custom_types ) ) {
|
|
|
|
remove_filter( 'woocommerce_coupon_discount_types', array( __CLASS__, 'filter_discount_types' ) );
|
|
|
|
remove_filter( 'woocommerce_coupon_get_discount_amount', array( __CLASS__, 'filter_get_discount_amount' ) );
|
|
|
|
remove_filter( 'woocommerce_coupon_is_valid_for_product', '__return_true' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register custom discount types.
|
|
|
|
*
|
|
|
|
* @param array $discount_types
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function filter_discount_types( $discount_types ) {
|
|
|
|
return array_merge( $discount_types, self::$custom_types );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get custom discount type amount. Works like 'percent' type.
|
|
|
|
*
|
|
|
|
* @param float $discount
|
|
|
|
* @param float $discounting_amount
|
|
|
|
* @param array|null $item
|
|
|
|
* @param bool $single
|
|
|
|
* @param WC_Coupon $coupon
|
|
|
|
*
|
|
|
|
* @return float
|
|
|
|
*/
|
|
|
|
public static function filter_get_discount_amount( $discount, $discounting_amount, $item, $single, $coupon ) {
|
|
|
|
if ( ! isset( self::$custom_types [ $coupon->get_discount_type() ] ) ) {
|
|
|
|
return $discount;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (float) $coupon->get_amount() * ( $discounting_amount / 100 );
|
|
|
|
}
|
2014-10-28 10:51:46 +00:00
|
|
|
}
|