在WooCommerce中,在Products>Attributes>[Name of Attribute]>Add New[Attribute Variation]下有一个标题为"Description"的部分,如何在varation.php文件中显示Description属性而不是Description {{{ data.variation.variation_description }}}

我已经try 了How to get current selected variable product specific data in WooCommerce个应答代码,但不知道如何显示属性描述.

推荐答案

这不需要使用JavaScript或jQuery.使用以下内容,您将能够使用属性术语描述(S)替换变体描述:

add_filter( 'woocommerce_available_variation', 'display_available_attributes_term_description', 10, 3 );
function display_available_attributes_term_description( $data, $product, $variation ) {
    $terms_descriptions = array(); // Initialize variable

    // loop through attributes variation
    foreach( $data['attributes'] as $attr_taxonomy => $term_slug ) {
        $taxonomy = str_replace('attribute_', '', $attr_taxonomy);
        // Check that is not a custom attribute
        if( ! taxonomy_exists($taxonomy) ) {
            continue;
        }
        $term_id = get_term_by('slug', $term_slug, $taxonomy)->term_id; // Get the term ID
        $term_description = term_description( $term_id ); // Get the term description
        // Check that term description is not empty
        if( ! empty($term_description) ) {
            $terms_descriptions[] = $term_description; // Add the term description to the array
        }
    }
    // Replace variation description with attribute(s) term description(s)
    if( count($terms_descriptions) > 0 ) {
        $data['variation_description'] = implode(' <br>', $terms_descriptions); // stringify the array
    }

    return $data;
}

代码位于您的子主题的unctions.php文件中(或在插件中).经过测试,效果良好.


如果要保留变体说明,并在其后面显示属性术语说明,请替换:

$data['variation_description'] = implode(' <br>', $terms_descriptions);

有:

$data['variation_description'] .= ' <br>' . implode(' <br>', $terms_descriptions);

Php相关问答推荐

Laravel 10 -扩展现有的artisan命令?

Laravel Livewire如何在数组值上使用IF、ELSE条件

如何在PHP中出现弃用警告时触发错误?

是否重新排序多维数组元素以将所有子数组中的子数组移动到元素列表的底部?

列出所有WooCommerce处理订单中的产品数量,SKU和品牌

如何在UML类图中可视化变量引用数组

模仿GUZLE中的curl 工作脚本(分块数据和二进制上传一起)

在WooCommerce上设置简单和可变产品的最小订单量

如何批量处理消息总线中的消息

以编程方式同步 WPML 翻译更改 Woocommerce 产品销售价格

无法在 WordPress 上获取 JSON 数据

PHP 中的 libgd 无法读取 Apache 配置中的 SetEnv GDFONTPATH

计算数组中连续值的数量

设置GA4 PHP API的setSubject以允许访问所有管理员账户属性

批量作业(job)提交错误无法处理所有文档,uris 似乎正确?

使用 GROUP_CONCAT 规范化/转置来自查询的数据

使用 splat 运算符时按引用传递 (...)

一个php应用程序可以实例化多个RNG吗

fopen 功能不断向我发送我重新加载网页时已发布的重复版本的表单

使用 OOP PHP 创建动态 WHERE 子句.如何实现 BETWEEN 运算符?