Merge pull request #15477 from woocommerce/fix/15392

Variations to inherit catalog_visibility
This commit is contained in:
Claudio Sanches 2017-06-06 14:27:46 -03:00 committed by GitHub
commit 20d5e0f769
2 changed files with 40 additions and 14 deletions

View File

@ -354,6 +354,16 @@ class WC_Product_Variation extends WC_Product_Simple {
return $shipping_class_id;
}
/**
* Get catalog visibility.
*
* @param string $context
* @return string
*/
public function get_catalog_visibility( $context = 'view' ) {
return apply_filters( $this->get_hook_prefix() . 'catalog_visibility', $this->parent_data['catalog_visibility'], $this );
}
/*
|--------------------------------------------------------------------------
| CRUD methods

View File

@ -285,21 +285,37 @@ class WC_Product_Variation_Data_Store_CPT extends WC_Product_Data_Store_CPT impl
$product->set_price( $product->get_regular_price( 'edit' ) );
}
$parent_object = get_post( $product->get_parent_id() );
$parent_object = get_post( $product->get_parent_id() );
$terms = get_the_terms( $product->get_parent_id(), 'product_visibility' );
$term_names = is_array( $terms ) ? wp_list_pluck( $terms, 'name' ) : array();
$exclude_search = in_array( 'exclude-from-search', $term_names );
$exclude_catalog = in_array( 'exclude-from-catalog', $term_names );
if ( $exclude_search && $exclude_catalog ) {
$catalog_visibility = 'hidden';
} elseif ( $exclude_search ) {
$catalog_visibility = 'catalog';
} elseif ( $exclude_catalog ) {
$catalog_visibility = 'search';
} else {
$catalog_visibility = 'visible';
}
$product->set_parent_data( array(
'title' => $parent_object->post_title,
'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' => wc_stock_amount( 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() ),
'purchase_note' => get_post_meta( $product->get_parent_id(), '_purchase_note', true ),
'title' => $parent_object->post_title,
'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' => wc_stock_amount( 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() ),
'purchase_note' => get_post_meta( $product->get_parent_id(), '_purchase_note', true ),
'catalog_visibility' => $catalog_visibility,
) );
// Pull data from the parent when there is no user-facing way to set props.