woocommerce/includes/class-wc-coupon.php

981 lines
30 KiB
PHP
Raw Normal View History

2011-08-10 17:11:11 +00:00
<?php
2016-02-23 16:01:40 +00:00
include_once( 'legacy/class-wc-legacy-coupon.php' );
2015-11-06 09:22:19 +00:00
if ( ! defined( 'ABSPATH' ) ) {
exit;
2015-11-06 09:22:19 +00:00
}
2011-08-10 17:11:11 +00:00
/**
2016-03-08 19:17:10 +00:00
* WooCommerce coupons.
2012-08-14 19:42:38 +00:00
*
2015-11-03 13:31:20 +00:00
* The WooCommerce coupons class gets coupon data from storage and checks coupon validity.
2011-08-10 17:11:11 +00:00
*
2012-01-27 16:38:39 +00:00
* @class WC_Coupon
2017-03-15 16:36:53 +00:00
* @version 3.0.0
2013-02-20 17:14:46 +00:00
* @package WooCommerce/Classes
2011-08-10 17:11:11 +00:00
* @category Class
* @author WooThemes
*/
class WC_Coupon extends WC_Legacy_Coupon {
2016-02-23 16:01:40 +00:00
/**
* Data array, with defaults.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-02-23 16:01:40 +00:00
* @var array
*/
protected $data = array(
2016-08-26 14:20:44 +00:00
'code' => '',
'amount' => 0,
2017-03-13 20:31:40 +00:00
'date_created' => null,
'date_modified' => null,
'date_expires' => null,
2016-08-26 15:44:00 +00:00
'discount_type' => 'fixed_cart',
'description' => '',
2016-08-26 14:20:44 +00:00
'usage_count' => 0,
'individual_use' => false,
'product_ids' => array(),
'excluded_product_ids' => array(),
'usage_limit' => 0,
'usage_limit_per_user' => 0,
'limit_usage_to_x_items' => null,
2016-08-26 14:20:44 +00:00
'free_shipping' => false,
'product_categories' => array(),
'excluded_product_categories' => array(),
'exclude_sale_items' => false,
'minimum_amount' => '',
'maximum_amount' => '',
'email_restrictions' => array(),
2017-02-17 02:10:46 +00:00
'used_by' => array(),
2016-02-23 16:01:40 +00:00
);
2012-08-14 19:42:38 +00:00
// Coupon message codes
const E_WC_COUPON_INVALID_FILTERED = 100;
const E_WC_COUPON_INVALID_REMOVED = 101;
const E_WC_COUPON_NOT_YOURS_REMOVED = 102;
const E_WC_COUPON_ALREADY_APPLIED = 103;
const E_WC_COUPON_ALREADY_APPLIED_INDIV_USE_ONLY = 104;
const E_WC_COUPON_NOT_EXIST = 105;
const E_WC_COUPON_USAGE_LIMIT_REACHED = 106;
const E_WC_COUPON_EXPIRED = 107;
const E_WC_COUPON_MIN_SPEND_LIMIT_NOT_MET = 108;
const E_WC_COUPON_NOT_APPLICABLE = 109;
const E_WC_COUPON_NOT_VALID_SALE_ITEMS = 110;
const E_WC_COUPON_PLEASE_ENTER = 111;
const E_WC_COUPON_MAX_SPEND_LIMIT_MET = 112;
const E_WC_COUPON_EXCLUDED_PRODUCTS = 113;
const E_WC_COUPON_EXCLUDED_CATEGORIES = 114;
const WC_COUPON_SUCCESS = 200;
const WC_COUPON_REMOVED = 201;
/**
* Cache group.
* @var string
*/
protected $cache_group = 'coupons';
2014-11-14 16:21:58 +00:00
/**
* Coupon constructor. Loads coupon data.
2016-08-26 13:50:17 +00:00
* @param mixed $data Coupon data, object, ID or code.
2014-11-14 16:21:58 +00:00
*/
2016-08-26 13:50:17 +00:00
public function __construct( $data = '' ) {
parent::__construct( $data );
2016-08-26 11:22:05 +00:00
2016-08-26 13:50:17 +00:00
if ( $data instanceof WC_Coupon ) {
$this->set_id( absint( $data->get_id() ) );
2016-08-26 13:50:17 +00:00
} elseif ( $coupon = apply_filters( 'woocommerce_get_shop_coupon_data', false, $data ) ) {
$this->read_manual_coupon( $data, $coupon );
return;
2017-06-27 20:53:56 +00:00
} elseif ( is_int( $data ) && 'shop_coupon' === get_post_type( $data ) ) {
$this->set_id( $data );
2016-08-26 13:50:17 +00:00
} elseif ( ! empty( $data ) ) {
2017-06-27 20:53:56 +00:00
$id = wc_get_coupon_id_by_code( $data );
// Need to support numeric strings for backwards compatibility.
if ( ! $id && 'shop_coupon' === get_post_type( $data ) ) {
$this->set_id( $data );
} else {
$this->set_id( $id );
$this->set_code( $data );
}
} else {
$this->set_object_read( true );
}
$this->data_store = WC_Data_Store::load( 'coupon' );
if ( $this->get_id() > 0 ) {
$this->data_store->read( $this );
2016-02-23 16:01:40 +00:00
}
2014-11-14 16:21:58 +00:00
}
/**
2016-02-23 16:01:40 +00:00
* Checks the coupon type.
* @param string $type Array or string of types
* @return bool
*/
2016-02-23 16:01:40 +00:00
public function is_type( $type ) {
return ( $this->get_discount_type() === $type || ( is_array( $type ) && in_array( $this->get_discount_type(), $type ) ) );
2016-02-23 16:01:40 +00:00
}
/**
* Prefix for action and filter hooks on data.
*
2017-03-15 16:36:53 +00:00
* @since 3.0.0
* @return string
*/
protected function get_hook_prefix() {
2017-01-24 19:02:06 +00:00
return 'woocommerce_coupon_get_';
}
2016-02-23 16:01:40 +00:00
/*
|--------------------------------------------------------------------------
| Getters
|--------------------------------------------------------------------------
|
| Methods for getting data from the coupon object.
|
*/
/**
2016-02-23 16:01:40 +00:00
* Get coupon code.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
* @param string $context
2016-02-23 16:01:40 +00:00
* @return string
*/
public function get_code( $context = 'view' ) {
return $this->get_prop( 'code', $context );
}
/**
2016-02-23 16:01:40 +00:00
* Get coupon description.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
* @param string $context
2016-02-23 16:01:40 +00:00
* @return string
*/
public function get_description( $context = 'view' ) {
return $this->get_prop( 'description', $context );
2016-02-23 16:01:40 +00:00
}
/**
* Get discount type.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
* @param string $context
2016-02-23 16:01:40 +00:00
* @return string
*/
public function get_discount_type( $context = 'view' ) {
return $this->get_prop( 'discount_type', $context );
2016-02-23 16:01:40 +00:00
}
/**
* Get coupon amount.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
* @param string $context
2016-02-23 16:01:40 +00:00
* @return float
*/
public function get_amount( $context = 'view' ) {
return $this->get_prop( 'amount', $context );
2016-02-23 16:01:40 +00:00
}
/**
* Get coupon expiration date.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
* @param string $context
2017-03-13 20:31:40 +00:00
* @return WC_DateTime|NULL object if the date is set or null if there is no date.
2016-08-26 14:20:44 +00:00
*/
public function get_date_expires( $context = 'view' ) {
return $this->get_prop( 'date_expires', $context );
2016-08-26 14:20:44 +00:00
}
/**
* Get date_created
2017-03-15 16:36:53 +00:00
* @since 3.0.0
* @param string $context
2017-03-13 20:31:40 +00:00
* @return WC_DateTime|NULL object if the date is set or null if there is no date.
2016-02-23 16:01:40 +00:00
*/
public function get_date_created( $context = 'view' ) {
return $this->get_prop( 'date_created', $context );
2016-08-26 14:20:44 +00:00
}
/**
* Get date_modified
2017-03-15 16:36:53 +00:00
* @since 3.0.0
* @param string $context
2017-03-13 20:31:40 +00:00
* @return WC_DateTime|NULL object if the date is set or null if there is no date.
2016-02-23 16:01:40 +00:00
*/
public function get_date_modified( $context = 'view' ) {
return $this->get_prop( 'date_modified', $context );
2016-02-23 16:01:40 +00:00
}
/**
* Get coupon usage count.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
* @param string $context
2016-02-23 16:01:40 +00:00
* @return integer
*/
public function get_usage_count( $context = 'view' ) {
return $this->get_prop( 'usage_count', $context );
2016-02-23 16:01:40 +00:00
}
/**
* Get the "indvidual use" checkbox status.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
* @param string $context
* @return bool
*/
public function get_individual_use( $context = 'view' ) {
return $this->get_prop( 'individual_use', $context );
}
2012-08-15 17:08:42 +00:00
/**
2016-02-23 16:01:40 +00:00
* Get product IDs this coupon can apply to.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
* @param string $context
2016-02-23 16:01:40 +00:00
* @return array
*/
public function get_product_ids( $context = 'view' ) {
return $this->get_prop( 'product_ids', $context );
2016-02-23 16:01:40 +00:00
}
/**
* Get product IDs that this coupon should not apply to.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
* @param string $context
2016-02-23 16:01:40 +00:00
* @return array
*/
public function get_excluded_product_ids( $context = 'view' ) {
return $this->get_prop( 'excluded_product_ids', $context );
2016-02-23 16:01:40 +00:00
}
/**
* Get coupon usage limit.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
* @param string $context
2016-02-23 16:01:40 +00:00
* @return integer
*/
public function get_usage_limit( $context = 'view' ) {
return $this->get_prop( 'usage_limit', $context );
2016-02-23 16:01:40 +00:00
}
/**
* Get coupon usage limit per customer (for a single customer)
2017-03-15 16:36:53 +00:00
* @since 3.0.0
* @param string $context
2016-02-23 16:01:40 +00:00
* @return integer
*/
public function get_usage_limit_per_user( $context = 'view' ) {
return $this->get_prop( 'usage_limit_per_user', $context );
2016-02-23 16:01:40 +00:00
}
/**
* Usage limited to certain amount of items
2017-03-15 16:36:53 +00:00
* @since 3.0.0
* @param string $context
* @return integer|null
2016-02-23 16:01:40 +00:00
*/
public function get_limit_usage_to_x_items( $context = 'view' ) {
return $this->get_prop( 'limit_usage_to_x_items', $context );
2016-02-23 16:01:40 +00:00
}
/**
* If this coupon grants free shipping or not.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
* @param string $context
* @return bool
*/
public function get_free_shipping( $context = 'view' ) {
return $this->get_prop( 'free_shipping', $context );
2016-02-23 16:01:40 +00:00
}
2012-08-15 17:08:42 +00:00
2016-02-23 16:01:40 +00:00
/**
* Get product categories this coupon can apply to.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
* @param string $context
2016-02-23 16:01:40 +00:00
* @return array
*/
public function get_product_categories( $context = 'view' ) {
return $this->get_prop( 'product_categories', $context );
2016-02-23 16:01:40 +00:00
}
2015-03-27 15:36:53 +00:00
2016-02-23 16:01:40 +00:00
/**
* Get product categories this coupon cannot not apply to.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
* @param string $context
2016-02-23 16:01:40 +00:00
* @return array
*/
public function get_excluded_product_categories( $context = 'view' ) {
return $this->get_prop( 'excluded_product_categories', $context );
2016-02-23 16:01:40 +00:00
}
2015-03-27 15:36:53 +00:00
2016-02-23 16:01:40 +00:00
/**
* If this coupon should exclude items on sale.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
* @param string $context
2016-02-23 16:01:40 +00:00
* @return bool
*/
public function get_exclude_sale_items( $context = 'view' ) {
return $this->get_prop( 'exclude_sale_items', $context );
2016-02-23 16:01:40 +00:00
}
/**
* Get minimum spend amount.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
* @param string $context
2016-02-23 16:01:40 +00:00
* @return float
*/
public function get_minimum_amount( $context = 'view' ) {
return $this->get_prop( 'minimum_amount', $context );
2016-02-23 16:01:40 +00:00
}
/**
* Get maximum spend amount.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
* @param string $context
2016-02-23 16:01:40 +00:00
* @return float
*/
public function get_maximum_amount( $context = 'view' ) {
return $this->get_prop( 'maximum_amount', $context );
2016-02-23 16:01:40 +00:00
}
/**
* Get emails to check customer usage restrictions.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
* @param string $context
* @return array
2016-02-23 16:01:40 +00:00
*/
public function get_email_restrictions( $context = 'view' ) {
return $this->get_prop( 'email_restrictions', $context );
2016-02-23 16:01:40 +00:00
}
2012-08-15 17:08:42 +00:00
2016-02-23 16:01:40 +00:00
/**
* Get records of all users who have used the current coupon.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
* @param string $context
2016-02-23 16:01:40 +00:00
* @return array
*/
public function get_used_by( $context = 'view' ) {
return $this->get_prop( 'used_by', $context );
}
2012-08-15 17:08:42 +00:00
2016-02-23 16:01:40 +00:00
/**
* Get discount amount for a cart item.
*
* @param float $discounting_amount Amount the coupon is being applied to
* @param array|null $cart_item Cart item being discounted if applicable
* @param boolean $single True if discounting a single qty item, false if its the line
* @return float Amount this coupon has discounted
*/
public function get_discount_amount( $discounting_amount, $cart_item = null, $single = false ) {
$discount = 0;
$cart_item_qty = is_null( $cart_item ) ? 1 : $cart_item['quantity'];
if ( $this->is_type( array( 'percent' ) ) ) {
2017-01-04 18:45:45 +00:00
$discount = (float) $this->get_amount() * ( $discounting_amount / 100 );
2016-02-23 16:01:40 +00:00
} elseif ( $this->is_type( 'fixed_cart' ) && ! is_null( $cart_item ) && WC()->cart->subtotal_ex_tax ) {
/**
* This is the most complex discount - we need to divide the discount between rows based on their price in.
* proportion to the subtotal. This is so rows with different tax rates get a fair discount, and so rows.
* with no price (free) don't get discounted.
*
* Get item discount by dividing item cost by subtotal to get a %.
*
* Uses price inc tax if prices include tax to work around https://github.com/woocommerce/woocommerce/issues/7669 and https://github.com/woocommerce/woocommerce/issues/8074.
2016-02-23 16:01:40 +00:00
*/
if ( wc_prices_include_tax() ) {
WIP - Product CRUD (#12065) * Created function to get the catalog visibility options * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * Hardcode the get_type per product class * Initial look through getters and setters and abstract data * Missing var * Add related product functions and deprecate those in class. * No need to exclude ID * Fixed coding standards and improved the docblocks * Get cached terms from wc_get_related_terms() * Fixed wrong variable in wc_get_related_terms * Use count() instead of sizeof() * Sanitize ids later * Remove unneeded comments * wc_get_product_term_ids instead of related wording and use in other places. get_the_terms is used here and also handles caching, something wp_get_post_terms does not. * Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it. * Bump template version * Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function * Made abstract function useful * External Product CRUD * _virtual meta should be 'no', not taxable, in product unit test helper * Grouped product class * Tests * Move children to meta and update test * Use get_upsell_ids * Spacing in query * Moving and refactoring methods * Availability html * Tidy/add todos * Rename method * Put back review functions (still todo) * missing $this * get_price_including_tax/excluding_tax functions * wc_get_price_to_display * Price handling * [Product CRUD] Variable (#12146) * [Product CRUD] Variable Products * Handle PR feedback. * [Product CRUD] Grouped Handling (#12151) * Handle grouped product saving * Update routine * [Product CRUD] Product crud terms (#12149) * Category and tag id handling * Replace template functions * Remove todo * Handle default name in save function * Product crud admin save routine (#12174) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Spacing * Fix comment * wc_implode_text_attributes helper function * [Product CRUD] Product crud admin use getters (#12196) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Move settings into new files * Refactor panels and use getters * Use getters for variation panel * Revert save variation changes for now * Add todos * Fix downloads * REST API CRUD Updates * Additional API updates/fixes. Added some todos * Fix final failing tests and implementing setters/getters and attributes functionality. * Fix comparison for is_on_sale and remove download_type from WC_Product. * Add a wc_get_products wrapper. * Remove the download type input from the product data metabox for downloadable products. (#12221) * [Product CRUD] Variations - setters, getters and admin. (#12228) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * Feedback fixes * Implement CRUD in the legacy REST API * Handle PR feedback * [Product CRUD] Getter setter proxy methods (#12236) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * get_prop implementation in abstract and data classes * Implement set_prop * Change handling * Array key exists * set_object_read * Use get_the_terms() instead of wp_get_post_terms() wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not use the object cache, and generates a database query every time it is used. get_the_terms() however can use data from the object cache if present. * Allow WP_Query to preload post data, and meta in wc_get_products() Allow WP_Query to bulk query for post data and meta if more than just IDs are requested from wc_get_products(). Reduces query count significantly. * [Product CRUD] Variable, variation, notices, and stock handling (#12277) * No longer needed * Remove old todos * Use getters in admin list * Related and upsells update for CRUD * Fix notice in gallery * Variable fixes and todos * Context * Price sync * Revert variation attributes change * Return parent data in view context * Defer term counting * wc_find_matching_product_variation * Stock manage tweaks * Stock fixes * Correct id * correct id * Better sync * Data logic setter fix * feedback * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * Hardcode the get_type per product class * Initial look through getters and setters and abstract data * Missing var * Fixed coding standards and improved the docblocks * Get cached terms from wc_get_related_terms() * Fixed wrong variable in wc_get_related_terms * Use count() instead of sizeof() * Add related product functions and deprecate those in class. * No need to exclude ID * Sanitize ids later * Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it. * Remove unneeded comments * wc_get_product_term_ids instead of related wording and use in other places. get_the_terms is used here and also handles caching, something wp_get_post_terms does not. * Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function * External Product CRUD * _virtual meta should be 'no', not taxable, in product unit test helper * Bump template version * Made abstract function useful * Grouped product class * Tests * Move children to meta and update test * Use get_upsell_ids * Spacing in query * Moving and refactoring methods * Availability html * Tidy/add todos * Rename method * Put back review functions (still todo) * missing $this * get_price_including_tax/excluding_tax functions * wc_get_price_to_display * Price handling * [Product CRUD] Variable (#12146) * [Product CRUD] Variable Products * Handle PR feedback. * [Product CRUD] Grouped Handling (#12151) * Handle grouped product saving * Update routine * [Product CRUD] Product crud terms (#12149) * Category and tag id handling * Replace template functions * Remove todo * Handle default name in save function * Product crud admin save routine (#12174) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Spacing * Fix comment * wc_implode_text_attributes helper function * [Product CRUD] Product crud admin use getters (#12196) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Move settings into new files * Refactor panels and use getters * Use getters for variation panel * Revert save variation changes for now * Add todos * Fix downloads * REST API CRUD Updates * Additional API updates/fixes. Added some todos * Fix final failing tests and implementing setters/getters and attributes functionality. * Fix comparison for is_on_sale and remove download_type from WC_Product. * Add a wc_get_products wrapper. * Remove the download type input from the product data metabox for downloadable products. (#12221) * [Product CRUD] Variations - setters, getters and admin. (#12228) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * Feedback fixes * Implement CRUD in the legacy REST API * Handle PR feedback * [Product CRUD] Getter setter proxy methods (#12236) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * get_prop implementation in abstract and data classes * Implement set_prop * Change handling * Array key exists * set_object_read * Use get_the_terms() instead of wp_get_post_terms() wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not use the object cache, and generates a database query every time it is used. get_the_terms() however can use data from the object cache if present. * [Product CRUD] Variable, variation, notices, and stock handling (#12277) * No longer needed * Remove old todos * Use getters in admin list * Related and upsells update for CRUD * Fix notice in gallery * Variable fixes and todos * Context * Price sync * Revert variation attributes change * Return parent data in view context * Defer term counting * wc_find_matching_product_variation * Stock manage tweaks * Stock fixes * Correct id * correct id * Better sync * Data logic setter fix * feedback * Prevent notices * Handle image_id from parent * Fix error * Remove _wc_save_product_price * Remove todo * Fixed wrong variation URLs * Fixed undefined $image_id in WC_Product_Variation::get_image_id() * Allow wc_rest_prepare_date_response() handle timestamps * Updated get methods on REST API for variations * Use variations CRUD to save variations metadata * [Product CRUD] Abstract todos (#12305) * Get dimensions and weights, with soft deprecation * Product attributes * Ratings * Fix read method * Downloads * Feedback * Revert "[Product CRUD] Abstract todos (#12305)" This reverts commit 9a6136fcf88fec16f97457b7c8a4388f7587bfa2. * Remove deprecated get_variation_id() * New default attributes method * [Product CRUD] Product Datastore (#12317) * Fix up tests in the product/* folder. * Handle data store updates for grouped, variable, external, simple, and general data store updates for products. * Variations & variable changes. * Update -functions.php calls to use data store. * Add an interface for the public product data store methods. * Finished product factory tests * Correctly delete in the api, fix up some comments, and implement an interface for the public variable methods. * Fix up delete in all versions of the api * Handle feedback * Match protected decloration to parent * Product crud abstract todos (#12316) * Get dimensions and weights, with soft deprecation * Product attributes * Ratings * Fix read method * Downloads * Feedback * Fix up store * Fixed method returning in write context * Fix error in variation admin * Check for parent value - fixes tax class * Remove old/complete todos * Allow set tax class as "parent" * Removed duplicated sync * Fixed wrong variation URLs * Fixed undefined $image_id in WC_Product_Variation::get_image_id() * Allow wc_rest_prepare_date_response() handle timestamps * Updated get methods on REST API for variations * Use variations CRUD to save variations metadata * Remove deprecated get_variation_id() * New default attributes method * Fixed method returning in write context * Allow set tax class as "parent" * Removed duplicated sync * Fixed coding standards * TODO is not accurate. * Should pass WC_Product instancies to WC_Comments methods (#12327) * Use new method in abstract order class to prevent headers sent issue in tests * Fixed variable description in REST API * Updated how create initial product variation * Fixed a few fatal errors and warnings in Products CRUD (#12329) * Fixed a few fatal errors and warnings in Products CRUD * Fixed sync functions * Add variations CRUD to legacy API (#12331) * Apply crud to variable products in legacy API v1 * New REST API do not need fallback for default attributes * Apply variations CRUD to legacy API v2 * Legacy v2 - save default attributes * Variations in legacy API v2 do not have descriptions * Fixed legacy API v2 variations params * Applied variations CRUD to legacy API v3 * Sync before save in legacy apis * Punc * Removed API todos * Removed test * Products endpoint tweaks (#12354) * Var type already normalized on CRUD * Let Product CRUD handle with validation, sanitization and conditional checks * Set downloads using WC_Product_Download * Stop try catch exceptions more than one time * Handle WC_Data_Exception in legacy API * Complete remove products when fails on creating * On creating I mean! * Already have a method to complete delete products * Fixed standards using WP CodeSniffer * get_the_terms() returns false when empty * get_manage_stock returns boolean @claudiosanches * Merge conflict * Variations API endpoint fixes * Product CRUD improvements (#12359) * args is not used any more - remove todo * Added test for attributes * wc_get_price_excluding_tax usage * parent usage * Fix rating counts * Test fixes * Cleanup after tests * Make sure status transition code runs even during API calls, not just in admin. * Default visibility * Fix attribute setting in API * Use get name instead of get title * variation id usage * Improved cross sell templates * variation_data * Grouped product sync * Notices * Sync is not needed in API * Delete * Rename interfaces * Update counts in data store
2016-11-16 12:38:24 +00:00
$discount_percent = ( wc_get_price_including_tax( $cart_item['data'] ) * $cart_item_qty ) / WC()->cart->subtotal;
} else {
WIP - Product CRUD (#12065) * Created function to get the catalog visibility options * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * Hardcode the get_type per product class * Initial look through getters and setters and abstract data * Missing var * Add related product functions and deprecate those in class. * No need to exclude ID * Fixed coding standards and improved the docblocks * Get cached terms from wc_get_related_terms() * Fixed wrong variable in wc_get_related_terms * Use count() instead of sizeof() * Sanitize ids later * Remove unneeded comments * wc_get_product_term_ids instead of related wording and use in other places. get_the_terms is used here and also handles caching, something wp_get_post_terms does not. * Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it. * Bump template version * Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function * Made abstract function useful * External Product CRUD * _virtual meta should be 'no', not taxable, in product unit test helper * Grouped product class * Tests * Move children to meta and update test * Use get_upsell_ids * Spacing in query * Moving and refactoring methods * Availability html * Tidy/add todos * Rename method * Put back review functions (still todo) * missing $this * get_price_including_tax/excluding_tax functions * wc_get_price_to_display * Price handling * [Product CRUD] Variable (#12146) * [Product CRUD] Variable Products * Handle PR feedback. * [Product CRUD] Grouped Handling (#12151) * Handle grouped product saving * Update routine * [Product CRUD] Product crud terms (#12149) * Category and tag id handling * Replace template functions * Remove todo * Handle default name in save function * Product crud admin save routine (#12174) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Spacing * Fix comment * wc_implode_text_attributes helper function * [Product CRUD] Product crud admin use getters (#12196) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Move settings into new files * Refactor panels and use getters * Use getters for variation panel * Revert save variation changes for now * Add todos * Fix downloads * REST API CRUD Updates * Additional API updates/fixes. Added some todos * Fix final failing tests and implementing setters/getters and attributes functionality. * Fix comparison for is_on_sale and remove download_type from WC_Product. * Add a wc_get_products wrapper. * Remove the download type input from the product data metabox for downloadable products. (#12221) * [Product CRUD] Variations - setters, getters and admin. (#12228) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * Feedback fixes * Implement CRUD in the legacy REST API * Handle PR feedback * [Product CRUD] Getter setter proxy methods (#12236) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * get_prop implementation in abstract and data classes * Implement set_prop * Change handling * Array key exists * set_object_read * Use get_the_terms() instead of wp_get_post_terms() wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not use the object cache, and generates a database query every time it is used. get_the_terms() however can use data from the object cache if present. * Allow WP_Query to preload post data, and meta in wc_get_products() Allow WP_Query to bulk query for post data and meta if more than just IDs are requested from wc_get_products(). Reduces query count significantly. * [Product CRUD] Variable, variation, notices, and stock handling (#12277) * No longer needed * Remove old todos * Use getters in admin list * Related and upsells update for CRUD * Fix notice in gallery * Variable fixes and todos * Context * Price sync * Revert variation attributes change * Return parent data in view context * Defer term counting * wc_find_matching_product_variation * Stock manage tweaks * Stock fixes * Correct id * correct id * Better sync * Data logic setter fix * feedback * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * Hardcode the get_type per product class * Initial look through getters and setters and abstract data * Missing var * Fixed coding standards and improved the docblocks * Get cached terms from wc_get_related_terms() * Fixed wrong variable in wc_get_related_terms * Use count() instead of sizeof() * Add related product functions and deprecate those in class. * No need to exclude ID * Sanitize ids later * Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it. * Remove unneeded comments * wc_get_product_term_ids instead of related wording and use in other places. get_the_terms is used here and also handles caching, something wp_get_post_terms does not. * Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function * External Product CRUD * _virtual meta should be 'no', not taxable, in product unit test helper * Bump template version * Made abstract function useful * Grouped product class * Tests * Move children to meta and update test * Use get_upsell_ids * Spacing in query * Moving and refactoring methods * Availability html * Tidy/add todos * Rename method * Put back review functions (still todo) * missing $this * get_price_including_tax/excluding_tax functions * wc_get_price_to_display * Price handling * [Product CRUD] Variable (#12146) * [Product CRUD] Variable Products * Handle PR feedback. * [Product CRUD] Grouped Handling (#12151) * Handle grouped product saving * Update routine * [Product CRUD] Product crud terms (#12149) * Category and tag id handling * Replace template functions * Remove todo * Handle default name in save function * Product crud admin save routine (#12174) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Spacing * Fix comment * wc_implode_text_attributes helper function * [Product CRUD] Product crud admin use getters (#12196) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Move settings into new files * Refactor panels and use getters * Use getters for variation panel * Revert save variation changes for now * Add todos * Fix downloads * REST API CRUD Updates * Additional API updates/fixes. Added some todos * Fix final failing tests and implementing setters/getters and attributes functionality. * Fix comparison for is_on_sale and remove download_type from WC_Product. * Add a wc_get_products wrapper. * Remove the download type input from the product data metabox for downloadable products. (#12221) * [Product CRUD] Variations - setters, getters and admin. (#12228) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * Feedback fixes * Implement CRUD in the legacy REST API * Handle PR feedback * [Product CRUD] Getter setter proxy methods (#12236) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * get_prop implementation in abstract and data classes * Implement set_prop * Change handling * Array key exists * set_object_read * Use get_the_terms() instead of wp_get_post_terms() wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not use the object cache, and generates a database query every time it is used. get_the_terms() however can use data from the object cache if present. * [Product CRUD] Variable, variation, notices, and stock handling (#12277) * No longer needed * Remove old todos * Use getters in admin list * Related and upsells update for CRUD * Fix notice in gallery * Variable fixes and todos * Context * Price sync * Revert variation attributes change * Return parent data in view context * Defer term counting * wc_find_matching_product_variation * Stock manage tweaks * Stock fixes * Correct id * correct id * Better sync * Data logic setter fix * feedback * Prevent notices * Handle image_id from parent * Fix error * Remove _wc_save_product_price * Remove todo * Fixed wrong variation URLs * Fixed undefined $image_id in WC_Product_Variation::get_image_id() * Allow wc_rest_prepare_date_response() handle timestamps * Updated get methods on REST API for variations * Use variations CRUD to save variations metadata * [Product CRUD] Abstract todos (#12305) * Get dimensions and weights, with soft deprecation * Product attributes * Ratings * Fix read method * Downloads * Feedback * Revert "[Product CRUD] Abstract todos (#12305)" This reverts commit 9a6136fcf88fec16f97457b7c8a4388f7587bfa2. * Remove deprecated get_variation_id() * New default attributes method * [Product CRUD] Product Datastore (#12317) * Fix up tests in the product/* folder. * Handle data store updates for grouped, variable, external, simple, and general data store updates for products. * Variations & variable changes. * Update -functions.php calls to use data store. * Add an interface for the public product data store methods. * Finished product factory tests * Correctly delete in the api, fix up some comments, and implement an interface for the public variable methods. * Fix up delete in all versions of the api * Handle feedback * Match protected decloration to parent * Product crud abstract todos (#12316) * Get dimensions and weights, with soft deprecation * Product attributes * Ratings * Fix read method * Downloads * Feedback * Fix up store * Fixed method returning in write context * Fix error in variation admin * Check for parent value - fixes tax class * Remove old/complete todos * Allow set tax class as "parent" * Removed duplicated sync * Fixed wrong variation URLs * Fixed undefined $image_id in WC_Product_Variation::get_image_id() * Allow wc_rest_prepare_date_response() handle timestamps * Updated get methods on REST API for variations * Use variations CRUD to save variations metadata * Remove deprecated get_variation_id() * New default attributes method * Fixed method returning in write context * Allow set tax class as "parent" * Removed duplicated sync * Fixed coding standards * TODO is not accurate. * Should pass WC_Product instancies to WC_Comments methods (#12327) * Use new method in abstract order class to prevent headers sent issue in tests * Fixed variable description in REST API * Updated how create initial product variation * Fixed a few fatal errors and warnings in Products CRUD (#12329) * Fixed a few fatal errors and warnings in Products CRUD * Fixed sync functions * Add variations CRUD to legacy API (#12331) * Apply crud to variable products in legacy API v1 * New REST API do not need fallback for default attributes * Apply variations CRUD to legacy API v2 * Legacy v2 - save default attributes * Variations in legacy API v2 do not have descriptions * Fixed legacy API v2 variations params * Applied variations CRUD to legacy API v3 * Sync before save in legacy apis * Punc * Removed API todos * Removed test * Products endpoint tweaks (#12354) * Var type already normalized on CRUD * Let Product CRUD handle with validation, sanitization and conditional checks * Set downloads using WC_Product_Download * Stop try catch exceptions more than one time * Handle WC_Data_Exception in legacy API * Complete remove products when fails on creating * On creating I mean! * Already have a method to complete delete products * Fixed standards using WP CodeSniffer * get_the_terms() returns false when empty * get_manage_stock returns boolean @claudiosanches * Merge conflict * Variations API endpoint fixes * Product CRUD improvements (#12359) * args is not used any more - remove todo * Added test for attributes * wc_get_price_excluding_tax usage * parent usage * Fix rating counts * Test fixes * Cleanup after tests * Make sure status transition code runs even during API calls, not just in admin. * Default visibility * Fix attribute setting in API * Use get name instead of get title * variation id usage * Improved cross sell templates * variation_data * Grouped product sync * Notices * Sync is not needed in API * Delete * Rename interfaces * Update counts in data store
2016-11-16 12:38:24 +00:00
$discount_percent = ( wc_get_price_excluding_tax( $cart_item['data'] ) * $cart_item_qty ) / WC()->cart->subtotal_ex_tax;
2016-02-23 16:01:40 +00:00
}
2017-01-04 18:45:45 +00:00
$discount = ( (float) $this->get_amount() * $discount_percent ) / $cart_item_qty;
2016-02-23 16:01:40 +00:00
} elseif ( $this->is_type( 'fixed_product' ) ) {
$discount = min( $this->get_amount(), $discounting_amount );
$discount = $single ? $discount : $discount * $cart_item_qty;
}
2017-07-27 14:31:10 +00:00
return apply_filters( 'woocommerce_coupon_get_discount_amount', round( min( $discount, $discounting_amount ), wc_get_rounding_precision() ), $discounting_amount, $cart_item, $single, $this );
2016-02-23 16:01:40 +00:00
}
/*
|--------------------------------------------------------------------------
| Setters
|--------------------------------------------------------------------------
|
| Functions for setting coupon data. These should not update anything in the
| database itself and should only change what is stored in the class
| object.
|
*/
2016-02-23 16:01:40 +00:00
/**
* Set coupon code.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-02-23 16:01:40 +00:00
* @param string $code
2016-08-26 11:33:33 +00:00
* @throws WC_Data_Exception
2016-02-23 16:01:40 +00:00
*/
public function set_code( $code ) {
$this->set_prop( 'code', wc_format_coupon_code( $code ) );
2016-02-23 16:01:40 +00:00
}
/**
* Set coupon description.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-02-23 16:01:40 +00:00
* @param string $description
2016-08-26 11:33:33 +00:00
* @throws WC_Data_Exception
2016-02-23 16:01:40 +00:00
*/
public function set_description( $description ) {
$this->set_prop( 'description', $description );
2016-02-23 16:01:40 +00:00
}
/**
* Set discount type.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-02-23 16:01:40 +00:00
* @param string $discount_type
2016-08-26 11:33:33 +00:00
* @throws WC_Data_Exception
2016-02-23 16:01:40 +00:00
*/
public function set_discount_type( $discount_type ) {
if ( 'percent_product' === $discount_type ) {
$discount_type = 'percent'; // Backwards compatibility.
}
2016-08-26 13:50:17 +00:00
if ( ! in_array( $discount_type, array_keys( wc_get_coupon_types() ) ) ) {
2016-08-26 11:33:33 +00:00
$this->error( 'coupon_invalid_discount_type', __( 'Invalid discount type', 'woocommerce' ) );
}
$this->set_prop( 'discount_type', $discount_type );
2016-02-23 16:01:40 +00:00
}
/**
* Set amount.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-02-23 16:01:40 +00:00
* @param float $amount
2016-08-26 11:33:33 +00:00
* @throws WC_Data_Exception
2016-02-23 16:01:40 +00:00
*/
public function set_amount( $amount ) {
2017-08-10 18:00:57 +00:00
if ( $amount < 0 ) {
$this->error( 'coupon_invalid_amount', __( 'Invalid discount amount', 'woocommerce' ) );
}
if ( 'percent' === $this->get_discount_type() && $amount > 100 ) {
$this->error( 'coupon_invalid_amount', __( 'Invalid discount amount', 'woocommerce' ) );
}
$this->set_prop( 'amount', wc_format_decimal( $amount ) );
2016-02-23 16:01:40 +00:00
}
/**
* Set expiration date.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2017-03-13 20:31:40 +00:00
* @param string|integer|null $date UTC timestamp, or ISO 8601 DateTime. If the DateTime string has no timezone or offset, WordPress site timezone will be assumed. Null if there is no date.
2016-08-26 11:33:33 +00:00
* @throws WC_Data_Exception
2016-02-23 16:01:40 +00:00
*/
2017-03-13 20:31:40 +00:00
public function set_date_expires( $date ) {
$this->set_date_prop( 'date_expires', $date );
2016-08-26 14:20:44 +00:00
}
/**
* Set date_created
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2017-03-13 20:31:40 +00:00
* @param string|integer|null $date UTC timestamp, or ISO 8601 DateTime. If the DateTime string has no timezone or offset, WordPress site timezone will be assumed. Null if there is no date.
2016-08-26 14:20:44 +00:00
* @throws WC_Data_Exception
*/
2017-03-13 20:31:40 +00:00
public function set_date_created( $date ) {
$this->set_date_prop( 'date_created', $date );
2016-08-26 14:20:44 +00:00
}
/**
* Set date_modified
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2017-03-13 20:31:40 +00:00
* @param string|integer|null $date UTC timestamp, or ISO 8601 DateTime. If the DateTime string has no timezone or offset, WordPress site timezone will be assumed. Null if there is no date.
2016-08-26 14:20:44 +00:00
* @throws WC_Data_Exception
*/
2017-03-13 20:31:40 +00:00
public function set_date_modified( $date ) {
$this->set_date_prop( 'date_modified', $date );
2016-02-23 16:01:40 +00:00
}
/**
* Set how many times this coupon has been used.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-02-23 16:01:40 +00:00
* @param int $usage_count
2016-08-26 11:33:33 +00:00
* @throws WC_Data_Exception
2016-02-23 16:01:40 +00:00
*/
public function set_usage_count( $usage_count ) {
$this->set_prop( 'usage_count', absint( $usage_count ) );
2016-02-23 16:01:40 +00:00
}
/**
* Set if this coupon can only be used once.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-02-23 16:01:40 +00:00
* @param bool $is_individual_use
2016-08-26 11:33:33 +00:00
* @throws WC_Data_Exception
2016-02-23 16:01:40 +00:00
*/
public function set_individual_use( $is_individual_use ) {
$this->set_prop( 'individual_use', (bool) $is_individual_use );
2016-02-23 16:01:40 +00:00
}
/**
* Set the product IDs this coupon can be used with.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-02-23 16:01:40 +00:00
* @param array $product_ids
2016-08-26 11:33:33 +00:00
* @throws WC_Data_Exception
2016-02-23 16:01:40 +00:00
*/
public function set_product_ids( $product_ids ) {
$this->set_prop( 'product_ids', array_filter( wp_parse_id_list( (array) $product_ids ) ) );
2016-02-23 16:01:40 +00:00
}
/**
* Set the product IDs this coupon cannot be used with.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-02-23 16:01:40 +00:00
* @param array $excluded_product_ids
2016-08-26 11:33:33 +00:00
* @throws WC_Data_Exception
2016-02-23 16:01:40 +00:00
*/
public function set_excluded_product_ids( $excluded_product_ids ) {
$this->set_prop( 'excluded_product_ids', array_filter( wp_parse_id_list( (array) $excluded_product_ids ) ) );
2016-02-23 16:01:40 +00:00
}
/**
* Set the amount of times this coupon can be used.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-02-23 16:01:40 +00:00
* @param int $usage_limit
2016-08-26 11:33:33 +00:00
* @throws WC_Data_Exception
2016-02-23 16:01:40 +00:00
*/
public function set_usage_limit( $usage_limit ) {
$this->set_prop( 'usage_limit', absint( $usage_limit ) );
2016-02-23 16:01:40 +00:00
}
/**
* Set the amount of times this coupon can be used per user.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-02-23 16:01:40 +00:00
* @param int $usage_limit
2016-08-26 11:33:33 +00:00
* @throws WC_Data_Exception
2016-02-23 16:01:40 +00:00
*/
public function set_usage_limit_per_user( $usage_limit ) {
$this->set_prop( 'usage_limit_per_user', absint( $usage_limit ) );
2016-02-23 16:01:40 +00:00
}
/**
* Set usage limit to x number of items.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
* @param int|null $limit_usage_to_x_items
2016-08-26 11:33:33 +00:00
* @throws WC_Data_Exception
2016-02-23 16:01:40 +00:00
*/
public function set_limit_usage_to_x_items( $limit_usage_to_x_items ) {
$this->set_prop( 'limit_usage_to_x_items', is_null( $limit_usage_to_x_items ) ? null : absint( $limit_usage_to_x_items ) );
2016-02-23 16:01:40 +00:00
}
/**
* Set if this coupon enables free shipping or not.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-02-23 16:01:40 +00:00
* @param bool $free_shipping
2016-08-26 11:33:33 +00:00
* @throws WC_Data_Exception
2016-02-23 16:01:40 +00:00
*/
public function set_free_shipping( $free_shipping ) {
$this->set_prop( 'free_shipping', (bool) $free_shipping );
2016-02-23 16:01:40 +00:00
}
/**
* Set the product category IDs this coupon can be used with.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-02-23 16:01:40 +00:00
* @param array $product_categories
2016-08-26 11:33:33 +00:00
* @throws WC_Data_Exception
2016-02-23 16:01:40 +00:00
*/
public function set_product_categories( $product_categories ) {
$this->set_prop( 'product_categories', array_filter( wp_parse_id_list( (array) $product_categories ) ) );
2016-02-23 16:01:40 +00:00
}
/**
* Set the product category IDs this coupon cannot be used with.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-02-23 16:01:40 +00:00
* @param array $excluded_product_categories
2016-08-26 11:33:33 +00:00
* @throws WC_Data_Exception
2016-02-23 16:01:40 +00:00
*/
public function set_excluded_product_categories( $excluded_product_categories ) {
$this->set_prop( 'excluded_product_categories', array_filter( wp_parse_id_list( (array) $excluded_product_categories ) ) );
2016-02-23 16:01:40 +00:00
}
/**
* Set if this coupon should excluded sale items or not.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-02-23 16:01:40 +00:00
* @param bool $exclude_sale_items
2016-08-26 11:33:33 +00:00
* @throws WC_Data_Exception
2016-02-23 16:01:40 +00:00
*/
public function set_exclude_sale_items( $exclude_sale_items ) {
$this->set_prop( 'exclude_sale_items', (bool) $exclude_sale_items );
2016-02-23 16:01:40 +00:00
}
/**
* Set the minimum spend amount.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-02-23 16:01:40 +00:00
* @param float $amount
2016-08-26 11:33:33 +00:00
* @throws WC_Data_Exception
2016-02-23 16:01:40 +00:00
*/
public function set_minimum_amount( $amount ) {
$this->set_prop( 'minimum_amount', wc_format_decimal( $amount ) );
2016-02-23 16:01:40 +00:00
}
/**
* Set the maximum spend amount.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-02-23 16:01:40 +00:00
* @param float $amount
2016-08-26 11:33:33 +00:00
* @throws WC_Data_Exception
2016-02-23 16:01:40 +00:00
*/
public function set_maximum_amount( $amount ) {
$this->set_prop( 'maximum_amount', wc_format_decimal( $amount ) );
2016-02-23 16:01:40 +00:00
}
/**
* Set email restrictions.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-02-23 16:01:40 +00:00
* @param array $emails
2016-08-26 11:33:33 +00:00
* @throws WC_Data_Exception
2016-02-23 16:01:40 +00:00
*/
public function set_email_restrictions( $emails = array() ) {
2017-06-19 09:48:00 +00:00
$emails = array_filter( array_map( 'sanitize_email', array_map( 'strtolower', (array) $emails ) ) );
2016-08-26 11:33:33 +00:00
foreach ( $emails as $email ) {
if ( ! is_email( $email ) ) {
$this->error( 'coupon_invalid_email_address', __( 'Invalid email address restriction', 'woocommerce' ) );
}
}
$this->set_prop( 'email_restrictions', $emails );
2016-02-23 16:01:40 +00:00
}
/**
* Set which users have used this coupon.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-02-23 16:01:40 +00:00
* @param array $used_by
2016-08-26 11:33:33 +00:00
* @throws WC_Data_Exception
2016-02-23 16:01:40 +00:00
*/
public function set_used_by( $used_by ) {
$this->set_prop( 'used_by', array_filter( $used_by ) );
2016-02-23 16:01:40 +00:00
}
/*
|--------------------------------------------------------------------------
| Other Actions
2016-02-23 16:01:40 +00:00
|--------------------------------------------------------------------------
*/
/**
* Developers can programmatically return coupons. This function will read those values into our WC_Coupon class.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
* @param string $code Coupon code
* @param array $coupon Array of coupon properties
*/
public function read_manual_coupon( $code, $coupon ) {
2016-08-30 17:58:28 +00:00
foreach ( $coupon as $key => $value ) {
switch ( $key ) {
case 'excluded_product_ids' :
case 'exclude_product_ids' :
if ( ! is_array( $coupon[ $key ] ) ) {
2017-03-15 16:36:53 +00:00
wc_doing_it_wrong( $key, $key . ' should be an array instead of a string.', '3.0' );
2016-08-30 17:58:28 +00:00
$coupon['excluded_product_ids'] = wc_string_to_array( $value );
}
break;
case 'exclude_product_categories' :
case 'excluded_product_categories' :
if ( ! is_array( $coupon[ $key ] ) ) {
2017-03-15 16:36:53 +00:00
wc_doing_it_wrong( $key, $key . ' should be an array instead of a string.', '3.0' );
2016-08-30 17:58:28 +00:00
$coupon['excluded_product_categories'] = wc_string_to_array( $value );
}
break;
case 'product_ids' :
if ( ! is_array( $coupon[ $key ] ) ) {
2017-03-15 16:36:53 +00:00
wc_doing_it_wrong( $key, $key . ' should be an array instead of a string.', '3.0' );
2016-08-30 17:58:28 +00:00
$coupon[ $key ] = wc_string_to_array( $value );
}
break;
case 'individual_use' :
case 'free_shipping' :
case 'exclude_sale_items' :
if ( ! is_bool( $coupon[ $key ] ) ) {
2017-03-15 16:36:53 +00:00
wc_doing_it_wrong( $key, $key . ' should be true or false instead of yes or no.', '3.0' );
2016-08-30 17:58:28 +00:00
$coupon[ $key ] = wc_string_to_bool( $value );
}
break;
case 'expiry_date' :
$coupon['date_expires'] = $value;
break;
}
}
$this->set_code( $code );
2016-08-26 12:13:50 +00:00
$this->set_props( $coupon );
}
2012-08-14 19:42:38 +00:00
/**
2014-11-11 20:52:45 +00:00
* Increase usage count for current coupon.
2012-08-14 19:42:38 +00:00
*
2015-07-16 19:55:48 +00:00
* @param string $used_by Either user ID or billing email
2012-08-14 19:42:38 +00:00
*/
public function increase_usage_count( $used_by = '' ) {
if ( $this->get_id() && $this->data_store ) {
$new_count = $this->data_store->increase_usage_count( $this, $used_by );
2017-03-10 18:32:27 +00:00
// Bypass set_prop and remove pending changes since the data store saves the count already.
$this->data['usage_count'] = $new_count;
if ( isset( $this->changes['usage_count'] ) ) {
unset( $this->changes['usage_count'] );
}
}
}
2012-08-14 19:42:38 +00:00
/**
2014-11-11 20:52:45 +00:00
* Decrease usage count for current coupon.
2012-08-14 19:42:38 +00:00
*
2015-07-16 19:55:48 +00:00
* @param string $used_by Either user ID or billing email
2012-08-14 19:42:38 +00:00
*/
public function decrease_usage_count( $used_by = '' ) {
if ( $this->get_id() && $this->get_usage_count() > 0 && $this->data_store ) {
$new_count = $this->data_store->decrease_usage_count( $this, $used_by );
2017-03-10 18:32:27 +00:00
// Bypass set_prop and remove pending changes since the data store saves the count already.
$this->data['usage_count'] = $new_count;
if ( isset( $this->changes['usage_count'] ) ) {
unset( $this->changes['usage_count'] );
}
}
2011-08-15 16:48:24 +00:00
}
2012-08-14 19:42:38 +00:00
2016-02-23 16:01:40 +00:00
/*
|--------------------------------------------------------------------------
| Validation & Error Handling
|--------------------------------------------------------------------------
*/
/**
2015-11-03 13:31:20 +00:00
* Returns the error_message string.
*
* @access public
* @return string
*/
public function get_error_message() {
return $this->error_message;
}
2012-06-10 18:07:19 +00:00
/**
* Check if a coupon is valid for the cart.
2014-11-14 17:18:02 +00:00
*
* @deprecated 3.2.0 In favor of WC_Discounts->is_coupon_valid.
* @throws Exception
* @return bool Validity.
2014-11-14 17:18:02 +00:00
*/
public function is_valid() {
$discounts = new WC_Discounts( WC()->cart );
$valid = $discounts->is_coupon_valid( $this );
if ( is_wp_error( $valid ) ) {
$this->error_message = $valid->get_error_message();
2014-11-14 17:18:02 +00:00
return false;
}
2017-07-28 12:02:39 +00:00
return $valid;
2011-08-10 17:11:11 +00:00
}
2013-02-15 03:56:35 +00:00
2013-11-25 15:00:54 +00:00
/**
2015-11-03 13:31:20 +00:00
* Check if a coupon is valid.
2013-11-25 15:00:54 +00:00
*
* @return bool
*/
public function is_valid_for_cart() {
return apply_filters( 'woocommerce_coupon_is_valid_for_cart', $this->is_type( wc_get_cart_coupon_types() ), $this );
}
/**
2015-11-03 13:31:20 +00:00
* Check if a coupon is valid for a product.
*
* @param WC_Product $product
* @param array $values
*
* @return bool
*/
2014-10-13 08:32:40 +00:00
public function is_valid_for_product( $product, $values = array() ) {
if ( ! $this->is_type( wc_get_product_coupon_types() ) ) {
return apply_filters( 'woocommerce_coupon_is_valid_for_product', false, $product, $this, $values );
}
$valid = false;
$product_cats = wc_get_product_cat_ids( $product->is_type( 'variation' ) ? $product->get_parent_id() : $product->get_id() );
WIP - Product CRUD (#12065) * Created function to get the catalog visibility options * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * Hardcode the get_type per product class * Initial look through getters and setters and abstract data * Missing var * Add related product functions and deprecate those in class. * No need to exclude ID * Fixed coding standards and improved the docblocks * Get cached terms from wc_get_related_terms() * Fixed wrong variable in wc_get_related_terms * Use count() instead of sizeof() * Sanitize ids later * Remove unneeded comments * wc_get_product_term_ids instead of related wording and use in other places. get_the_terms is used here and also handles caching, something wp_get_post_terms does not. * Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it. * Bump template version * Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function * Made abstract function useful * External Product CRUD * _virtual meta should be 'no', not taxable, in product unit test helper * Grouped product class * Tests * Move children to meta and update test * Use get_upsell_ids * Spacing in query * Moving and refactoring methods * Availability html * Tidy/add todos * Rename method * Put back review functions (still todo) * missing $this * get_price_including_tax/excluding_tax functions * wc_get_price_to_display * Price handling * [Product CRUD] Variable (#12146) * [Product CRUD] Variable Products * Handle PR feedback. * [Product CRUD] Grouped Handling (#12151) * Handle grouped product saving * Update routine * [Product CRUD] Product crud terms (#12149) * Category and tag id handling * Replace template functions * Remove todo * Handle default name in save function * Product crud admin save routine (#12174) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Spacing * Fix comment * wc_implode_text_attributes helper function * [Product CRUD] Product crud admin use getters (#12196) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Move settings into new files * Refactor panels and use getters * Use getters for variation panel * Revert save variation changes for now * Add todos * Fix downloads * REST API CRUD Updates * Additional API updates/fixes. Added some todos * Fix final failing tests and implementing setters/getters and attributes functionality. * Fix comparison for is_on_sale and remove download_type from WC_Product. * Add a wc_get_products wrapper. * Remove the download type input from the product data metabox for downloadable products. (#12221) * [Product CRUD] Variations - setters, getters and admin. (#12228) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * Feedback fixes * Implement CRUD in the legacy REST API * Handle PR feedback * [Product CRUD] Getter setter proxy methods (#12236) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * get_prop implementation in abstract and data classes * Implement set_prop * Change handling * Array key exists * set_object_read * Use get_the_terms() instead of wp_get_post_terms() wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not use the object cache, and generates a database query every time it is used. get_the_terms() however can use data from the object cache if present. * Allow WP_Query to preload post data, and meta in wc_get_products() Allow WP_Query to bulk query for post data and meta if more than just IDs are requested from wc_get_products(). Reduces query count significantly. * [Product CRUD] Variable, variation, notices, and stock handling (#12277) * No longer needed * Remove old todos * Use getters in admin list * Related and upsells update for CRUD * Fix notice in gallery * Variable fixes and todos * Context * Price sync * Revert variation attributes change * Return parent data in view context * Defer term counting * wc_find_matching_product_variation * Stock manage tweaks * Stock fixes * Correct id * correct id * Better sync * Data logic setter fix * feedback * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * Hardcode the get_type per product class * Initial look through getters and setters and abstract data * Missing var * Fixed coding standards and improved the docblocks * Get cached terms from wc_get_related_terms() * Fixed wrong variable in wc_get_related_terms * Use count() instead of sizeof() * Add related product functions and deprecate those in class. * No need to exclude ID * Sanitize ids later * Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it. * Remove unneeded comments * wc_get_product_term_ids instead of related wording and use in other places. get_the_terms is used here and also handles caching, something wp_get_post_terms does not. * Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function * External Product CRUD * _virtual meta should be 'no', not taxable, in product unit test helper * Bump template version * Made abstract function useful * Grouped product class * Tests * Move children to meta and update test * Use get_upsell_ids * Spacing in query * Moving and refactoring methods * Availability html * Tidy/add todos * Rename method * Put back review functions (still todo) * missing $this * get_price_including_tax/excluding_tax functions * wc_get_price_to_display * Price handling * [Product CRUD] Variable (#12146) * [Product CRUD] Variable Products * Handle PR feedback. * [Product CRUD] Grouped Handling (#12151) * Handle grouped product saving * Update routine * [Product CRUD] Product crud terms (#12149) * Category and tag id handling * Replace template functions * Remove todo * Handle default name in save function * Product crud admin save routine (#12174) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Spacing * Fix comment * wc_implode_text_attributes helper function * [Product CRUD] Product crud admin use getters (#12196) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Move settings into new files * Refactor panels and use getters * Use getters for variation panel * Revert save variation changes for now * Add todos * Fix downloads * REST API CRUD Updates * Additional API updates/fixes. Added some todos * Fix final failing tests and implementing setters/getters and attributes functionality. * Fix comparison for is_on_sale and remove download_type from WC_Product. * Add a wc_get_products wrapper. * Remove the download type input from the product data metabox for downloadable products. (#12221) * [Product CRUD] Variations - setters, getters and admin. (#12228) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * Feedback fixes * Implement CRUD in the legacy REST API * Handle PR feedback * [Product CRUD] Getter setter proxy methods (#12236) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * get_prop implementation in abstract and data classes * Implement set_prop * Change handling * Array key exists * set_object_read * Use get_the_terms() instead of wp_get_post_terms() wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not use the object cache, and generates a database query every time it is used. get_the_terms() however can use data from the object cache if present. * [Product CRUD] Variable, variation, notices, and stock handling (#12277) * No longer needed * Remove old todos * Use getters in admin list * Related and upsells update for CRUD * Fix notice in gallery * Variable fixes and todos * Context * Price sync * Revert variation attributes change * Return parent data in view context * Defer term counting * wc_find_matching_product_variation * Stock manage tweaks * Stock fixes * Correct id * correct id * Better sync * Data logic setter fix * feedback * Prevent notices * Handle image_id from parent * Fix error * Remove _wc_save_product_price * Remove todo * Fixed wrong variation URLs * Fixed undefined $image_id in WC_Product_Variation::get_image_id() * Allow wc_rest_prepare_date_response() handle timestamps * Updated get methods on REST API for variations * Use variations CRUD to save variations metadata * [Product CRUD] Abstract todos (#12305) * Get dimensions and weights, with soft deprecation * Product attributes * Ratings * Fix read method * Downloads * Feedback * Revert "[Product CRUD] Abstract todos (#12305)" This reverts commit 9a6136fcf88fec16f97457b7c8a4388f7587bfa2. * Remove deprecated get_variation_id() * New default attributes method * [Product CRUD] Product Datastore (#12317) * Fix up tests in the product/* folder. * Handle data store updates for grouped, variable, external, simple, and general data store updates for products. * Variations & variable changes. * Update -functions.php calls to use data store. * Add an interface for the public product data store methods. * Finished product factory tests * Correctly delete in the api, fix up some comments, and implement an interface for the public variable methods. * Fix up delete in all versions of the api * Handle feedback * Match protected decloration to parent * Product crud abstract todos (#12316) * Get dimensions and weights, with soft deprecation * Product attributes * Ratings * Fix read method * Downloads * Feedback * Fix up store * Fixed method returning in write context * Fix error in variation admin * Check for parent value - fixes tax class * Remove old/complete todos * Allow set tax class as "parent" * Removed duplicated sync * Fixed wrong variation URLs * Fixed undefined $image_id in WC_Product_Variation::get_image_id() * Allow wc_rest_prepare_date_response() handle timestamps * Updated get methods on REST API for variations * Use variations CRUD to save variations metadata * Remove deprecated get_variation_id() * New default attributes method * Fixed method returning in write context * Allow set tax class as "parent" * Removed duplicated sync * Fixed coding standards * TODO is not accurate. * Should pass WC_Product instancies to WC_Comments methods (#12327) * Use new method in abstract order class to prevent headers sent issue in tests * Fixed variable description in REST API * Updated how create initial product variation * Fixed a few fatal errors and warnings in Products CRUD (#12329) * Fixed a few fatal errors and warnings in Products CRUD * Fixed sync functions * Add variations CRUD to legacy API (#12331) * Apply crud to variable products in legacy API v1 * New REST API do not need fallback for default attributes * Apply variations CRUD to legacy API v2 * Legacy v2 - save default attributes * Variations in legacy API v2 do not have descriptions * Fixed legacy API v2 variations params * Applied variations CRUD to legacy API v3 * Sync before save in legacy apis * Punc * Removed API todos * Removed test * Products endpoint tweaks (#12354) * Var type already normalized on CRUD * Let Product CRUD handle with validation, sanitization and conditional checks * Set downloads using WC_Product_Download * Stop try catch exceptions more than one time * Handle WC_Data_Exception in legacy API * Complete remove products when fails on creating * On creating I mean! * Already have a method to complete delete products * Fixed standards using WP CodeSniffer * get_the_terms() returns false when empty * get_manage_stock returns boolean @claudiosanches * Merge conflict * Variations API endpoint fixes * Product CRUD improvements (#12359) * args is not used any more - remove todo * Added test for attributes * wc_get_price_excluding_tax usage * parent usage * Fix rating counts * Test fixes * Cleanup after tests * Make sure status transition code runs even during API calls, not just in admin. * Default visibility * Fix attribute setting in API * Use get name instead of get title * variation id usage * Improved cross sell templates * variation_data * Grouped product sync * Notices * Sync is not needed in API * Delete * Rename interfaces * Update counts in data store
2016-11-16 12:38:24 +00:00
$product_ids = array( $product->get_id(), $product->get_parent_id() );
// Specific products get the discount
if ( sizeof( $this->get_product_ids() ) && sizeof( array_intersect( $product_ids, $this->get_product_ids() ) ) ) {
2016-06-27 13:59:50 +00:00
$valid = true;
}
// Category discounts
if ( sizeof( $this->get_product_categories() ) && sizeof( array_intersect( $product_cats, $this->get_product_categories() ) ) ) {
2016-06-27 13:59:50 +00:00
$valid = true;
}
2016-06-27 13:59:50 +00:00
// No product ids - all items discounted
2016-02-23 16:01:40 +00:00
if ( ! sizeof( $this->get_product_ids() ) && ! sizeof( $this->get_product_categories() ) ) {
$valid = true;
}
// Specific product IDs excluded from the discount
if ( sizeof( $this->get_excluded_product_ids() ) && sizeof( array_intersect( $product_ids, $this->get_excluded_product_ids() ) ) ) {
2016-06-27 13:59:50 +00:00
$valid = false;
}
// Specific categories excluded from the discount
if ( sizeof( $this->get_excluded_product_categories() ) && sizeof( array_intersect( $product_cats, $this->get_excluded_product_categories() ) ) ) {
2016-06-27 13:59:50 +00:00
$valid = false;
}
// Sale Items excluded from discount
if ( $this->get_exclude_sale_items() && $product->is_on_sale() ) {
$valid = false;
}
return apply_filters( 'woocommerce_coupon_is_valid_for_product', $valid, $product, $this, $values );
}
2013-02-15 00:29:55 +00:00
/**
2015-11-03 13:31:20 +00:00
* Converts one of the WC_Coupon message/error codes to a message string and.
2013-02-15 03:56:35 +00:00
* displays the message/error.
2013-02-15 00:29:55 +00:00
*
2013-02-15 03:56:35 +00:00
* @param int $msg_code Message/error code.
2013-02-15 00:29:55 +00:00
*/
2013-02-15 03:56:35 +00:00
public function add_coupon_message( $msg_code ) {
2015-03-06 16:36:34 +00:00
$msg = $msg_code < 200 ? $this->get_coupon_error( $msg_code ) : $this->get_coupon_message( $msg_code );
if ( ! $msg ) {
return;
}
if ( $msg_code < 200 ) {
wc_add_notice( $msg, 'error' );
} else {
wc_add_notice( $msg );
}
2013-02-15 03:56:35 +00:00
}
2013-02-15 00:29:55 +00:00
2013-02-15 03:56:35 +00:00
/**
2015-11-03 13:31:20 +00:00
* Map one of the WC_Coupon message codes to a message string.
2013-02-15 03:56:35 +00:00
*
2014-09-07 23:37:55 +00:00
* @param integer $msg_code
2013-02-15 03:56:35 +00:00
* @return string| Message/error string
*/
public function get_coupon_message( $msg_code ) {
switch ( $msg_code ) {
case self::WC_COUPON_SUCCESS :
2013-02-15 03:56:35 +00:00
$msg = __( 'Coupon code applied successfully.', 'woocommerce' );
break;
case self::WC_COUPON_REMOVED :
$msg = __( 'Coupon code removed successfully.', 'woocommerce' );
break;
2013-02-15 03:56:35 +00:00
default:
$msg = '';
break;
2013-02-15 03:56:35 +00:00
}
return apply_filters( 'woocommerce_coupon_message', $msg, $msg_code, $this );
2013-02-15 00:29:55 +00:00
}
2013-02-15 03:56:35 +00:00
/**
2015-11-03 13:31:20 +00:00
* Map one of the WC_Coupon error codes to a message string.
*
* @param int $err_code Message/error code.
* @return string| Message/error string
*/
public function get_coupon_error( $err_code ) {
switch ( $err_code ) {
case self::E_WC_COUPON_INVALID_FILTERED:
$err = __( 'Coupon is not valid.', 'woocommerce' );
break;
case self::E_WC_COUPON_NOT_EXIST:
2016-10-29 17:32:38 +00:00
/* translators: %s: coupon code */
2016-02-23 16:01:40 +00:00
$err = sprintf( __( 'Coupon "%s" does not exist!', 'woocommerce' ), $this->get_code() );
break;
case self::E_WC_COUPON_INVALID_REMOVED:
2016-10-29 17:32:38 +00:00
/* translators: %s: coupon code */
2016-02-23 16:01:40 +00:00
$err = sprintf( __( 'Sorry, it seems the coupon "%s" is invalid - it has now been removed from your order.', 'woocommerce' ), $this->get_code() );
break;
case self::E_WC_COUPON_NOT_YOURS_REMOVED:
2016-10-29 17:32:38 +00:00
/* translators: %s: coupon code */
2016-02-23 16:01:40 +00:00
$err = sprintf( __( 'Sorry, it seems the coupon "%s" is not yours - it has now been removed from your order.', 'woocommerce' ), $this->get_code() );
break;
case self::E_WC_COUPON_ALREADY_APPLIED:
$err = __( 'Coupon code already applied!', 'woocommerce' );
break;
case self::E_WC_COUPON_ALREADY_APPLIED_INDIV_USE_ONLY:
2016-10-29 17:32:38 +00:00
/* translators: %s: coupon code */
2016-02-23 16:01:40 +00:00
$err = sprintf( __( 'Sorry, coupon "%s" has already been applied and cannot be used in conjunction with other coupons.', 'woocommerce' ), $this->get_code() );
break;
case self::E_WC_COUPON_USAGE_LIMIT_REACHED:
$err = __( 'Coupon usage limit has been reached.', 'woocommerce' );
break;
case self::E_WC_COUPON_EXPIRED:
$err = __( 'This coupon has expired.', 'woocommerce' );
break;
case self::E_WC_COUPON_MIN_SPEND_LIMIT_NOT_MET:
2016-10-29 17:32:38 +00:00
/* translators: %s: coupon minimum amount */
2016-02-23 16:01:40 +00:00
$err = sprintf( __( 'The minimum spend for this coupon is %s.', 'woocommerce' ), wc_price( $this->get_minimum_amount() ) );
break;
case self::E_WC_COUPON_MAX_SPEND_LIMIT_MET:
2016-10-29 17:32:38 +00:00
/* translators: %s: coupon maximum amount */
2016-02-23 16:01:40 +00:00
$err = sprintf( __( 'The maximum spend for this coupon is %s.', 'woocommerce' ), wc_price( $this->get_maximum_amount() ) );
break;
case self::E_WC_COUPON_NOT_APPLICABLE:
$err = __( 'Sorry, this coupon is not applicable to your cart contents.', 'woocommerce' );
break;
case self::E_WC_COUPON_EXCLUDED_PRODUCTS:
// Store excluded products that are in cart in $products
$products = array();
2015-05-14 21:18:53 +00:00
if ( ! WC()->cart->is_empty() ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
WIP - Product CRUD (#12065) * Created function to get the catalog visibility options * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * Hardcode the get_type per product class * Initial look through getters and setters and abstract data * Missing var * Add related product functions and deprecate those in class. * No need to exclude ID * Fixed coding standards and improved the docblocks * Get cached terms from wc_get_related_terms() * Fixed wrong variable in wc_get_related_terms * Use count() instead of sizeof() * Sanitize ids later * Remove unneeded comments * wc_get_product_term_ids instead of related wording and use in other places. get_the_terms is used here and also handles caching, something wp_get_post_terms does not. * Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it. * Bump template version * Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function * Made abstract function useful * External Product CRUD * _virtual meta should be 'no', not taxable, in product unit test helper * Grouped product class * Tests * Move children to meta and update test * Use get_upsell_ids * Spacing in query * Moving and refactoring methods * Availability html * Tidy/add todos * Rename method * Put back review functions (still todo) * missing $this * get_price_including_tax/excluding_tax functions * wc_get_price_to_display * Price handling * [Product CRUD] Variable (#12146) * [Product CRUD] Variable Products * Handle PR feedback. * [Product CRUD] Grouped Handling (#12151) * Handle grouped product saving * Update routine * [Product CRUD] Product crud terms (#12149) * Category and tag id handling * Replace template functions * Remove todo * Handle default name in save function * Product crud admin save routine (#12174) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Spacing * Fix comment * wc_implode_text_attributes helper function * [Product CRUD] Product crud admin use getters (#12196) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Move settings into new files * Refactor panels and use getters * Use getters for variation panel * Revert save variation changes for now * Add todos * Fix downloads * REST API CRUD Updates * Additional API updates/fixes. Added some todos * Fix final failing tests and implementing setters/getters and attributes functionality. * Fix comparison for is_on_sale and remove download_type from WC_Product. * Add a wc_get_products wrapper. * Remove the download type input from the product data metabox for downloadable products. (#12221) * [Product CRUD] Variations - setters, getters and admin. (#12228) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * Feedback fixes * Implement CRUD in the legacy REST API * Handle PR feedback * [Product CRUD] Getter setter proxy methods (#12236) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * get_prop implementation in abstract and data classes * Implement set_prop * Change handling * Array key exists * set_object_read * Use get_the_terms() instead of wp_get_post_terms() wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not use the object cache, and generates a database query every time it is used. get_the_terms() however can use data from the object cache if present. * Allow WP_Query to preload post data, and meta in wc_get_products() Allow WP_Query to bulk query for post data and meta if more than just IDs are requested from wc_get_products(). Reduces query count significantly. * [Product CRUD] Variable, variation, notices, and stock handling (#12277) * No longer needed * Remove old todos * Use getters in admin list * Related and upsells update for CRUD * Fix notice in gallery * Variable fixes and todos * Context * Price sync * Revert variation attributes change * Return parent data in view context * Defer term counting * wc_find_matching_product_variation * Stock manage tweaks * Stock fixes * Correct id * correct id * Better sync * Data logic setter fix * feedback * First methods for WP_Product crud * Product set methods * Fixed several erros while setting data * Hardcode the get_type per product class * Initial look through getters and setters and abstract data * Missing var * Fixed coding standards and improved the docblocks * Get cached terms from wc_get_related_terms() * Fixed wrong variable in wc_get_related_terms * Use count() instead of sizeof() * Add related product functions and deprecate those in class. * No need to exclude ID * Sanitize ids later * Clean up the abstract product class a bit, deprecate two functions we have renamed, make update & create work properly, and add some tests for it. * Remove unneeded comments * wc_get_product_term_ids instead of related wording and use in other places. get_the_terms is used here and also handles caching, something wp_get_post_terms does not. * Handle PR feedback: Remove duplicate regular_price update, allow changing of post status for products, remove deprecation for get_title since we might still offer it as a function * External Product CRUD * _virtual meta should be 'no', not taxable, in product unit test helper * Bump template version * Made abstract function useful * Grouped product class * Tests * Move children to meta and update test * Use get_upsell_ids * Spacing in query * Moving and refactoring methods * Availability html * Tidy/add todos * Rename method * Put back review functions (still todo) * missing $this * get_price_including_tax/excluding_tax functions * wc_get_price_to_display * Price handling * [Product CRUD] Variable (#12146) * [Product CRUD] Variable Products * Handle PR feedback. * [Product CRUD] Grouped Handling (#12151) * Handle grouped product saving * Update routine * [Product CRUD] Product crud terms (#12149) * Category and tag id handling * Replace template functions * Remove todo * Handle default name in save function * Product crud admin save routine (#12174) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Spacing * Fix comment * wc_implode_text_attributes helper function * [Product CRUD] Product crud admin use getters (#12196) * Initial props * Work on admin saving * Set/get attributes * Atom was moaning about this before but no longer. * Update get_shipping_class * WC_Product_Attribute * Use getter in admin panel * Fix attribute saving * Move settings into new files * Refactor panels and use getters * Use getters for variation panel * Revert save variation changes for now * Add todos * Fix downloads * REST API CRUD Updates * Additional API updates/fixes. Added some todos * Fix final failing tests and implementing setters/getters and attributes functionality. * Fix comparison for is_on_sale and remove download_type from WC_Product. * Add a wc_get_products wrapper. * Remove the download type input from the product data metabox for downloadable products. (#12221) * [Product CRUD] Variations - setters, getters and admin. (#12228) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * Feedback fixes * Implement CRUD in the legacy REST API * Handle PR feedback * [Product CRUD] Getter setter proxy methods (#12236) * Started on variation changes * Stock functions * Variation class * Bulk change ->id to get_id() to fix variation form display * Missing status * Fix add to cart * Start on stored data save * save variation * Save_variations * Variation edit panel * Save variations code works. * Remove stored data code and fix save * Improve legacy class * wc_bool_to_string * prepare_set_attributes * Use wc_get_products * More feedback fixes * get_prop implementation in abstract and data classes * Implement set_prop * Change handling * Array key exists * set_object_read * Use get_the_terms() instead of wp_get_post_terms() wp_get_post_terms() is a wrapper around wp_get_object_terms() which does not use the object cache, and generates a database query every time it is used. get_the_terms() however can use data from the object cache if present. * [Product CRUD] Variable, variation, notices, and stock handling (#12277) * No longer needed * Remove old todos * Use getters in admin list * Related and upsells update for CRUD * Fix notice in gallery * Variable fixes and todos * Context * Price sync * Revert variation attributes change * Return parent data in view context * Defer term counting * wc_find_matching_product_variation * Stock manage tweaks * Stock fixes * Correct id * correct id * Better sync * Data logic setter fix * feedback * Prevent notices * Handle image_id from parent * Fix error * Remove _wc_save_product_price * Remove todo * Fixed wrong variation URLs * Fixed undefined $image_id in WC_Product_Variation::get_image_id() * Allow wc_rest_prepare_date_response() handle timestamps * Updated get methods on REST API for variations * Use variations CRUD to save variations metadata * [Product CRUD] Abstract todos (#12305) * Get dimensions and weights, with soft deprecation * Product attributes * Ratings * Fix read method * Downloads * Feedback * Revert "[Product CRUD] Abstract todos (#12305)" This reverts commit 9a6136fcf88fec16f97457b7c8a4388f7587bfa2. * Remove deprecated get_variation_id() * New default attributes method * [Product CRUD] Product Datastore (#12317) * Fix up tests in the product/* folder. * Handle data store updates for grouped, variable, external, simple, and general data store updates for products. * Variations & variable changes. * Update -functions.php calls to use data store. * Add an interface for the public product data store methods. * Finished product factory tests * Correctly delete in the api, fix up some comments, and implement an interface for the public variable methods. * Fix up delete in all versions of the api * Handle feedback * Match protected decloration to parent * Product crud abstract todos (#12316) * Get dimensions and weights, with soft deprecation * Product attributes * Ratings * Fix read method * Downloads * Feedback * Fix up store * Fixed method returning in write context * Fix error in variation admin * Check for parent value - fixes tax class * Remove old/complete todos * Allow set tax class as "parent" * Removed duplicated sync * Fixed wrong variation URLs * Fixed undefined $image_id in WC_Product_Variation::get_image_id() * Allow wc_rest_prepare_date_response() handle timestamps * Updated get methods on REST API for variations * Use variations CRUD to save variations metadata * Remove deprecated get_variation_id() * New default attributes method * Fixed method returning in write context * Allow set tax class as "parent" * Removed duplicated sync * Fixed coding standards * TODO is not accurate. * Should pass WC_Product instancies to WC_Comments methods (#12327) * Use new method in abstract order class to prevent headers sent issue in tests * Fixed variable description in REST API * Updated how create initial product variation * Fixed a few fatal errors and warnings in Products CRUD (#12329) * Fixed a few fatal errors and warnings in Products CRUD * Fixed sync functions * Add variations CRUD to legacy API (#12331) * Apply crud to variable products in legacy API v1 * New REST API do not need fallback for default attributes * Apply variations CRUD to legacy API v2 * Legacy v2 - save default attributes * Variations in legacy API v2 do not have descriptions * Fixed legacy API v2 variations params * Applied variations CRUD to legacy API v3 * Sync before save in legacy apis * Punc * Removed API todos * Removed test * Products endpoint tweaks (#12354) * Var type already normalized on CRUD * Let Product CRUD handle with validation, sanitization and conditional checks * Set downloads using WC_Product_Download * Stop try catch exceptions more than one time * Handle WC_Data_Exception in legacy API * Complete remove products when fails on creating * On creating I mean! * Already have a method to complete delete products * Fixed standards using WP CodeSniffer * get_the_terms() returns false when empty * get_manage_stock returns boolean @claudiosanches * Merge conflict * Variations API endpoint fixes * Product CRUD improvements (#12359) * args is not used any more - remove todo * Added test for attributes * wc_get_price_excluding_tax usage * parent usage * Fix rating counts * Test fixes * Cleanup after tests * Make sure status transition code runs even during API calls, not just in admin. * Default visibility * Fix attribute setting in API * Use get name instead of get title * variation id usage * Improved cross sell templates * variation_data * Grouped product sync * Notices * Sync is not needed in API * Delete * Rename interfaces * Update counts in data store
2016-11-16 12:38:24 +00:00
if ( in_array( $cart_item['product_id'], $this->get_excluded_product_ids() ) || in_array( $cart_item['variation_id'], $this->get_excluded_product_ids() ) || in_array( $cart_item['data']->get_parent_id(), $this->get_excluded_product_ids() ) ) {
$products[] = $cart_item['data']->get_name();
}
}
}
2016-10-29 17:32:38 +00:00
/* translators: %s: products list */
$err = sprintf( __( 'Sorry, this coupon is not applicable to the products: %s.', 'woocommerce' ), implode( ', ', $products ) );
break;
case self::E_WC_COUPON_EXCLUDED_CATEGORIES:
// Store excluded categories that are in cart in $categories
$categories = array();
2015-05-14 21:18:53 +00:00
if ( ! WC()->cart->is_empty() ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product_cats = wc_get_product_cat_ids( $cart_item['product_id'] );
2016-02-23 16:01:40 +00:00
if ( sizeof( $intersect = array_intersect( $product_cats, $this->get_excluded_product_categories() ) ) > 0 ) {
foreach ( $intersect as $cat_id ) {
$cat = get_term( $cat_id, 'product_cat' );
$categories[] = $cat->name;
}
}
}
}
2016-10-29 17:32:38 +00:00
/* translators: %s: categories list */
$err = sprintf( __( 'Sorry, this coupon is not applicable to the categories: %s.', 'woocommerce' ), implode( ', ', array_unique( $categories ) ) );
break;
case self::E_WC_COUPON_NOT_VALID_SALE_ITEMS:
$err = __( 'Sorry, this coupon is not valid for sale items.', 'woocommerce' );
break;
default:
$err = '';
break;
}
return apply_filters( 'woocommerce_coupon_error', $err, $err_code, $this );
}
/**
2015-11-03 13:31:20 +00:00
* Map one of the WC_Coupon error codes to an error string.
2013-02-15 03:56:35 +00:00
* No coupon instance will be available where a coupon does not exist,
* so this static method exists.
*
* @param int $err_code Error code
* @return string| Error string
*/
public static function get_generic_coupon_error( $err_code ) {
switch ( $err_code ) {
case self::E_WC_COUPON_NOT_EXIST:
$err = __( 'Coupon does not exist!', 'woocommerce' );
break;
case self::E_WC_COUPON_PLEASE_ENTER:
$err = __( 'Please enter a coupon code.', 'woocommerce' );
break;
2013-02-15 03:56:35 +00:00
default:
$err = '';
break;
2013-02-15 03:56:35 +00:00
}
// When using this static method, there is no $this to pass to filter
return apply_filters( 'woocommerce_coupon_error', $err, $err_code, null );
2013-02-15 03:56:35 +00:00
}
2013-01-29 13:23:52 +00:00
}