Inherit shipping class id from parent

This commit is contained in:
Mike Jolley 2017-02-07 12:35:24 +00:00
parent 89fb5de19d
commit a4f87293ca
3 changed files with 37 additions and 14 deletions

View File

@ -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

View File

@ -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();
}

View File

@ -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() ),
) );
}