From fa0e5569f8b4260bb766b489bec4d3844d2d75a7 Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Mon, 9 Oct 2017 16:20:03 -0300 Subject: [PATCH] Add 'parent' to the list of valid tax classes for product variations Commit d9f9e74bd added a check to `WC_Product::set_tax_class()` to only accept valid tax classes, but this created a bug for product variations as this type of product has an extra tax class called 'parent'. This commit fixes this problem by adding a new method to `WC_Product` that returns a list of valid tax classes. `WC_Product_Variation` then override this method, returning another list including the tax class 'parent'. Fixes #17024 --- includes/abstracts/abstract-wc-product.php | 11 ++++++++++- includes/class-wc-product-variation.php | 12 ++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/includes/abstracts/abstract-wc-product.php b/includes/abstracts/abstract-wc-product.php index bb0feaa3e25..62f1545a270 100644 --- a/includes/abstracts/abstract-wc-product.php +++ b/includes/abstracts/abstract-wc-product.php @@ -902,7 +902,7 @@ class WC_Product extends WC_Abstract_Legacy_Product { public function set_tax_class( $class ) { $class = sanitize_title( $class ); $class = 'standard' === $class ? '' : $class; - $valid_classes = WC_Tax::get_tax_class_slugs(); + $valid_classes = $this->get_valid_tax_classes(); if ( ! in_array( $class, $valid_classes ) ) { $class = ''; @@ -911,6 +911,15 @@ class WC_Product extends WC_Abstract_Legacy_Product { $this->set_prop( 'tax_class', $class ); } + /** + * Return an array of valid tax classes + * + * @return array valid tax classes + */ + protected function get_valid_tax_classes() { + return WC_Tax::get_tax_class_slugs(); + } + /** * Set if product manage stock. * diff --git a/includes/class-wc-product-variation.php b/includes/class-wc-product-variation.php index 4991c24bdcb..5de955ef312 100644 --- a/includes/class-wc-product-variation.php +++ b/includes/class-wc-product-variation.php @@ -487,4 +487,16 @@ class WC_Product_Variation extends WC_Product_Simple { public function variation_is_visible() { 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 ); } + + /** + * Return valid tax classes. Adds 'parent' to the default list of valid tax classes. + * + * @return array valid tax classes + */ + protected function get_valid_tax_classes() { + $valid_classes = WC_Tax::get_tax_class_slugs(); + $valid_classes[] = 'parent'; + + return $valid_classes; + } }