Php 为变体的特定属性获取相应的数据值
我正在编辑variable-subscription.php文件,试图显示特定的变量订阅属性以及某些属性数据。这是到目前为止我的代码,您可以看到我正在尝试显示属性名称以及变体图像和描述Php 为变体的特定属性获取相应的数据值,php,wordpress,woocommerce,subscriptions,variations,Php,Wordpress,Woocommerce,Subscriptions,Variations,我正在编辑variable-subscription.php文件,试图显示特定的变量订阅属性以及某些属性数据。这是到目前为止我的代码,您可以看到我正在尝试显示属性名称以及变体图像和描述 <?php // get all variations $variations = $product->get_available_variations(); // get shirt values $shirt_values = get_the_terms( $product_id, 'pa_sh
<?php
// get all variations
$variations = $product->get_available_variations();
// get shirt values
$shirt_values = get_the_terms( $product_id, 'pa_shirt');
// for each shirt value
foreach ( $shirt_values as $shirt_value ) { ?>
<h1>
<?php // name
echo echo $shirt_value->name;
?>
</h1>
<?php // description
foreach ( $variations as $variable_array ){
$variation = new WC_Product_Variation( $variable_array['variation_id'] );
echo $variation->variation_description;
}
?>
<?php // image
foreach ( $variations as $variation ) { ?>
<img src="<?php echo $variation['image_src']; ?>">
<?php }
?>
">
目前,我有3件衬衫出售(在定制产品属性中定义),然后我有几个变体。问题是PHP显示了每件衬衫的所有可能的描述/图像变体,我只想显示与每个特定衬衫属性相关的变体
任何建议都将不胜感激。您需要在类WC\u Product\u变体中使用该方法,该方法将为该变体返回属性数组及其值 因此,对于属性
pa_shirt
,显示特定变量值的代码将是:
$variations = $product->get_available_variations();
// Here we use our method to get the array of attribute values for the variation
$variation_attribute = $variation->get_variation_attributes();
// Here is the value for this variation and "pa_shirt" attribute
$variation_attrib_shirt_value = $variation_attribute['attribute_pa_shirt'];
正如您可以注意到的那样,数组中的属性键slug在开始时具有所有的'attribute'