Skip to content

Instantly share code, notes, and snippets.

@webtoffee-git
Last active September 8, 2021 11:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webtoffee-git/955c27c49a009746bd1ec537e007b718 to your computer and use it in GitHub Desktop.
Save webtoffee-git/955c27c49a009746bd1ec537e007b718 to your computer and use it in GitHub Desktop.
To exclude variations from sync - Product Catalog Sync for WooCommerce(https://www.webtoffee.com/product/product-catalog-sync-for-facebook/)
add_filter( 'wt_facebook_sync_products', 'modify_wt_facebook_sync_products', 10, 1 );
function modify_wt_facebook_sync_products( $products ) {
// modify the below example array $attr_taxonomies_term_slugs
// Specify the array key as 'product attribute' and array elements as 'term slugs'
$attr_taxonomies_term_slugs = array (
'pa_color' => array( 'blue', 'green', 'gray' ),
'collar_type' => array( 'u-neck', 'v-neck', 'round' )
);
$exclude_products = wt_get_variable_products_to_exclude($products, $attr_taxonomies_term_slugs);
$sync_products = array_diff( $products, $exclude_products );
return $sync_products;
}
function wt_get_variable_products_to_exclude($products, $attr_list = array() ) {
if( empty($attr_list) )
return array();
$meta_query_array = array('relation' => 'OR');
foreach ( $attr_list as $attr_taxonomy => $attribute_term_slugs ) {
$meta_query_array[] = array (
'key' => 'attribute_' . $attr_taxonomy,
'value' => $attribute_term_slugs,
'compare' => 'IN'
);
}
$products = new WP_Query (
array (
'post_type' => 'product_variation',
'post_status' => 'publish',
'posts_per_page' => -1,
'post__in' => $products,
'meta_query' => $meta_query_array,
'fields' => 'ids',
)
);
if( $products->have_posts() ) {
return $products->posts;
}
return array();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment