Get all products with specific meta_key whose meta_value is equal in WooCommerce

Billy

In single product page i want to display the stock quantity of all products that have the same value in a custom field (base_number) and i assume that this would be possible by taking the ids of these products.

What i've tried without any luck

$product = wc_get_product( $product_id );

$same_base = get_posts( array(
    'post_type' => 'product',
    'post_status' => 'publish',
    'meta_key' => 'base_number',
    'meta_value' => get_post_meta(get_the_ID(), 'base_number', TRUE),
    'fields' => 'ids',
));

return $same_base;

Any help would be much appreciated.

7uc1f3r

Use this to get all products where meta_key = base_number

// Get WC_Product Objects
$products = wc_get_products( array(
    'limit'         => -1,
    'status'        => 'publish',
    'meta_key'      => 'base_number',
));

// Loop through
foreach( $products as $product ) {
    // Get product ID
    $product_id = $product->get_id();
    echo '<p>Product ID: ' . $product_id . '</p>';
    
    // Get stock quantity
    $stock_quantity = $product->get_stock_quantity();
    echo '<p>Stock quantity: ' . $stock_quantity . '</p>';
}

Use this to get all products where meta_key = base_number and let's say, meta_value = 30

// Get WC_Product Objects
$products = wc_get_products( array(
    'limit'         => -1,
    'status'        => 'publish',
    'meta_key'      => 'base_number',
    'meta_value'    => 30
));

// Loop through
foreach( $products as $product ) {
    // Get product ID
    $product_id = $product->get_id();
    echo '<p>Product ID: ' . $product_id . '</p>';
    
    // Get stock quantity
    $stock_quantity = $product->get_stock_quantity();
    echo '<p>Stock quantity: ' . $stock_quantity . '</p>';
}

Use this to get all product IDs where meta_key = base_number and meta_value = $some_variable

// Some variable
$some_variable = 25;

// Get product ID's
$product_ids = wc_get_products( array(
    'limit'         => -1,
    'status'        => 'publish',
    'meta_key'      => 'base_number',
    'meta_value'    => $some_variable,
    'return'        => 'ids'
));

// Loop through
foreach( $product_ids as $product_id ) {
    echo '<p>Product ID: ' . $product_id . '</p>';
}

And to get the base_number of the product you are currently viewing, use

// Get the global product object
global $product;

// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
    // Get meta
    $base_number = $product->get_meta( 'base_number' );
    
    // NOT empty
    if ( ! empty ( $base_number ) ) {
        echo '<p>Base number: ' . $base_number . '</p>';
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Get all WooCommerce products from a category that have specific meta data

WooCommerce sql query to find products with a specific meta key and meta value

Sort Woocommerce Products from a product category and custom meta_key

Update product price if specific meta_key contains a specific meta_value

Sql query to exclude meta_value where meta_key = x on SUM for WooCommerce

WP SQL add meta_key with an empty meta_value to all users

Woocommerce: How to update meta attribute value of all products programmatically

Filter by custom field 'meta_key', 'meta_value' for archive

Construct MySQL query ( meta_key/meta_value table)

Update meta_value for specific word

Exclude specific products from everywhere with a meta query in Woocommerce

Filter products from a specific custom meta data in Woocommerce shop page

Show products by high int meta_key valve

How to select all meta values of certain meta_key's?

Get Meta_Value from WP database

Looking for query to get meta_value

Get specific value from a post meta array variable value in WooCommerce

how to get key and value of object whose values are not equal to null?

Getting 'meta_value' when searching for matching 'post_id' and 'meta_key', 'meta_value' combo

Mysql delete all posts that have a given meta_key

Specific SQL query (multiple meta_value / same row)

WooCommerce get products based on specific taxonomy

Wordpress, multiple meta_key in pre_get_posts

How to count how many times a meta_value appears in a column by certain meta_key?

Group similar meta_key values and create result set with meta_value data

Retrieve Post by Custom Query String identified by meta_key and meta_value in Wordpress

CONCAT(meta_key,meta_value) into different columns from wp_postmeta table

How to copy meta_value of wp_usermeta to the same table however different meta_key?

SQL query with meta_key,meta_value and have 2 conditions