api
3 years ago
abstract-wc-legacy-order.php
5 years ago
abstract-wc-legacy-payment-token.php
5 years ago
abstract-wc-legacy-product.php
5 years ago
class-wc-legacy-api.php
4 years ago
class-wc-legacy-cart.php
5 years ago
class-wc-legacy-coupon.php
5 years ago
class-wc-legacy-customer.php
5 years ago
class-wc-legacy-shipping-zone.php
5 years ago
class-wc-legacy-webhook.php
5 years ago
class-wc-legacy-coupon.php
205 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; |
| 4 | } |
| 5 | |
| 6 | /** |
| 7 | * Legacy Coupon. |
| 8 | * |
| 9 | * Legacy and deprecated functions are here to keep the WC_Legacy_Coupon class clean. |
| 10 | * This class will be removed in future versions. |
| 11 | * |
| 12 | * @class WC_Legacy_Coupon |
| 13 | * @version 3.0.0 |
| 14 | * @package WooCommerce\Classes |
| 15 | * @category Class |
| 16 | * @author WooThemes |
| 17 | */ |
| 18 | abstract class WC_Legacy_Coupon extends WC_Data { |
| 19 | |
| 20 | /** |
| 21 | * Magic __isset method for backwards compatibility. Legacy properties which could be accessed directly in the past. |
| 22 | * @param string $key |
| 23 | * @return bool |
| 24 | */ |
| 25 | public function __isset( $key ) { |
| 26 | $legacy_keys = array( |
| 27 | 'id', |
| 28 | 'exists', |
| 29 | 'coupon_custom_fields', |
| 30 | 'type', |
| 31 | 'discount_type', |
| 32 | 'amount', |
| 33 | 'coupon_amount', |
| 34 | 'code', |
| 35 | 'individual_use', |
| 36 | 'product_ids', |
| 37 | 'exclude_product_ids', |
| 38 | 'usage_limit', |
| 39 | 'usage_limit_per_user', |
| 40 | 'limit_usage_to_x_items', |
| 41 | 'usage_count', |
| 42 | 'expiry_date', |
| 43 | 'product_categories', |
| 44 | 'exclude_product_categories', |
| 45 | 'minimum_amount', |
| 46 | 'maximum_amount', |
| 47 | 'customer_email', |
| 48 | ); |
| 49 | if ( in_array( $key, $legacy_keys ) ) { |
| 50 | return true; |
| 51 | } |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Magic __get method for backwards compatibility. Maps legacy vars to new getters. |
| 57 | * @param string $key |
| 58 | * @return mixed |
| 59 | */ |
| 60 | public function __get( $key ) { |
| 61 | wc_doing_it_wrong( $key, 'Coupon properties should not be accessed directly.', '3.0' ); |
| 62 | |
| 63 | switch ( $key ) { |
| 64 | case 'id' : |
| 65 | $value = $this->get_id(); |
| 66 | break; |
| 67 | case 'exists' : |
| 68 | $value = $this->get_id() > 0; |
| 69 | break; |
| 70 | case 'coupon_custom_fields' : |
| 71 | $legacy_custom_fields = array(); |
| 72 | $custom_fields = $this->get_id() ? $this->get_meta_data() : array(); |
| 73 | if ( ! empty( $custom_fields ) ) { |
| 74 | foreach ( $custom_fields as $cf_value ) { |
| 75 | // legacy only supports 1 key |
| 76 | $legacy_custom_fields[ $cf_value->key ][0] = $cf_value->value; |
| 77 | } |
| 78 | } |
| 79 | $value = $legacy_custom_fields; |
| 80 | break; |
| 81 | case 'type' : |
| 82 | case 'discount_type' : |
| 83 | $value = $this->get_discount_type(); |
| 84 | break; |
| 85 | case 'amount' : |
| 86 | case 'coupon_amount' : |
| 87 | $value = $this->get_amount(); |
| 88 | break; |
| 89 | case 'code' : |
| 90 | $value = $this->get_code(); |
| 91 | break; |
| 92 | case 'individual_use' : |
| 93 | $value = ( true === $this->get_individual_use() ) ? 'yes' : 'no'; |
| 94 | break; |
| 95 | case 'product_ids' : |
| 96 | $value = $this->get_product_ids(); |
| 97 | break; |
| 98 | case 'exclude_product_ids' : |
| 99 | $value = $this->get_excluded_product_ids(); |
| 100 | break; |
| 101 | case 'usage_limit' : |
| 102 | $value = $this->get_usage_limit(); |
| 103 | break; |
| 104 | case 'usage_limit_per_user' : |
| 105 | $value = $this->get_usage_limit_per_user(); |
| 106 | break; |
| 107 | case 'limit_usage_to_x_items' : |
| 108 | $value = $this->get_limit_usage_to_x_items(); |
| 109 | break; |
| 110 | case 'usage_count' : |
| 111 | $value = $this->get_usage_count(); |
| 112 | break; |
| 113 | case 'expiry_date' : |
| 114 | $value = ( $this->get_date_expires() ? $this->get_date_expires()->date( 'Y-m-d' ) : '' ); |
| 115 | break; |
| 116 | case 'product_categories' : |
| 117 | $value = $this->get_product_categories(); |
| 118 | break; |
| 119 | case 'exclude_product_categories' : |
| 120 | $value = $this->get_excluded_product_categories(); |
| 121 | break; |
| 122 | case 'minimum_amount' : |
| 123 | $value = $this->get_minimum_amount(); |
| 124 | break; |
| 125 | case 'maximum_amount' : |
| 126 | $value = $this->get_maximum_amount(); |
| 127 | break; |
| 128 | case 'customer_email' : |
| 129 | $value = $this->get_email_restrictions(); |
| 130 | break; |
| 131 | default : |
| 132 | $value = ''; |
| 133 | break; |
| 134 | } |
| 135 | |
| 136 | return $value; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Format loaded data as array. |
| 141 | * @param string|array $array |
| 142 | * @return array |
| 143 | */ |
| 144 | public function format_array( $array ) { |
| 145 | wc_deprecated_function( 'WC_Coupon::format_array', '3.0' ); |
| 146 | if ( ! is_array( $array ) ) { |
| 147 | if ( is_serialized( $array ) ) { |
| 148 | $array = maybe_unserialize( $array ); |
| 149 | } else { |
| 150 | $array = explode( ',', $array ); |
| 151 | } |
| 152 | } |
| 153 | return array_filter( array_map( 'trim', array_map( 'strtolower', $array ) ) ); |
| 154 | } |
| 155 | |
| 156 | |
| 157 | /** |
| 158 | * Check if coupon needs applying before tax. |
| 159 | * |
| 160 | * @return bool |
| 161 | */ |
| 162 | public function apply_before_tax() { |
| 163 | wc_deprecated_function( 'WC_Coupon::apply_before_tax', '3.0' ); |
| 164 | return true; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Check if a coupon enables free shipping. |
| 169 | * |
| 170 | * @return bool |
| 171 | */ |
| 172 | public function enable_free_shipping() { |
| 173 | wc_deprecated_function( 'WC_Coupon::enable_free_shipping', '3.0', 'WC_Coupon::get_free_shipping' ); |
| 174 | return $this->get_free_shipping(); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Check if a coupon excludes sale items. |
| 179 | * |
| 180 | * @return bool |
| 181 | */ |
| 182 | public function exclude_sale_items() { |
| 183 | wc_deprecated_function( 'WC_Coupon::exclude_sale_items', '3.0', 'WC_Coupon::get_exclude_sale_items' ); |
| 184 | return $this->get_exclude_sale_items(); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Increase usage count for current coupon. |
| 189 | * |
| 190 | * @param string $used_by Either user ID or billing email |
| 191 | */ |
| 192 | public function inc_usage_count( $used_by = '' ) { |
| 193 | $this->increase_usage_count( $used_by ); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Decrease usage count for current coupon. |
| 198 | * |
| 199 | * @param string $used_by Either user ID or billing email |
| 200 | */ |
| 201 | public function dcr_usage_count( $used_by = '' ) { |
| 202 | $this->decrease_usage_count( $used_by ); |
| 203 | } |
| 204 | } |
| 205 |