Skip to content

Instantly share code, notes, and snippets.

@Bobz-zg
Last active September 19, 2018 18:11
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 Bobz-zg/81bb91a84858655d66f40ec88c2ba0b7 to your computer and use it in GitHub Desktop.
Save Bobz-zg/81bb91a84858655d66f40ec88c2ba0b7 to your computer and use it in GitHub Desktop.
Filter videos products from product search JSON in wp-admin 'Woocommerce orders' view
<?php if ( ! defined( 'ABSPATH' ) ) exit;
/**
* Filter Streaming videos products from JSON search in wp-admin 'Woocommerce orders' view
*/
add_filter( 'woocommerce_json_search_found_products', function( $products ) {
/**
* Check HTTP Referer
*/
$referer = isset($_SERVER['HTTP_REFERER']) ? sanitize_text_field( $_SERVER['HTTP_REFERER'] ) : NULL;
$query_str = parse_url($referer, PHP_URL_QUERY);
parse_str($query_str, $query_params);
/**
* Get post_type or post_id from Referer URL
*/
$post_id = isset( $query_params['post'] ) ? intval( $query_params['post'] ) : NULL;
$type = isset( $query_params['post_type'] ) ? sanitize_text_field( $query_params['post_type'] ) : NULL;
/**
* Return search results if
* - 'post' query var is not found in query string, should be when editing existing order
* - 'post_type' is not shop_order, should be when creating new order
*/
if ( ! $post_id && 'shop_order' != $type )
return $products;
/**
* Just to double check user is actually on edit order screen
*/
if ( ! $type && $post_id ) {
$order = get_post($post_id);
$type = isset( $order['post_type'] ) ? $order['post_type'] : NULL;
}
if ( 'shop_order' != $type )
return $products;
/**
* Filter all products that are product type: streaming, variable
*/
foreach ($products as $id => $title) :
// Skip everything that is not 'product' post_type
if( 'product' != get_post_type( $id ) ) continue;
// Filter by type from $products array
if ( in_array( WC_Product_Factory::get_product_type($id), ['variable'] ) )
unset( $products[$id] );
endforeach;
return $products;
}, 10, 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment