החזר את 'תכונות' המוצר בשורטקוד

החזרת תכונות מוצרים דרך שורטקוד דרך מצוינת להצגת תכונות במוצרים מרובי תכונות – למי שלא אוהב את העיצוב הדיפולטיבי ומוגדר התבנית של טבלת ווקומרס (ווידג'ט 'מידע נוסף'),
הכניסו את הקוד הזה לתבנית שלכם:

function display_product_attributes_shortcode( $atts ) {
$atts = shortcode_atts( array(
'id' => get_the_ID(), // get the current post ID if no ID is provided
), $atts, 'product_attributes' );
 
// get the product object
$product = wc_get_product( $atts['id'] );
 
// get the attributes for the product
$attributes = $product->get_attributes();
 
// create an empty array to store the attribute strings
$attribute_strings = array();
 
// loop through the attributes and add them to the array
foreach ( $attributes as $attribute ) {
// remove the "pa_" prefix from the attribute name
$name = str_replace( 'pa_', '', $attribute->get_name() );
 
if ( $attribute->is_taxonomy() ) {
// if the attribute is a taxonomy, get the term(s) and add them to the array
$terms = wp_get_post_terms( $atts['id'], $attribute->get_name(), 'all' );
 
$term_names = array();
 
foreach ( $terms as $term ) {
$term_names[] = $term->name;
}
 
$attribute_string = '<strong>' . $name . ':</strong> ' . implode( ', ', $term_names );
} else {
// if the attribute is not a taxonomy, get the options and add them to the array
$options = $attribute->get_options();
 
$option_names = array();
 
foreach ( $options as $option ) {
$option_names[] = $option;
}
 
$attribute_string = '<strong>' . $name . ':</strong> ' . implode( ', ', $option_names );
}
 
// add the attribute string to the array
$attribute_strings[] = $attribute_string;
}
 
// join the attribute strings into a single comma-separated string with a period at the end
$output = implode( '. ', $attribute_strings ) . '.';
 
return $output;
}
add_shortcode( 'product_attributes', 'display_product_attributes_shortcode' )

 

ולסיום הכניסו את השוקטקוד הזה: [product_attributes] לכל וידג'ט, הקוד יזין ברצף ובאופן דינמי את כל תכונות המוצר שהוזנו, עם הדגשת שם התכונה!
טיפ קטן – תוכלו לתת תוכן דינמי בוידג'ט ואחריו את השורטקוד!

 

אפשרות שנייה

החזרת התכונות מסודר ברשימת תבליטים

עוד אפשרות אם תרצו את התכונות מסודרות ברשימת תבליטים אז הכניסו לתבנית את הקוד הזה במקום הקוד למעלה:

function display_product_attributes_shortcode( $atts ) {
$atts = shortcode_atts( array(
'id' => get_the_ID(), // get the current post ID if no ID is provided
), $atts, 'product_attributes' );
 
// get the product object
$product = wc_get_product( $atts['id'] );
 
// get the attributes for the product
$attributes = $product->get_attributes();
 
// create an output string to display the attributes
$output = '<ul class="product-attributes">';
 
// loop through the attributes and add them to the output string
foreach ( $attributes as $attribute ) {
// remove the "pa_" prefix from the attribute name
$name = str_replace( 'pa_', '', $attribute->get_name() );
 
$output .= '<li><strong>' . $name . ':</strong> ';
 
if ( $attribute->is_taxonomy() ) {
// if the attribute is a taxonomy, get the term(s) and add them to the output string
$terms = wp_get_post_terms( $atts['id'], $attribute->get_name(), 'all' );
 
foreach ( $terms as $term ) {
$output .= $term->name . ', ';
}
 
$output = rtrim( $output, ', ' ); // remove the trailing comma and space
} else {
// if the attribute is not a taxonomy, get the options and add them to the output string
$options = $attribute->get_options();
 
foreach ( $options as $option ) {
$output .= $option . ', ';
}
 
$output = rtrim( $output, ', ' ); // remove the trailing comma and space
}
 
$output .= '</li>';
}
 
$output .= '</ul>';
 
return $output;
}
add_shortcode( 'product_attributes', 'display_product_attributes_shortcode' );

 

המדריך עזר לכם?

החזר את 'תכונות' המוצר בשורטקוד
מה במדריך ?

Products

CSS & Design

Shortcode