Created
December 10, 2016 00:28
-
-
Save joedooley/43dc50adab4c39d140f0d7cd86524078 to your computer and use it in GitHub Desktop.
FacetWP - variable products test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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