[英]Get product variation id by their product attributes slug values pairs in Woocommerce
How can I get product variation ID by taxonomy terms?如何通过分类术语获取产品变体 ID?
For example if I have two taxonomies and their terms:例如,如果我有两个分类法及其术语:
1) Size: medium, large
2) Color: blue, green
And my product variations:我的产品变体:
Variation id 1: medium > blue
Variation id 2: medium > green
Variation id 3: large > blue
Variation id 4: large > green
I would like to get variation ID of this terms combination medium > green
.我想获得此术语组合
medium > green
变体 ID。
The following function uses a very light SQL query to get the variation ID for from a variable product for defined product attributes "color" and "size" term slugs:以下函数使用一个非常轻量级的 SQL 查询从变量产品中获取已定义产品属性“颜色”和“尺寸”术语 slug 的变体 ID:
function get_product_variation_id( $size, $color, $product_id = 0 ) {
global $wpdb;
if ( $product_id == 0 )
$product_id = get_the_id();
return $wpdb->get_var( "
SELECT p.ID
FROM {$wpdb->prefix}posts as p
JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id
JOIN {$wpdb->prefix}postmeta as pm2 ON p.ID = pm2.post_id
WHERE pm.meta_key = 'attribute_pa_color'
AND pm.meta_value LIKE '$color'
AND pm2.meta_key = 'attribute_pa_size'
AND pm2.meta_value LIKE '$size'
AND p.post_parent = $product_id
" );
}
Code goes in function.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 function.php 文件中。 Tested and works.
测试和工作。
USAGE (2 cases examples) :用法(2 例) :
From a defined variable product ID (where 746
is the parent variable product ID):从定义的变量产品 ID(其中
746
是父变量产品 ID):
$variation_id = get_product_variation_id( 'large', 'blue', 746 ); $variation_id = get_product_variation_id('大','蓝色',746);
On the variable product single product page (No need to define the variable product ID) :在可变产品单品页面(无需定义可变产品ID) :
$variation_id = get_product_variation_id( 'large', 'blue' ); $variation_id = get_product_variation_id('大','蓝色');
I haven't tested but this should be nearly there or ready to ship.我还没有测试过,但这应该快到了或准备发货了。
function get_variation_by($size, $color) {
$args = array(
'post_type' => 'product',
'numberposts' => -1,
);
$product_posts = get_posts( $args );
foreach ( $product_posts as $single_product_post ) {
$_product = wc_get_product( $single_product_post->ID );
if ($product_s->product_type != 'variable') continue;
$avail_vars = $_product->get_available_variations();
foreach ($avail_vars as $v)
if ($v["attributes"]["attribute_pa_size"] == $size
&& $v["attributes"]["attribute_pa_color"] == $color)
return $v;
}
return null;
}
$variation = get_variation_by( "large", "green");
$variation_id = $variation->variation_id;
If you have the product_id of the variable product already, you can simplify like this:如果你已经有了变量 product 的 product_id ,你可以像这样简化:
function get_variation_by($variable_product_id, size, $color) {
$var_prod = wc_get_product( $variable_product_id );
$avail_vars = $var_prod->get_available_variations();
foreach ($avail_vars as $v)
if ($v["attributes"]["attribute_pa_size"] == $size
&& $v["attributes"]["attribute_pa_color"] == $color)
return $v;
return null;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.