class-wc-product-variation.php
| 1 | <?php |
| 2 | /** |
| 3 | * Product Variation |
| 4 | * |
| 5 | * The WooCommerce product variation class handles product variation data. |
| 6 | * |
| 7 | * @package WooCommerce\Classes |
| 8 | * @version 3.0.0 |
| 9 | */ |
| 10 | |
| 11 | use Automattic\WooCommerce\Enums\ProductStatus; |
| 12 | use Automattic\WooCommerce\Enums\ProductType; |
| 13 | use Automattic\WooCommerce\Enums\CatalogVisibility; |
| 14 | |
| 15 | defined( 'ABSPATH' ) || exit; |
| 16 | |
| 17 | /** |
| 18 | * Product variation class. |
| 19 | */ |
| 20 | class WC_Product_Variation extends WC_Product_Simple { |
| 21 | |
| 22 | /** |
| 23 | * Post type. |
| 24 | * |
| 25 | * @var string |
| 26 | */ |
| 27 | protected $post_type = 'product_variation'; |
| 28 | |
| 29 | /** |
| 30 | * Parent data. |
| 31 | * |
| 32 | * @var array |
| 33 | */ |
| 34 | protected $parent_data = array( |
| 35 | 'title' => '', |
| 36 | 'sku' => '', |
| 37 | 'manage_stock' => '', |
| 38 | 'backorders' => '', |
| 39 | 'stock_quantity' => '', |
| 40 | 'weight' => '', |
| 41 | 'length' => '', |
| 42 | 'width' => '', |
| 43 | 'height' => '', |
| 44 | 'tax_class' => '', |
| 45 | 'shipping_class_id' => '', |
| 46 | 'image_id' => '', |
| 47 | 'purchase_note' => '', |
| 48 | ); |
| 49 | |
| 50 | /** |
| 51 | * Override the default constructor to set custom defaults. |
| 52 | * |
| 53 | * @param int|WC_Product|object $product Product to init. |
| 54 | */ |
| 55 | public function __construct( $product = 0 ) { |
| 56 | $this->data['tax_class'] = 'parent'; |
| 57 | $this->data['attribute_summary'] = ''; |
| 58 | $this->data['cogs_value_is_additive'] = false; |
| 59 | parent::__construct( $product ); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Prefix for action and filter hooks on data. |
| 64 | * |
| 65 | * @since 3.0.0 |
| 66 | * @return string |
| 67 | */ |
| 68 | protected function get_hook_prefix() { |
| 69 | return 'woocommerce_product_variation_get_'; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Get internal type. |
| 74 | * |
| 75 | * @return string |
| 76 | */ |
| 77 | public function get_type() { |
| 78 | return ProductType::VARIATION; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * If the stock level comes from another product ID. |
| 83 | * |
| 84 | * @since 3.0.0 |
| 85 | * @return int |
| 86 | */ |
| 87 | public function get_stock_managed_by_id() { |
| 88 | return 'parent' === $this->get_manage_stock() ? $this->get_parent_id() : $this->get_id(); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Get the product's title. For variations this is the parent product name. |
| 93 | * |
| 94 | * @return string |
| 95 | */ |
| 96 | public function get_title() { |
| 97 | return apply_filters( 'woocommerce_product_title', $this->parent_data['title'], $this ); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Get product name with SKU or ID. Used within admin. |
| 102 | * |
| 103 | * @return string Formatted product name |
| 104 | */ |
| 105 | public function get_formatted_name() { |
| 106 | if ( $this->get_sku() ) { |
| 107 | $identifier = $this->get_sku(); |
| 108 | } else { |
| 109 | $identifier = '#' . $this->get_id(); |
| 110 | } |
| 111 | |
| 112 | $formatted_variation_list = wc_get_formatted_variation( $this, true, true, true ); |
| 113 | |
| 114 | return sprintf( '%2$s (%1$s)', $identifier, $this->get_name() ) . '<span class="description">' . $formatted_variation_list . '</span>'; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Get variation attribute values. Keys are prefixed with attribute_, as stored, unless $with_prefix is false. |
| 119 | * |
| 120 | * @param bool $with_prefix Whether keys should be prepended with attribute_ or not, default is true. |
| 121 | * @return array of attributes and their values for this variation. |
| 122 | */ |
| 123 | public function get_variation_attributes( $with_prefix = true ) { |
| 124 | $attributes = $this->get_attributes(); |
| 125 | $variation_attributes = array(); |
| 126 | $prefix = $with_prefix ? 'attribute_' : ''; |
| 127 | |
| 128 | foreach ( $attributes as $key => $value ) { |
| 129 | $variation_attributes[ $prefix . $key ] = $value; |
| 130 | } |
| 131 | return $variation_attributes; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Returns a single product attribute as a string. |
| 136 | * |
| 137 | * @param string $attribute to get. |
| 138 | * @return string |
| 139 | */ |
| 140 | public function get_attribute( $attribute ) { |
| 141 | $attributes = $this->get_attributes(); |
| 142 | $attribute = sanitize_title( $attribute ); |
| 143 | |
| 144 | if ( isset( $attributes[ $attribute ] ) ) { |
| 145 | $value = $attributes[ $attribute ]; |
| 146 | $term = taxonomy_exists( $attribute ) ? get_term_by( 'slug', $value, $attribute ) : false; |
| 147 | return ! is_wp_error( $term ) && $term ? $term->name : $value; |
| 148 | } |
| 149 | |
| 150 | $att_str = 'pa_' . $attribute; |
| 151 | if ( isset( $attributes[ $att_str ] ) ) { |
| 152 | $value = $attributes[ $att_str ]; |
| 153 | $term = taxonomy_exists( $att_str ) ? get_term_by( 'slug', $value, $att_str ) : false; |
| 154 | return ! is_wp_error( $term ) && $term ? $term->name : $value; |
| 155 | } |
| 156 | |
| 157 | return ''; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Wrapper for get_permalink. Adds this variations attributes to the URL. |
| 162 | * |
| 163 | * @param array|null $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. |
| 164 | * @return string |
| 165 | */ |
| 166 | public function get_permalink( $item_object = null ) { |
| 167 | $url = get_permalink( $this->get_parent_id() ); |
| 168 | |
| 169 | if ( ! empty( $item_object['item_meta_array'] ) ) { |
| 170 | $data_keys = array_map( 'wc_variation_attribute_name', wp_list_pluck( $item_object['item_meta_array'], 'key' ) ); |
| 171 | $data_values = wp_list_pluck( $item_object['item_meta_array'], 'value' ); |
| 172 | $data = array_intersect_key( array_combine( $data_keys, $data_values ), $this->get_variation_attributes() ); |
| 173 | } elseif ( ! empty( $item_object['variation'] ) && is_array( $item_object['variation'] ) ) { |
| 174 | $data = $item_object['variation']; |
| 175 | } else { |
| 176 | $data = $this->get_variation_attributes(); |
| 177 | } |
| 178 | |
| 179 | $data = array_filter( $data, 'wc_array_filter_default_attributes' ); |
| 180 | |
| 181 | if ( empty( $data ) ) { |
| 182 | return $url; |
| 183 | } |
| 184 | |
| 185 | // Filter and encode keys and values so this is not broken by add_query_arg. |
| 186 | $data = array_map( 'urlencode', $data ); |
| 187 | $keys = array_map( 'urlencode', array_keys( $data ) ); |
| 188 | |
| 189 | return add_query_arg( array_combine( $keys, $data ), $url ); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Get the add to url used mainly in loops. |
| 194 | * |
| 195 | * @return string |
| 196 | */ |
| 197 | public function add_to_cart_url() { |
| 198 | $url = $this->is_purchasable() ? remove_query_arg( |
| 199 | 'added-to-cart', |
| 200 | add_query_arg( |
| 201 | array( |
| 202 | 'variation_id' => $this->get_id(), |
| 203 | 'add-to-cart' => $this->get_parent_id(), |
| 204 | ), |
| 205 | $this->get_permalink() |
| 206 | ) |
| 207 | ) : $this->get_permalink(); |
| 208 | return apply_filters( 'woocommerce_product_add_to_cart_url', $url, $this ); |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Get SKU (Stock-keeping unit) - product unique ID. |
| 213 | * |
| 214 | * @param string $context What the value is for. Valid values are view and edit. |
| 215 | * @return string |
| 216 | */ |
| 217 | public function get_sku( $context = 'view' ) { |
| 218 | $value = $this->get_prop( 'sku', $context ); |
| 219 | |
| 220 | // Inherit value from parent. |
| 221 | if ( 'view' === $context && empty( $value ) ) { |
| 222 | $value = apply_filters( $this->get_hook_prefix() . 'sku', $this->parent_data['sku'], $this ); |
| 223 | } |
| 224 | return $value; |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Returns the product's weight. |
| 229 | * |
| 230 | * @param string $context What the value is for. Valid values are view and edit. |
| 231 | * @return string |
| 232 | */ |
| 233 | public function get_weight( $context = 'view' ) { |
| 234 | $value = $this->get_prop( 'weight', $context ); |
| 235 | |
| 236 | // Inherit value from parent. |
| 237 | if ( 'view' === $context && empty( $value ) ) { |
| 238 | $value = apply_filters( $this->get_hook_prefix() . 'weight', $this->parent_data['weight'], $this ); |
| 239 | } |
| 240 | return $value; |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Returns the product length. |
| 245 | * |
| 246 | * @param string $context What the value is for. Valid values are view and edit. |
| 247 | * @return string |
| 248 | */ |
| 249 | public function get_length( $context = 'view' ) { |
| 250 | $value = $this->get_prop( 'length', $context ); |
| 251 | |
| 252 | // Inherit value from parent. |
| 253 | if ( 'view' === $context && empty( $value ) ) { |
| 254 | $value = apply_filters( $this->get_hook_prefix() . 'length', $this->parent_data['length'], $this ); |
| 255 | } |
| 256 | return $value; |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Returns the product width. |
| 261 | * |
| 262 | * @param string $context What the value is for. Valid values are view and edit. |
| 263 | * @return string |
| 264 | */ |
| 265 | public function get_width( $context = 'view' ) { |
| 266 | $value = $this->get_prop( 'width', $context ); |
| 267 | |
| 268 | // Inherit value from parent. |
| 269 | if ( 'view' === $context && empty( $value ) ) { |
| 270 | $value = apply_filters( $this->get_hook_prefix() . 'width', $this->parent_data['width'], $this ); |
| 271 | } |
| 272 | return $value; |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Returns the product height. |
| 277 | * |
| 278 | * @param string $context What the value is for. Valid values are view and edit. |
| 279 | * @return string |
| 280 | */ |
| 281 | public function get_height( $context = 'view' ) { |
| 282 | $value = $this->get_prop( 'height', $context ); |
| 283 | |
| 284 | // Inherit value from parent. |
| 285 | if ( 'view' === $context && empty( $value ) ) { |
| 286 | $value = apply_filters( $this->get_hook_prefix() . 'height', $this->parent_data['height'], $this ); |
| 287 | } |
| 288 | return $value; |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Returns the tax class. |
| 293 | * |
| 294 | * Does not use get_prop so it can handle 'parent' inheritance correctly. |
| 295 | * |
| 296 | * @param string $context view, edit, or unfiltered. |
| 297 | * @return string |
| 298 | */ |
| 299 | public function get_tax_class( $context = 'view' ) { |
| 300 | $value = null; |
| 301 | |
| 302 | if ( array_key_exists( 'tax_class', $this->data ) ) { |
| 303 | $value = array_key_exists( 'tax_class', $this->changes ) ? $this->changes['tax_class'] : $this->data['tax_class']; |
| 304 | |
| 305 | if ( 'edit' !== $context && 'parent' === $value ) { |
| 306 | $value = $this->parent_data['tax_class']; |
| 307 | } |
| 308 | |
| 309 | if ( 'view' === $context ) { |
| 310 | $value = apply_filters( $this->get_hook_prefix() . 'tax_class', $value, $this ); |
| 311 | } |
| 312 | } |
| 313 | return $value; |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * Return if product manage stock. |
| 318 | * |
| 319 | * @since 3.0.0 |
| 320 | * @param string $context What the value is for. Valid values are view and edit. |
| 321 | * @return boolean|string true, false, or parent. |
| 322 | */ |
| 323 | public function get_manage_stock( $context = 'view' ) { |
| 324 | $value = $this->get_prop( 'manage_stock', $context ); |
| 325 | |
| 326 | // Inherit value from parent. |
| 327 | if ( 'view' === $context && false === $value && true === wc_string_to_bool( $this->parent_data['manage_stock'] ) ) { |
| 328 | $value = 'parent'; |
| 329 | } |
| 330 | return $value; |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Returns number of items available for sale. |
| 335 | * |
| 336 | * @param string $context What the value is for. Valid values are view and edit. |
| 337 | * @return int|null |
| 338 | */ |
| 339 | public function get_stock_quantity( $context = 'view' ) { |
| 340 | $value = $this->get_prop( 'stock_quantity', $context ); |
| 341 | |
| 342 | // Inherit value from parent. |
| 343 | if ( 'view' === $context && 'parent' === $this->get_manage_stock() ) { |
| 344 | $value = apply_filters( $this->get_hook_prefix() . 'stock_quantity', $this->parent_data['stock_quantity'], $this ); |
| 345 | } |
| 346 | return $value; |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * Get backorders. |
| 351 | * |
| 352 | * @param string $context What the value is for. Valid values are view and edit. |
| 353 | * @since 3.0.0 |
| 354 | * @return string yes no or notify |
| 355 | */ |
| 356 | public function get_backorders( $context = 'view' ) { |
| 357 | $value = $this->get_prop( 'backorders', $context ); |
| 358 | |
| 359 | // Inherit value from parent. |
| 360 | if ( 'view' === $context && 'parent' === $this->get_manage_stock() ) { |
| 361 | $value = apply_filters( $this->get_hook_prefix() . 'backorders', $this->parent_data['backorders'], $this ); |
| 362 | } |
| 363 | return $value; |
| 364 | } |
| 365 | |
| 366 | /** |
| 367 | * Get main image ID. |
| 368 | * |
| 369 | * @since 3.0.0 |
| 370 | * @param string $context What the value is for. Valid values are view and edit. |
| 371 | * @return string |
| 372 | */ |
| 373 | public function get_image_id( $context = 'view' ) { |
| 374 | $image_id = $this->get_prop( 'image_id', $context ); |
| 375 | |
| 376 | if ( 'view' === $context && ! $image_id ) { |
| 377 | $image_id = apply_filters( $this->get_hook_prefix() . 'image_id', $this->parent_data['image_id'], $this ); |
| 378 | } |
| 379 | |
| 380 | return $image_id; |
| 381 | } |
| 382 | |
| 383 | /** |
| 384 | * Get purchase note. |
| 385 | * |
| 386 | * @since 3.0.0 |
| 387 | * @param string $context What the value is for. Valid values are view and edit. |
| 388 | * @return string |
| 389 | */ |
| 390 | public function get_purchase_note( $context = 'view' ) { |
| 391 | $value = $this->get_prop( 'purchase_note', $context ); |
| 392 | |
| 393 | // Inherit value from parent. |
| 394 | if ( 'view' === $context && empty( $value ) ) { |
| 395 | $value = apply_filters( $this->get_hook_prefix() . 'purchase_note', $this->parent_data['purchase_note'], $this ); |
| 396 | } |
| 397 | return $value; |
| 398 | } |
| 399 | |
| 400 | /** |
| 401 | * Get shipping class ID. |
| 402 | * |
| 403 | * @since 3.0.0 |
| 404 | * @param string $context What the value is for. Valid values are view and edit. |
| 405 | * @return int |
| 406 | */ |
| 407 | public function get_shipping_class_id( $context = 'view' ) { |
| 408 | $shipping_class_id = $this->get_prop( 'shipping_class_id', $context ); |
| 409 | |
| 410 | if ( 'view' === $context && ! $shipping_class_id ) { |
| 411 | $shipping_class_id = apply_filters( $this->get_hook_prefix() . 'shipping_class_id', $this->parent_data['shipping_class_id'], $this ); |
| 412 | } |
| 413 | |
| 414 | return $shipping_class_id; |
| 415 | } |
| 416 | |
| 417 | /** |
| 418 | * Get catalog visibility. |
| 419 | * |
| 420 | * @param string $context What the value is for. Valid values are view and edit. |
| 421 | * @return string |
| 422 | */ |
| 423 | public function get_catalog_visibility( $context = 'view' ) { |
| 424 | return apply_filters( $this->get_hook_prefix() . 'catalog_visibility', $this->parent_data['catalog_visibility'], $this ); |
| 425 | } |
| 426 | |
| 427 | /** |
| 428 | * Get attribute summary. |
| 429 | * |
| 430 | * By default, attribute summary contains comma-delimited 'attribute_name: attribute_value' pairs for all attributes. |
| 431 | * |
| 432 | * @param string $context What the value is for. Valid values are view and edit. |
| 433 | * |
| 434 | * @since 3.6.0 |
| 435 | * @return string |
| 436 | */ |
| 437 | public function get_attribute_summary( $context = 'view' ) { |
| 438 | return $this->get_prop( 'attribute_summary', $context ); |
| 439 | } |
| 440 | |
| 441 | |
| 442 | /** |
| 443 | * Set attribute summary. |
| 444 | * |
| 445 | * By default, attribute summary contains comma-delimited 'attribute_name: attribute_value' pairs for all attributes. |
| 446 | * |
| 447 | * @since 3.6.0 |
| 448 | * @param string $attribute_summary Summary of attribute names and values assigned to the variation. |
| 449 | */ |
| 450 | public function set_attribute_summary( $attribute_summary ) { |
| 451 | $this->set_prop( 'attribute_summary', $attribute_summary ); |
| 452 | } |
| 453 | |
| 454 | /* |
| 455 | |-------------------------------------------------------------------------- |
| 456 | | CRUD methods |
| 457 | |-------------------------------------------------------------------------- |
| 458 | */ |
| 459 | |
| 460 | /** |
| 461 | * Set the parent data array for this variation. |
| 462 | * |
| 463 | * @since 3.0.0 |
| 464 | * @param array $parent_data parent data array for this variation. |
| 465 | */ |
| 466 | public function set_parent_data( $parent_data ) { |
| 467 | $parent_data = wp_parse_args( |
| 468 | $parent_data, |
| 469 | array( |
| 470 | 'title' => '', |
| 471 | 'status' => '', |
| 472 | 'sku' => '', |
| 473 | 'manage_stock' => 'no', |
| 474 | 'backorders' => 'no', |
| 475 | 'stock_quantity' => '', |
| 476 | 'weight' => '', |
| 477 | 'length' => '', |
| 478 | 'width' => '', |
| 479 | 'height' => '', |
| 480 | 'tax_class' => '', |
| 481 | 'shipping_class_id' => 0, |
| 482 | 'image_id' => 0, |
| 483 | 'purchase_note' => '', |
| 484 | 'catalog_visibility' => CatalogVisibility::VISIBLE, |
| 485 | ) |
| 486 | ); |
| 487 | |
| 488 | // Normalize tax class. |
| 489 | $parent_data['tax_class'] = sanitize_title( $parent_data['tax_class'] ); |
| 490 | $parent_data['tax_class'] = 'standard' === $parent_data['tax_class'] ? '' : $parent_data['tax_class']; |
| 491 | $valid_classes = $this->get_valid_tax_classes(); |
| 492 | |
| 493 | if ( ! in_array( $parent_data['tax_class'], $valid_classes, true ) ) { |
| 494 | $parent_data['tax_class'] = ''; |
| 495 | } |
| 496 | |
| 497 | $this->parent_data = $parent_data; |
| 498 | } |
| 499 | |
| 500 | /** |
| 501 | * Get the parent data array for this variation. |
| 502 | * |
| 503 | * @since 3.0.0 |
| 504 | * @return array |
| 505 | */ |
| 506 | public function get_parent_data() { |
| 507 | return $this->parent_data; |
| 508 | } |
| 509 | |
| 510 | /** |
| 511 | * Set attributes. Unlike the parent product which uses terms, variations are assigned |
| 512 | * specific attributes using name value pairs. |
| 513 | * |
| 514 | * @param array $raw_attributes array of raw attributes. |
| 515 | */ |
| 516 | public function set_attributes( $raw_attributes ) { |
| 517 | $raw_attributes = (array) $raw_attributes; |
| 518 | $attributes = array(); |
| 519 | |
| 520 | foreach ( $raw_attributes as $key => $value ) { |
| 521 | // Remove attribute prefix which meta gets stored with. |
| 522 | if ( 0 === strpos( $key, 'attribute_' ) ) { |
| 523 | $key = substr( $key, 10 ); |
| 524 | } |
| 525 | $attributes[ $key ] = $value; |
| 526 | } |
| 527 | $this->set_prop( 'attributes', $attributes ); |
| 528 | } |
| 529 | |
| 530 | /** |
| 531 | * Returns whether or not the product has any visible attributes. |
| 532 | * |
| 533 | * Variations are mapped to specific attributes unlike products, and the return |
| 534 | * value of ->get_attributes differs. Therefore this returns false. |
| 535 | * |
| 536 | * @return boolean |
| 537 | */ |
| 538 | public function has_attributes() { |
| 539 | return false; |
| 540 | } |
| 541 | |
| 542 | /* |
| 543 | |-------------------------------------------------------------------------- |
| 544 | | Conditionals |
| 545 | |-------------------------------------------------------------------------- |
| 546 | */ |
| 547 | |
| 548 | /** |
| 549 | * Returns false if the product cannot be bought. |
| 550 | * 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. |
| 551 | * |
| 552 | * @return bool |
| 553 | */ |
| 554 | public function is_purchasable() { |
| 555 | /** |
| 556 | * Filter to adjust if a variation is purchasable. |
| 557 | * |
| 558 | * @since 3.0.0 |
| 559 | * @param bool $purchasable If the variation is purchasable. |
| 560 | * @param object $variation The variation object. |
| 561 | */ |
| 562 | return apply_filters( 'woocommerce_variation_is_purchasable', $this->variation_is_visible() && parent::is_purchasable() && ( ProductStatus::PUBLISH === $this->parent_data['status'] || current_user_can( 'edit_post', $this->get_parent_id() ) ), $this ); |
| 563 | } |
| 564 | |
| 565 | /** |
| 566 | * Controls whether this particular variation will appear greyed-out (inactive) or not (active). |
| 567 | * Used by extensions to make incompatible variations appear greyed-out, etc. |
| 568 | * Other possible uses: prevent out-of-stock variations from being selected. |
| 569 | * |
| 570 | * @return bool |
| 571 | */ |
| 572 | public function variation_is_active() { |
| 573 | return apply_filters( 'woocommerce_variation_is_active', true, $this ); |
| 574 | } |
| 575 | |
| 576 | /** |
| 577 | * Checks if this particular variation is visible. Invisible variations are enabled and can be selected, but no price / stock info is displayed. |
| 578 | * Instead, a suitable 'unavailable' message is displayed. |
| 579 | * Invisible by default: Disabled variations and variations with an empty price. |
| 580 | * |
| 581 | * @return bool |
| 582 | */ |
| 583 | public function variation_is_visible() { |
| 584 | /** |
| 585 | * Filter to adjust if a variation is visible. |
| 586 | * |
| 587 | * @since 3.0.0 |
| 588 | * @param bool $visible If the variation is visible. |
| 589 | * @param int $variation_id The variation ID. |
| 590 | * @param int $product_id The product ID. |
| 591 | * @param object $variation The variation object. |
| 592 | */ |
| 593 | return apply_filters( 'woocommerce_variation_is_visible', ProductStatus::PUBLISH === get_post_status( $this->get_id() ) && '' !== $this->get_price(), $this->get_id(), $this->get_parent_id(), $this ); |
| 594 | } |
| 595 | |
| 596 | /** |
| 597 | * Return valid tax classes. Adds 'parent' to the default list of valid tax classes. |
| 598 | * |
| 599 | * @return array valid tax classes |
| 600 | */ |
| 601 | protected function get_valid_tax_classes() { |
| 602 | $valid_classes = WC_Tax::get_tax_class_slugs(); |
| 603 | $valid_classes[] = 'parent'; |
| 604 | |
| 605 | return $valid_classes; |
| 606 | } |
| 607 | |
| 608 | /** |
| 609 | * Get the value of the "Cost of Goods Sold value is additive" flag for this product. |
| 610 | * See get_cogs_effective_value_core. |
| 611 | * |
| 612 | * @return bool The current value of the flag. |
| 613 | */ |
| 614 | public function get_cogs_value_is_additive(): bool { |
| 615 | return (bool) $this->get_prop( 'cogs_value_is_additive' ); |
| 616 | } |
| 617 | |
| 618 | /** |
| 619 | * Set the value of the "Cost of Goods Sold value is additive" flag for this product. |
| 620 | * See get_cogs_effective_value_core. |
| 621 | * |
| 622 | * WARNING! If the Cost of Goods Sold feature is disabled this value will NOT be persisted when the product is saved. |
| 623 | * |
| 624 | * @param bool $value The value to set for the flag. |
| 625 | */ |
| 626 | public function set_cogs_value_is_additive( bool $value ): void { |
| 627 | $this->set_prop( 'cogs_value_is_additive', $value ); |
| 628 | } |
| 629 | |
| 630 | /** |
| 631 | * Replacement of the parent adjust_cogs_value_before_set method |
| 632 | * to disable the conversion of zero to null. |
| 633 | * |
| 634 | * @param float|null $value Cost value passed to the set_cogs_value method. |
| 635 | * @return float|null The actual value that will be set for the cost property. |
| 636 | */ |
| 637 | protected function adjust_cogs_value_before_set( ?float $value ): ?float { |
| 638 | return $value; |
| 639 | } |
| 640 | |
| 641 | /** |
| 642 | * Get the effective total value of the Cost of Goods Sold for this product. |
| 643 | * (the monetary value that will be applied to orders and used for analytics purposes). |
| 644 | * |
| 645 | * If "additive" flag is set, the total value is equal to sum of the effective values of the variation and the parent product. |
| 646 | * Otherwise, if the defined value for this variation is null, the effective value is equal to the effective value of the parent product. |
| 647 | * Otherwise, the effective value is equal to the effective value of the variation. |
| 648 | * |
| 649 | * @return float |
| 650 | */ |
| 651 | protected function get_cogs_total_value_core(): float { |
| 652 | if ( $this->get_cogs_value_is_additive() ) { |
| 653 | return $this->get_parent_cogs_effective_value() + $this->get_cogs_effective_value(); |
| 654 | } else { |
| 655 | return is_null( $this->get_cogs_value() ) ? $this->get_parent_cogs_effective_value() : $this->get_cogs_effective_value(); |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | /** |
| 660 | * Get the Cost of Goods Sold effective value of the parent product. |
| 661 | * |
| 662 | * @return float Cost of Goods Sold effective value of the parent product. |
| 663 | */ |
| 664 | public function get_parent_cogs_effective_value(): float { |
| 665 | $parent_cogs = $this->parent_data['cogs_effective_value'] ?? null; |
| 666 | if ( is_null( $parent_cogs ) ) { |
| 667 | $parent_product = wc_get_product( $this->get_parent_id() ); |
| 668 | $parent_cogs = $parent_product ? $parent_product->get_cogs_effective_value() : 0; |
| 669 | $this->parent_data['cogs_effective_value'] = $parent_cogs; |
| 670 | } |
| 671 | return $parent_cogs; |
| 672 | } |
| 673 | } |
| 674 |