我已成功将 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>';
}
}
您不应该使用自定义产品属性(在产品上本地创建),您应该使用在产品属性部分全局设置的全局产品属性分类法。这样,多语言插件将处理翻译。
现在您可以尝试在需要翻译的字符串上使用 WordPress
__()
函数,例如:
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';
printf( '<span class="attribute-s">%s: %s</span><br>', __("Klasse graafmachine (t)", "text_domain"), $product->get_attribute($taxonomy) );
}
}
然后,当使用任何多语言插件时,您将能够翻译该字符串。
wc_attribute_label()
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';
printf( '<span class="attribute-s">%s: %s</span><br>', wc_attribute_label($taxonomy, $product), $product->get_attribute($taxonomy) );
}
}