WooCommerce 在商店页面显示产品属性,兼容多语言

问题描述 投票:0回答:1

我已成功将 WooCommerce 更改为在商店页面上显示属性。 但是,我的网站是多语言的,我怎样才能使这个功能多语言呢?

add_action( 'woocommerce_after_shop_loop_item_title', 'display_size_attribute', 5 );
function display_size_attribute() {
    global $product;

    if ( $product->is_type('simple') ) {
        $taxonomy = 'klasse-graafmachine-ton';
        echo '<span class="attribute-s">Klasse graafmachine (t): ' . $product->get_attribute($taxonomy) . '</span>','<br>';
    }
}
php woocommerce attributes custom-taxonomy wpml
1个回答
0
投票

您不应该使用自定义产品属性(在产品上本地创建),您应该使用在产品属性部分全局设置的全局产品属性分类法。

然后为您的产品属性配置术语...

现在,您将在每个产品上配置属性(并为其选择术语):

现在您可以在多语言插件中翻译此产品属性及其术语,因为它是类似于产品类别或产品标签的分类法。

然后您可以使用以下修改后的代码来显示档案中的属性:

add_action( 'woocommerce_after_shop_loop_item_title', 'display_size_attribute', 5 );
function display_size_attribute() {
    global $product;

    if ( $product->is_type('simple') ) {
        $taxonomy = 'pa_klasse-graafmachine-ton';
        printf( '<span class="attribute-s">%s: %s</span><br>', wc_attribute_label($taxonomy, $product), $product->get_attribute($taxonomy) );
    }
}

应该可以。

注意: 代码中使用的分类法为 (始终以“pa_”开头)

© www.soinside.com 2019 - 2024. All rights reserved.