abstracts
9 years ago
admin
9 years ago
api
9 years ago
cli
9 years ago
data-stores
9 years ago
emails
9 years ago
gateways
9 years ago
interfaces
9 years ago
legacy
9 years ago
libraries
9 years ago
log-handlers
9 years ago
payment-tokens
9 years ago
shipping
9 years ago
shortcodes
9 years ago
theme-support
9 years ago
vendor
9 years ago
walkers
9 years ago
widgets
9 years ago
class-wc-ajax.php
9 years ago
class-wc-api.php
9 years ago
class-wc-auth.php
9 years ago
class-wc-autoloader.php
9 years ago
class-wc-background-emailer.php
9 years ago
class-wc-background-updater.php
9 years ago
class-wc-breadcrumb.php
9 years ago
class-wc-cache-helper.php
9 years ago
class-wc-cart.php
9 years ago
class-wc-checkout.php
9 years ago
class-wc-cli.php
9 years ago
class-wc-comments.php
9 years ago
class-wc-countries.php
9 years ago
class-wc-coupon.php
9 years ago
class-wc-customer-download.php
9 years ago
class-wc-customer.php
9 years ago
class-wc-data-exception.php
9 years ago
class-wc-data-store.php
9 years ago
class-wc-datetime.php
9 years ago
class-wc-deprecated-action-hooks.php
9 years ago
class-wc-deprecated-filter-hooks.php
9 years ago
class-wc-download-handler.php
9 years ago
class-wc-emails.php
9 years ago
class-wc-embed.php
9 years ago
class-wc-form-handler.php
9 years ago
class-wc-frontend-scripts.php
9 years ago
class-wc-geo-ip.php
9 years ago
class-wc-geolocation.php
9 years ago
class-wc-https.php
9 years ago
class-wc-install.php
9 years ago
class-wc-integrations.php
9 years ago
class-wc-legacy-api.php
9 years ago
class-wc-log-levels.php
9 years ago
class-wc-logger.php
9 years ago
class-wc-order-factory.php
9 years ago
class-wc-order-item-coupon.php
9 years ago
class-wc-order-item-fee.php
9 years ago
class-wc-order-item-meta.php
9 years ago
class-wc-order-item-product.php
9 years ago
class-wc-order-item-shipping.php
9 years ago
class-wc-order-item-tax.php
9 years ago
class-wc-order-item.php
9 years ago
class-wc-order-refund.php
9 years ago
class-wc-order.php
9 years ago
class-wc-payment-gateways.php
9 years ago
class-wc-payment-tokens.php
9 years ago
class-wc-post-data.php
9 years ago
class-wc-post-types.php
9 years ago
class-wc-product-attribute.php
9 years ago
class-wc-product-download.php
9 years ago
class-wc-product-external.php
9 years ago
class-wc-product-factory.php
9 years ago
class-wc-product-grouped.php
9 years ago
class-wc-product-simple.php
9 years ago
class-wc-product-variable.php
9 years ago
class-wc-product-variation.php
9 years ago
class-wc-query.php
9 years ago
class-wc-register-wp-admin-settings.php
9 years ago
class-wc-session-handler.php
9 years ago
class-wc-shipping-rate.php
9 years ago
class-wc-shipping-zone.php
9 years ago
class-wc-shipping-zones.php
9 years ago
class-wc-shipping.php
9 years ago
class-wc-shortcodes.php
9 years ago
class-wc-structured-data.php
9 years ago
class-wc-tax.php
9 years ago
class-wc-template-loader.php
9 years ago
class-wc-tracker.php
9 years ago
class-wc-validation.php
9 years ago
class-wc-webhook.php
9 years ago
wc-account-functions.php
9 years ago
wc-attribute-functions.php
9 years ago
wc-cart-functions.php
9 years ago
wc-conditional-functions.php
9 years ago
wc-core-functions.php
9 years ago
wc-coupon-functions.php
9 years ago
wc-deprecated-functions.php
9 years ago
wc-formatting-functions.php
9 years ago
wc-notice-functions.php
9 years ago
wc-order-functions.php
9 years ago
wc-order-item-functions.php
9 years ago
wc-page-functions.php
9 years ago
wc-product-functions.php
9 years ago
wc-rest-functions.php
9 years ago
wc-stock-functions.php
9 years ago
wc-template-functions.php
9 years ago
wc-template-hooks.php
9 years ago
wc-term-functions.php
9 years ago
wc-update-functions.php
9 years ago
wc-user-functions.php
9 years ago
wc-webhook-functions.php
9 years ago
wc-widget-functions.php
9 years ago
class-wc-product-variation.php
433 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; |
| 4 | } |
| 5 | |
| 6 | /** |
| 7 | * Product Variation Class. |
| 8 | * |
| 9 | * The WooCommerce product variation class handles product variation data. |
| 10 | * |
| 11 | * @class WC_Product_Variation |
| 12 | * @version 3.0.0 |
| 13 | * @package WooCommerce/Classes |
| 14 | * @category Class |
| 15 | * @author WooThemes |
| 16 | */ |
| 17 | class WC_Product_Variation extends WC_Product_Simple { |
| 18 | |
| 19 | /** |
| 20 | * Post type. |
| 21 | * @var string |
| 22 | */ |
| 23 | public $post_type = 'product_variation'; |
| 24 | |
| 25 | /** |
| 26 | * Parent data. |
| 27 | * @var array |
| 28 | */ |
| 29 | protected $parent_data = array( |
| 30 | 'title' => '', |
| 31 | 'sku' => '', |
| 32 | 'manage_stock' => '', |
| 33 | 'backorders' => '', |
| 34 | 'stock_quantity' => '', |
| 35 | 'weight' => '', |
| 36 | 'length' => '', |
| 37 | 'width' => '', |
| 38 | 'height' => '', |
| 39 | 'tax_class' => '', |
| 40 | 'shipping_class_id' => '', |
| 41 | 'image_id' => '', |
| 42 | ); |
| 43 | |
| 44 | /** |
| 45 | * Prefix for action and filter hooks on data. |
| 46 | * |
| 47 | * @since 3.0.0 |
| 48 | * @return string |
| 49 | */ |
| 50 | protected function get_hook_prefix() { |
| 51 | return 'woocommerce_product_variation_get_'; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Get internal type. |
| 56 | * @return string |
| 57 | */ |
| 58 | public function get_type() { |
| 59 | return 'variation'; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * If the stock level comes from another product ID. |
| 64 | * @since 3.0.0 |
| 65 | * @return int |
| 66 | */ |
| 67 | public function get_stock_managed_by_id() { |
| 68 | return 'parent' === $this->get_manage_stock() ? $this->get_parent_id() : $this->get_id(); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Get the product's title. For variations this is the parent product name. |
| 73 | * |
| 74 | * @return string |
| 75 | */ |
| 76 | public function get_title() { |
| 77 | return apply_filters( 'woocommerce_product_title', $this->parent_data['title'], $this ); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Get variation attribute values. Keys are prefixed with attribute_, as stored. |
| 82 | * |
| 83 | * @return array of attributes and their values for this variation |
| 84 | */ |
| 85 | public function get_variation_attributes() { |
| 86 | $attributes = $this->get_attributes(); |
| 87 | $variation_attributes = array(); |
| 88 | foreach ( $attributes as $key => $value ) { |
| 89 | $variation_attributes[ 'attribute_' . $key ] = $value; |
| 90 | } |
| 91 | return $variation_attributes; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Returns a single product attribute as a string. |
| 96 | * @param string $attribute to get. |
| 97 | * @return string |
| 98 | */ |
| 99 | public function get_attribute( $attribute ) { |
| 100 | $attributes = $this->get_attributes(); |
| 101 | $attribute = sanitize_title( $attribute ); |
| 102 | |
| 103 | if ( isset( $attributes[ $attribute ] ) ) { |
| 104 | $value = $attributes[ $attribute ]; |
| 105 | $term = taxonomy_exists( $attribute ) ? get_term_by( 'slug', $value, $attribute ) : false; |
| 106 | $value = ! is_wp_error( $term ) && $term ? $term->name : $value; |
| 107 | } elseif ( isset( $attributes[ 'pa_' . $attribute ] ) ) { |
| 108 | $value = $attributes[ 'pa_' . $attribute ]; |
| 109 | $term = taxonomy_exists( 'pa_' . $attribute ) ? get_term_by( 'slug', $value, 'pa_' . $attribute ) : false; |
| 110 | $value = ! is_wp_error( $term ) && $term ? $term->name : $value; |
| 111 | } else { |
| 112 | return ''; |
| 113 | } |
| 114 | |
| 115 | return $value; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Wrapper for get_permalink. Adds this variations attributes to the URL. |
| 120 | * |
| 121 | * @param $item_object item array If a cart or order item is passed, we can get a link containing the exact attributes selected for the variation, rather than the default attributes. |
| 122 | * @return string |
| 123 | */ |
| 124 | public function get_permalink( $item_object = null ) { |
| 125 | $url = get_permalink( $this->get_parent_id() ); |
| 126 | |
| 127 | if ( ! empty( $item_object['variation'] ) ) { |
| 128 | $data = $item_object['variation']; |
| 129 | } elseif ( ! empty( $item_object['item_meta_array'] ) ) { |
| 130 | $data_keys = array_map( 'wc_variation_attribute_name', wp_list_pluck( $item_object['item_meta_array'], 'key' ) ); |
| 131 | $data_values = wp_list_pluck( $item_object['item_meta_array'], 'value' ); |
| 132 | $data = array_intersect_key( array_combine( $data_keys, $data_values ), $this->get_variation_attributes() ); |
| 133 | } else { |
| 134 | $data = $this->get_variation_attributes(); |
| 135 | } |
| 136 | |
| 137 | return add_query_arg( array_map( 'urlencode', array_filter( $data ) ), $url ); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Get the add to url used mainly in loops. |
| 142 | * |
| 143 | * @return string |
| 144 | */ |
| 145 | public function add_to_cart_url() { |
| 146 | $variation_data = array_map( 'urlencode', $this->get_variation_attributes() ); |
| 147 | $url = $this->is_purchasable() ? remove_query_arg( 'added-to-cart', add_query_arg( array( 'variation_id' => $this->get_id(), 'add-to-cart' => $this->get_parent_id() ), $this->get_permalink() ) ) : $this->get_permalink(); |
| 148 | return apply_filters( 'woocommerce_product_add_to_cart_url', $url, $this ); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Get SKU (Stock-keeping unit) - product unique ID. |
| 153 | * |
| 154 | * @param string $context |
| 155 | * @return string |
| 156 | */ |
| 157 | public function get_sku( $context = 'view' ) { |
| 158 | $value = $this->get_prop( 'sku', $context ); |
| 159 | |
| 160 | // Inherit value from parent. |
| 161 | if ( 'view' === $context && empty( $value ) ) { |
| 162 | $value = apply_filters( $this->get_hook_prefix() . 'sku', $this->parent_data['sku'], $this ); |
| 163 | } |
| 164 | return $value; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Returns the product's weight. |
| 169 | * |
| 170 | * @param string $context |
| 171 | * @return string |
| 172 | */ |
| 173 | public function get_weight( $context = 'view' ) { |
| 174 | $value = $this->get_prop( 'weight', $context ); |
| 175 | |
| 176 | // Inherit value from parent. |
| 177 | if ( 'view' === $context && empty( $value ) ) { |
| 178 | $value = apply_filters( $this->get_hook_prefix() . 'weight', $this->parent_data['weight'], $this ); |
| 179 | } |
| 180 | return $value; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Returns the product length. |
| 185 | * |
| 186 | * @param string $context |
| 187 | * @return string |
| 188 | */ |
| 189 | public function get_length( $context = 'view' ) { |
| 190 | $value = $this->get_prop( 'length', $context ); |
| 191 | |
| 192 | // Inherit value from parent. |
| 193 | if ( 'view' === $context && empty( $value ) ) { |
| 194 | $value = apply_filters( $this->get_hook_prefix() . 'length', $this->parent_data['length'], $this ); |
| 195 | } |
| 196 | return $value; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Returns the product width. |
| 201 | * |
| 202 | * @param string $context |
| 203 | * @return string |
| 204 | */ |
| 205 | public function get_width( $context = 'view' ) { |
| 206 | $value = $this->get_prop( 'width', $context ); |
| 207 | |
| 208 | // Inherit value from parent. |
| 209 | if ( 'view' === $context && empty( $value ) ) { |
| 210 | $value = apply_filters( $this->get_hook_prefix() . 'width', $this->parent_data['width'], $this ); |
| 211 | } |
| 212 | return $value; |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Returns the product height. |
| 217 | * |
| 218 | * @param string $context |
| 219 | * @return string |
| 220 | */ |
| 221 | public function get_height( $context = 'view' ) { |
| 222 | $value = $this->get_prop( 'height', $context ); |
| 223 | |
| 224 | // Inherit value from parent. |
| 225 | if ( 'view' === $context && empty( $value ) ) { |
| 226 | $value = apply_filters( $this->get_hook_prefix() . 'height', $this->parent_data['height'], $this ); |
| 227 | } |
| 228 | return $value; |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Returns the tax class. |
| 233 | * |
| 234 | * Does not use get_prop so it can handle 'parent' Inheritance correctly. |
| 235 | * |
| 236 | * @param string $context view, edit, or unfiltered |
| 237 | * @return string |
| 238 | */ |
| 239 | public function get_tax_class( $context = 'view' ) { |
| 240 | $value = null; |
| 241 | |
| 242 | if ( array_key_exists( 'tax_class', $this->data ) ) { |
| 243 | $value = array_key_exists( 'tax_class', $this->changes ) ? $this->changes['tax_class'] : $this->data['tax_class']; |
| 244 | |
| 245 | if ( 'edit' !== $context && 'parent' === $value ) { |
| 246 | $value = $this->parent_data['tax_class']; |
| 247 | } |
| 248 | |
| 249 | if ( 'view' === $context ) { |
| 250 | $value = apply_filters( $this->get_hook_prefix() . 'tax_class', $value, $this ); |
| 251 | } |
| 252 | } |
| 253 | return $value; |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Return if product manage stock. |
| 258 | * |
| 259 | * @since 3.0.0 |
| 260 | * @param string $context |
| 261 | * @return boolean|string true, false, or parent. |
| 262 | */ |
| 263 | public function get_manage_stock( $context = 'view' ) { |
| 264 | $value = $this->get_prop( 'manage_stock', $context ); |
| 265 | |
| 266 | // Inherit value from parent. |
| 267 | if ( 'view' === $context && false === $value && true === wc_string_to_bool( $this->parent_data['manage_stock'] ) ) { |
| 268 | $value = 'parent'; |
| 269 | } |
| 270 | return $value; |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Returns number of items available for sale. |
| 275 | * |
| 276 | * @param string $context |
| 277 | * @return int|null |
| 278 | */ |
| 279 | public function get_stock_quantity( $context = 'view' ) { |
| 280 | $value = $this->get_prop( 'stock_quantity', $context ); |
| 281 | |
| 282 | // Inherit value from parent. |
| 283 | if ( 'view' === $context && 'parent' === $this->get_manage_stock() ) { |
| 284 | $value = apply_filters( $this->get_hook_prefix() . 'stock_quantity', $this->parent_data['stock_quantity'], $this ); |
| 285 | } |
| 286 | return $value; |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * Get backorders. |
| 291 | * |
| 292 | * @param string $context |
| 293 | * @since 3.0.0 |
| 294 | * @return string yes no or notify |
| 295 | */ |
| 296 | public function get_backorders( $context = 'view' ) { |
| 297 | $value = $this->get_prop( 'backorders', $context ); |
| 298 | |
| 299 | // Inherit value from parent. |
| 300 | if ( 'view' === $context && 'parent' === $this->get_manage_stock() ) { |
| 301 | $value = apply_filters( $this->get_hook_prefix() . 'backorders', $this->parent_data['backorders'], $this ); |
| 302 | } |
| 303 | return $value; |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * Get main image ID. |
| 308 | * |
| 309 | * @since 3.0.0 |
| 310 | * @param string $context |
| 311 | * @return string |
| 312 | */ |
| 313 | public function get_image_id( $context = 'view' ) { |
| 314 | $image_id = $this->get_prop( 'image_id', $context ); |
| 315 | |
| 316 | if ( 'view' === $context && ! $image_id ) { |
| 317 | $value = apply_filters( $this->get_hook_prefix() . 'image_id', $this->parent_data['image_id'], $this ); |
| 318 | } |
| 319 | |
| 320 | return $image_id; |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * Get shipping class ID. |
| 325 | * |
| 326 | * @since 3.0.0 |
| 327 | * @param string $context |
| 328 | * @return int |
| 329 | */ |
| 330 | public function get_shipping_class_id( $context = 'view' ) { |
| 331 | $shipping_class_id = $this->get_prop( 'shipping_class_id', $context ); |
| 332 | |
| 333 | if ( 'view' === $context && ! $shipping_class_id ) { |
| 334 | $value = apply_filters( $this->get_hook_prefix() . 'shipping_class_id', $this->parent_data['shipping_class_id'], $this ); |
| 335 | } |
| 336 | |
| 337 | return $shipping_class_id; |
| 338 | } |
| 339 | |
| 340 | /* |
| 341 | |-------------------------------------------------------------------------- |
| 342 | | CRUD methods |
| 343 | |-------------------------------------------------------------------------- |
| 344 | */ |
| 345 | |
| 346 | /** |
| 347 | * Set the parent data array for this variation. |
| 348 | * |
| 349 | * @since 3.0.0 |
| 350 | * @param array |
| 351 | */ |
| 352 | public function set_parent_data( $parent_data ) { |
| 353 | $this->parent_data = $parent_data; |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * Get the parent data array for this variation. |
| 358 | * |
| 359 | * @since 3.0.0 |
| 360 | * @return array |
| 361 | */ |
| 362 | public function get_parent_data() { |
| 363 | return $this->parent_data; |
| 364 | } |
| 365 | |
| 366 | /** |
| 367 | * Set attributes. Unlike the parent product which uses terms, variations are assigned |
| 368 | * specific attributes using name value pairs. |
| 369 | * @param array $raw_attributes |
| 370 | */ |
| 371 | public function set_attributes( $raw_attributes ) { |
| 372 | $raw_attributes = (array) $raw_attributes; |
| 373 | $attributes = array(); |
| 374 | |
| 375 | foreach ( $raw_attributes as $key => $value ) { |
| 376 | // Remove attribute prefix which meta gets stored with. |
| 377 | if ( 0 === strpos( $key, 'attribute_' ) ) { |
| 378 | $key = substr( $key, 10 ); |
| 379 | } |
| 380 | $attributes[ $key ] = $value; |
| 381 | } |
| 382 | $this->set_prop( 'attributes', $attributes ); |
| 383 | } |
| 384 | |
| 385 | /** |
| 386 | * Returns array of attribute name value pairs. Keys are prefixed with attribute_, as stored. |
| 387 | * |
| 388 | * @param string $context |
| 389 | * @return array |
| 390 | */ |
| 391 | public function get_attributes( $context = 'view' ) { |
| 392 | return $this->get_prop( 'attributes', $context ); |
| 393 | } |
| 394 | |
| 395 | /* |
| 396 | |-------------------------------------------------------------------------- |
| 397 | | Conditionals |
| 398 | |-------------------------------------------------------------------------- |
| 399 | */ |
| 400 | |
| 401 | /** |
| 402 | * Returns false if the product cannot be bought. |
| 403 | * Override abstract method so that: i) Disabled variations are not be purchasable by admins. ii) Enabled variations are not purchasable if the parent product is not purchasable. |
| 404 | * |
| 405 | * @return bool |
| 406 | */ |
| 407 | public function is_purchasable() { |
| 408 | return apply_filters( 'woocommerce_variation_is_purchasable', $this->variation_is_visible() && parent::is_purchasable(), $this ); |
| 409 | } |
| 410 | |
| 411 | /** |
| 412 | * Controls whether this particular variation will appear greyed-out (inactive) or not (active). |
| 413 | * Used by extensions to make incompatible variations appear greyed-out, etc. |
| 414 | * Other possible uses: prevent out-of-stock variations from being selected. |
| 415 | * |
| 416 | * @return bool |
| 417 | */ |
| 418 | public function variation_is_active() { |
| 419 | return apply_filters( 'woocommerce_variation_is_active', true, $this ); |
| 420 | } |
| 421 | |
| 422 | /** |
| 423 | * Checks if this particular variation is visible. Invisible variations are enabled and can be selected, but no price / stock info is displayed. |
| 424 | * Instead, a suitable 'unavailable' message is displayed. |
| 425 | * Invisible by default: Disabled variations and variations with an empty price. |
| 426 | * |
| 427 | * @return bool |
| 428 | */ |
| 429 | public function variation_is_visible() { |
| 430 | return apply_filters( 'woocommerce_variation_is_visible', 'publish' === get_post_status( $this->get_id() ) && '' !== $this->get_price(), $this->get_id(), $this->get_parent_id(), $this ); |
| 431 | } |
| 432 | } |
| 433 |