我想始终在产品页面上显示每个变体的变体描述(当前,它将仅显示通过下拉列表选择的任何变体的描述)。我似乎无法访问
在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应该使用什么/如何为所有变量打印出来?
答案 0 :(得分:0)
最终,我可以通过获取产品的所有变体,循环浏览它们,找到匹配项,然后将该描述设置为变量来实现此目的。这似乎有些笨拙,可能会导致较大的目录出现问题,因此欢迎进行任何改进!
// 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;