Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
867 views
in Technique[技术] by (71.8m points)

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>';
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...