[英]Display product attributes values for specific categories in WooCommerce product loops
I'm building a shop in WP + WooCommerce.我正在 WP + WooCommerce 中建立一个商店。 I have different types of product categories like discs and bags.
我有不同类型的产品类别,如光盘和包。 For discs products I have some specific attributes like Speed, Glide, Turn and Fade that don't have any other product categories.
对于光盘产品,我有一些特定的属性,例如 Speed、Glide、Turn 和 Fade,这些属性没有任何其他产品类别。 I want to display these product attribute values only on shop pages under the product picture.
我只想在产品图片下的商店页面上显示这些产品属性值。
I have found one code for that and I added myself a separation symbol "|", but this separation symbol is now displayed under all the products that are variable.我找到了一个代码,我给自己添加了一个分隔符号“|”,但是这个分隔符号现在显示在所有可变的产品下。
Is it possible to change the code not to variables but only for specific product categories and sub-categories?是否可以将代码更改为变量而不是仅针对特定产品类别和子类别?
Code:代码:
add_action( 'woocommerce_before_shop_loop_item_title', 'display_size_attribute', 5 );
function display_size_attribute() {
global $product;
if ( $product->is_type('variable') ) {
$taxonomy = 'pa_speed';
echo '<span class="attribute-speed">' . $product->get_attribute($taxonomy) . '</span>' ;
echo ' | ';
$taxonomy = 'pa_Glide';
echo '<span class="attribute-Glide">' . $product->get_attribute($taxonomy) . '</span>';
echo ' | ';
$taxonomy = 'pa_Turn';
echo '<span class="attribute-Turn">' . $product->get_attribute($taxonomy) . '</span>';
echo ' | ';
$taxonomy = 'pa_Fade';
echo '<span class="attribute-Fade">' . $product->get_attribute($taxonomy) . '</span>';
}
}
Use the following revisited code instead to get separated attribute values only if they exist on your variable products:仅当变量产品上存在单独的属性值时,才使用以下重新访问的代码来获取它们:
add_action( 'woocommerce_before_shop_loop_item_title', 'display_some_attributes', 5 );
function display_some_attributes() {
global $product;
if ( $product->is_type('variable') ) {
$attributes = array('speed', 'Glide', 'Turn', 'Fade'); // here you defined attribute slugs
$output = array(); // Initializing
// Loop through product attributes
foreach ( $attributes as $attribute ) {
$taxonomy = 'pa_' . $attribute;
$values = $product->get_attribute($taxonomy);
// If not empty add it to the array
if ( ! empty($values) ) {
$output[] = '<span class="attribute-' . $attribute . '">' . $values . '</span>';
}
}
// Convert array to a separated string by pipes
echo implode(' | ', $output);
}
}
To target specific product categories only, replace in the code the block:要仅针对特定产品类别,请在代码中替换以下块:
global $product;
if ( $product->is_type('variable') ) {
with (where you will define your product categories terms) : with (您将在其中定义产品类别术语) :
global $product;
$categories = array( 'cats', dogs' ); // Here your category terms ids, slugs or names
if ( $product->is_type('variable') && has_term( $categories, 'product_cat', $product->get_id() ) ) {
Code goes in functions.php file of the active child theme (or active theme).代码进入活动子主题(或活动主题)的functions.php文件。 Tested and works.
测试和工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.