From a4f87293caec486fd21c90a2ff1a892a478462a5 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Tue, 7 Feb 2017 12:35:24 +0000 Subject: [PATCH] Inherit shipping class id from parent --- includes/class-wc-product-variation.php | 17 +++++++++++++ .../data-stores/class-wc-data-store-wp.php | 9 +++++-- ...ss-wc-product-variation-data-store-cpt.php | 25 ++++++++++--------- 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/includes/class-wc-product-variation.php b/includes/class-wc-product-variation.php index 99fec657b14..55b1da94923 100644 --- a/includes/class-wc-product-variation.php +++ b/includes/class-wc-product-variation.php @@ -283,6 +283,23 @@ class WC_Product_Variation extends WC_Product_Simple { return $image_id; } + /** + * Get shipping class ID. + * + * @since 2.7.0 + * @param string $context + * @return int + */ + public function get_shipping_class_id( $context = 'view' ) { + $shipping_class_id = $this->get_prop( 'shipping_class_id', $context ); + + if ( 'view' === $context && ! $shipping_class_id ) { + $shipping_class_id = $this->parent_data['shipping_class_id']; + } + + return $shipping_class_id; + } + /* |-------------------------------------------------------------------------- | CRUD methods diff --git a/includes/data-stores/class-wc-data-store-wp.php b/includes/data-stores/class-wc-data-store-wp.php index 2944f294561..dad6c37db19 100644 --- a/includes/data-stores/class-wc-data-store-wp.php +++ b/includes/data-stores/class-wc-data-store-wp.php @@ -41,12 +41,17 @@ class WC_Data_Store_WP { * Get and store terms from a taxonomy. * * @since 2.7.0 - * @param WC_Data + * @param WC_Data|integer $object * @param string $taxonomy Taxonomy name e.g. product_cat * @return array of terms */ protected function get_term_ids( $object, $taxonomy ) { - $terms = get_the_terms( $object->get_id(), $taxonomy ); + if ( is_numeric( $object ) ) { + $object_id = $object; + } else { + $object_id = $object->get_id(); + } + $terms = get_the_terms( $object_id, $taxonomy ); if ( false === $terms || is_wp_error( $terms ) ) { return array(); } diff --git a/includes/data-stores/class-wc-product-variation-data-store-cpt.php b/includes/data-stores/class-wc-product-variation-data-store-cpt.php index e2faa328fae..19363473511 100644 --- a/includes/data-stores/class-wc-product-variation-data-store-cpt.php +++ b/includes/data-stores/class-wc-product-variation-data-store-cpt.php @@ -191,7 +191,7 @@ class WC_Product_Variation_Data_Store_CPT extends WC_Product_Data_Store_CPT impl 'tax_status' => get_post_meta( $id, '_tax_status', true ), 'manage_stock' => get_post_meta( $id, '_manage_stock', true ), 'stock_status' => get_post_meta( $id, '_stock_status', true ), - 'shipping_class_id' => current( $this->get_term_ids( $product, 'product_shipping_class' ) ), + 'shipping_class_id' => current( $this->get_term_ids( $id, 'product_shipping_class' ) ), 'virtual' => get_post_meta( $id, '_virtual', true ), 'downloadable' => get_post_meta( $id, '_downloadable', true ), 'downloads' => array_filter( (array) get_post_meta( $id, '_downloadable_files', true ) ), @@ -216,17 +216,18 @@ class WC_Product_Variation_Data_Store_CPT extends WC_Product_Data_Store_CPT impl } $product->set_parent_data( array( - 'title' => get_the_title( $product->get_parent_id() ), - 'sku' => get_post_meta( $product->get_parent_id(), '_sku', true ), - 'manage_stock' => get_post_meta( $product->get_parent_id(), '_manage_stock', true ), - 'backorders' => get_post_meta( $product->get_parent_id(), '_backorders', true ), - 'stock_quantity' => get_post_meta( $product->get_parent_id(), '_stock', true ), - 'weight' => get_post_meta( $product->get_parent_id(), '_weight', true ), - 'length' => get_post_meta( $product->get_parent_id(), '_length', true ), - 'width' => get_post_meta( $product->get_parent_id(), '_width', true ), - 'height' => get_post_meta( $product->get_parent_id(), '_height', true ), - 'tax_class' => get_post_meta( $product->get_parent_id(), '_tax_class', true ), - 'image_id' => get_post_thumbnail_id( $product->get_parent_id() ), + 'title' => get_the_title( $product->get_parent_id() ), + 'sku' => get_post_meta( $product->get_parent_id(), '_sku', true ), + 'manage_stock' => get_post_meta( $product->get_parent_id(), '_manage_stock', true ), + 'backorders' => get_post_meta( $product->get_parent_id(), '_backorders', true ), + 'stock_quantity' => get_post_meta( $product->get_parent_id(), '_stock', true ), + 'weight' => get_post_meta( $product->get_parent_id(), '_weight', true ), + 'length' => get_post_meta( $product->get_parent_id(), '_length', true ), + 'width' => get_post_meta( $product->get_parent_id(), '_width', true ), + 'height' => get_post_meta( $product->get_parent_id(), '_height', true ), + 'tax_class' => get_post_meta( $product->get_parent_id(), '_tax_class', true ), + 'shipping_class_id' => absint( current( $this->get_term_ids( $product->get_parent_id(), 'product_shipping_class' ) ) ), + 'image_id' => get_post_thumbnail_id( $product->get_parent_id() ), ) ); }