在Woocommerce中,我有这些属性和术语:
color: blue(description=1), red(description=2)
size: xl(description=10), m(description=20)所有术语都有上面提到的描述字段。
对于可变产品,我有以下变化:
blue-m
red-xl为了自动生成变体SKU (基于这些描述),我需要获取每个变体中使用的属性术语的描述(我们为每个变量产品使用不同的属性)。
例如,对于red颜色和xl大小的变化,我希望得到类似210的内容(来自red和xl的描述)
我尝试使用$variation['attributes']并删除与get_terms一起使用的额外字符,以获取在变体中使用的属性的描述,但我不能成功。
有什么想法吗?
发布于 2018-01-16 01:00:28
一旦您以这种方式获得产品变体ID $variation_id,就可以很容易地完成此操作:
// Initializing
$variation_sku = '';
// Get the product variation object (instance)
$variation = wc_get_product( $variation_id );
// Loop through variation attributes "taxonomy" / "terms" pairs
foreach( $variation->get_variation_attributes() as $attribute => $value ){
$taxonomy = str_replace( 'attribute_', '', $attribute );
$term = get_term_by( 'slug', $value, $taxonomy );
// $term_name = $term->name;
$variation_sku .= $term->description; // <= HERE the Description
}
// Displaying the sku
echo '<p>'.__("SKU: ").$variation_sku.'</p>';https://stackoverflow.com/questions/48266302
复制相似问题