Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't sync invisible products to MC #880

Merged
merged 12 commits into from
Jul 20, 2021
2 changes: 1 addition & 1 deletion src/Admin/MetaBox/ChannelVisibilityMetaBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ protected function get_view_context( WP_Post $post, array $args ): array {
'field_id' => $this->get_visibility_field_id(),
'product_id' => $product_id,
'product' => $product,
'visibility' => $this->product_helper->get_visibility( $product ),
'visibility' => $this->product_helper->get_channel_visibility( $product ),
'sync_status' => $this->meta_handler->get_sync_status( $product ),
'issues' => $this->product_helper->get_validation_errors( $product ),
];
Expand Down
2 changes: 1 addition & 1 deletion src/DB/ProductFeedQueryHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function get( WP_REST_Request $request ): array {
$products[ $id ] = [
'id' => $id,
'title' => $product->get_name(),
'visible' => $product_helper->get_visibility( $product ) !== ChannelVisibility::DONT_SYNC_AND_SHOW,
'visible' => $product_helper->get_channel_visibility( $product ) !== ChannelVisibility::DONT_SYNC_AND_SHOW,
'status' => $product_helper->get_mc_status( $product ) ?: $product_helper->get_sync_status( $product ),
'errors' => array_values( $errors ),
];
Expand Down
21 changes: 17 additions & 4 deletions src/Product/ProductHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,16 +276,29 @@ public function is_product_synced( WC_Product $product ): bool {
* @return bool
*/
public function is_sync_ready( WC_Product $product ): bool {
$product_status = $product->get_status();
$hide_out_of_stock = 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' );
$product_visibility = ! $hide_out_of_stock || $product->is_in_stock();
$product_status = $product->get_status();

if ( $product instanceof WC_Product_Variation && ! empty( $product->get_parent_id() ) ) {
// Check the post status of the parent product if it's a variation
$parent = $this->get_wc_product( $product->get_parent_id() );
$product_status = $parent->get_status();

/**
* Optionally hide invisible variations (disabled variations and variations with empty price).
*
* @see WC_Product_Variable::get_available_variations for filter documentation
*/
if ( apply_filters( 'woocommerce_hide_invisible_variations', true, $parent->get_id(), $product ) && ! $product->variation_is_visible() ) {
$product_visibility = false;
}
}

return ( ChannelVisibility::DONT_SYNC_AND_SHOW !== $this->get_visibility( $product ) ) &&
return ( ChannelVisibility::DONT_SYNC_AND_SHOW !== $this->get_channel_visibility( $product ) ) &&
( in_array( $product->get_type(), ProductSyncer::get_supported_product_types(), true ) ) &&
( 'publish' === $product_status );
( 'publish' === $product_status ) &&
$product_visibility;
}

/**
Expand All @@ -312,7 +325,7 @@ public function is_sync_failed_recently( WC_Product $product ): bool {
*
* @return string|null
*/
public function get_visibility( WC_Product $wc_product ): ?string {
public function get_channel_visibility( WC_Product $wc_product ): ?string {
$visibility = $this->meta_handler->get_visibility( $wc_product );
if ( $wc_product instanceof WC_Product_Variation ) {
// todo: we might need to define visibility per variation later.
Expand Down
Loading