WooCommerce:显示产品变化/变量描述



我希望始终在产品页面上显示每个变体的变体描述(目前它只显示通过下拉菜单选择的任何变体的描述(。我似乎无法访问

plugins/woocommerce/templates/single-product/add-to-cart/variation.php中,它似乎可以通过:访问

{{{ data.variation.variation_description }}}

当检查编辑产品页面中所需的字段时,其name=为:

variable_description0

我尝试通过这两种方式访问,以及:

if($product && taxonomy_exists($attribute)) {
$terms = wc_get_product_terms($product->get_id(), $attribute, array(
'fields' => 'all',
));
foreach($terms as $term) {
$variable_description = get_post_meta( $id, '_variation_description', true );
if(in_array($term->slug, $options, true)) {
$radios .= '<div><input type="radio" name="'.esc_attr($name).'" value="'.esc_attr($term->slug).'" '.checked(sanitize_title($args['selected']), $term->slug, false).'><label for="'.esc_attr($term->slug).'">'.esc_html(apply_filters('woocommerce_variation_option_name', $term->name)).' <span class="description">' . 'DESCRIPTION' . '</span></label></div>';
}
}
} else {
foreach($options as $option) {
// var_dump($options);
$variable_description = get_post_meta( $id, '_variation_description', true );

$checked    = sanitize_title($args['selected']) === $args['selected'] ? checked($args['selected'], sanitize_title($option), false) : checked($args['selected'], $option, false);
$radios    .= '<div><input type="radio" name="'.esc_attr($name).'" value="'.esc_attr($option).'" id="'.sanitize_title($option).'" '.$checked.'><label for="'.sanitize_title($option).'">'.esc_html(apply_filters('woocommerce_variation_option_name', $option)).' <span class="description">' . $variable_description . '</span></label></div>';
}
}

但是,所有内容都返回为NULL。

$id应该使用什么/如何为所有变量打印出来?

我最终能够实现这一点,方法是获取产品的所有变体,在它们之间循环,找到匹配项,然后将该描述设置为变量。这看起来有点笨拙,可能会导致更大的目录出现问题,所以欢迎任何改进!

// Get all variations of product
$variations1 = $product->get_children();
// Loop through variations to find description
foreach ($variations1 as $value):
$single_variation = new WC_Product_Variation($value);
$variation_full_name = $single_variation->name;
$variation_name = str_replace($product->name . " - ", "", $variation_full_name);
// Set the variable description
if ( $option === $variation_name ):
$variable_description = $single_variation->description;
endif;
endforeach;

更新2020年1月30日我们似乎在使用不推荐使用的代码

我已替换:

$variation_data[$single_variation->attributes[$attribute]] = $single_variation->description;
}

带有

$variation_data[$single_variation->get_attributes()[$attribute]] = $single_variation->get_description();

此处存在相同问题。我不敢相信没有一个简单的解决方案。

感谢您的代码-非常有用的

一个改进(我希望(是只对变体进行一次循环,并将值保存到数组中。

所以:

function mc_return_variation_data($product, $attribute){
$variation_data = array();
$variations = $product->get_children();
foreach ($variations as $value){
$single_variation = new WC_Product_Variation($value);
$variation_data[$single_variation->get_attributes()[$attribute]] = $single_variation->get_description();
}
return $variation_data;
}

然后,在获得$product和$attribute的值后,调用该函数一次,因此:

$variation_data = mc_return_variation_data($product, $attribute);

然后,当您在foreach($terms作为$term(循环中时,要获得实际描述,请使用:

$variable_description = $variation_data[$term->slug];

我唯一不确定的是我需要做多少错误检查来确保

$variation_data[$term->slug]

定义为

最新更新