Skip to content

Instantly share code, notes, and snippets.

@joedooley
Created December 10, 2016 00:28
Show Gist options
  • Select an option

  • Save joedooley/43dc50adab4c39d140f0d7cd86524078 to your computer and use it in GitHub Desktop.

Select an option

Save joedooley/43dc50adab4c39d140f0d7cd86524078 to your computer and use it in GitHub Desktop.
FacetWP - variable products test
<?php
// Example - check for "in_stock" with a "Size" attribute
function show_instock_product_variations( $params, $class ) {
if ( 'tax/pa_size' == $params['facet_source'] && 'product' == get_post_type( $params['post_id'] ) ) {
WC()->api->includes();
WC()->api->register_resources( new WC_API_Server( '/' ) );
$response = WC()->api->WC_API_Products->get_product( $params['post_id'] );
$product = $response['product'];
if ( 'variable' == $product['type'] ) { // variable product
foreach ( $product['variations'] as $variation ) { // loop through each variation
$attr = $variation['attributes'][0];
if ( 'tax/pa_' . $attr['slug'] == $params['facet_source'] ) { // correct variation
if ( $attr['option'] == $params['facet_value'] ) { // correct value
if ( true === $variation['in_stock'] ) { // in stock
return $params;
}
break;
}
}
}
return false;
}
elseif ( ! $product['in_stock'] ) { // simple product (out of stock)
return false;
}
}
return $params; // default indexer
}
add_filter( 'facetwp_index_row', 'show_instock_product_variations', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment