获取产品变体属性术语的描述

时间:2018-01-15 15:47:11

标签: php wordpress woocommerce custom-taxonomy variations

在Woocommerce中,我有这些属性及其条款:

color: blue(description=1), red(description=2)
size:  xl(description=10),  m(description=20)

所有条款都有上述说明字段。

对于可变产品,我有以下变化:

blue-m 
red-xl

为了自动生成变体SKU(基于这些描述),我需要获得每个变体中使用的属性术语的描述(我们对每个变量产品使用不同的属性)。

例如,对于red颜色和xl尺寸的变体,我希望获得类似210的内容(来自redxl的说明)

我尝试使用$variation['attributes']并删除其中的额外字符以与get_terms一起使用以获取变体中使用的属性的描述,但我无法成功。

任何想法?

1 个答案:

答案 0 :(得分:1)

一旦您以这种方式获得产品变体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>';
相关问题