使用短代码在侧边栏中获取Woocommerce自定义分类
I found this code that adds product dimensions to product loop:
add_action( 'woocommerce_after_shop_loop_item', 'show_product_dimensions_loop', 100 );
function show_product_dimensions_loop() {
global $product;
$dimensions = $product->get_dimensions();
if ( ! empty( $dimensions ) ) {
echo '<div class="dimensions"><b>Height:</b> ' . $product->get_height() . get_option( 'woocommerce_dimension_unit' );
echo '<br><b>Width:</b> ' . $product->get_width() . get_option( 'woocommerce_dimension_unit' );
echo '<br><b>Length:</b> ' . $product->get_length() . get_option( 'woocommerce_dimension_unit' );
echo '</div>';
}
}
How do I transform it to shortcode? I also need to display custom attributes and taxonomies as colors. How to reach that?
我发现此代码将产品维度添加到产品循环中: p>
add_action('woocommerce_after_shop_loop_item','show_product_dimensions_loop',100); function show_product_dimensions_loop(){ global $ product; $ dimensions = $ product-&gt; get_dimensions(); if (!empty($ dimensions)){ echo'&lt; div class =“dimensions”&gt;&lt; b&gt; Height:&lt; / b&gt; '。 $ product-&gt; get_height()。 get_option('woocommerce_dimension_unit'); echo'&lt; br&gt;&lt; b&gt;宽度:&lt; / b&gt; '。 $ product-&gt; get_width()。 get_option('woocommerce_dimension_unit'); echo'&lt; br&gt;&lt; b&gt;长度:&lt; / b&gt; '。 $ product-&gt; get_length()。 get_option('woocommerce_dimension_unit'); echo'&lt; / div&gt;'; } } code> pre>如何将其转换为短代码?\ 我还需要将自定义属性和分类法显示为颜色。 如何达到? p> div>
Try the following shortcode based on single-product/product-attributes.php
Woocommerce template, that will display product weight, dimensions and product attributes. You can specify the product ID using the id
attribute:
[product_taxonomies id="37"]
or used in php code:
echo do_shortcode("[product_taxonomies id='37']");
or without specifying the product ID (takes the current post ID):
[product_taxonomies]
The code:
add_shortcode( 'product_taxonomies', 'product_taxonomies_shortcode' );
function product_taxonomies_shortcode( $atts ){
// Shortcode Attributes
$atts = shortcode_atts( array(
'id' => '',
), $atts, 'product_taxonomies' );
$product_id = ! empty($atts['id']) ? $atts['id'] : 0;
if( $product_id > 0 ) {
$product = wc_get_product( $product_id );
} else {
global $product;
}
if( ! is_a($product, 'WC_Product' ) ) {
$product = wc_get_product( get_the_id() );
}
if ( is_a($product, 'WC_Product' ) ) {
ob_start();
// Weight
if ( $product->has_weight() ) {
$weight_unit = get_option('woocommerce_weight_unit');
$weight_html = '<strong>'.__("Weight").':</strong> ' . $value . $weight_unit;
echo '<div class="dimensions">' . $weight_html . '</div>';
}
// Dimensions
if ( $product->has_dimensions() ) {
$dimension_unit = get_option('woocommerce_dimension_unit');
$dimensions = array();
foreach( $product->get_dimensions( false ) as $key => $value ) {
if( ! empty($value) )
$dimensions[] = '<strong>'.ucfirst($key).':</strong> ' . $value . $dimension_unit;
}
echo '<div class="dimensions">' . implode('<br>', $dimensions) . '</div>';
}
// Product attributes (visible)
if ( $attributes = array_filter( $product->get_attributes(), 'wc_attributes_array_filter_visible' ) ) {
foreach ( $attributes as $attribute ) {
$attribute_label_name = '<strong>'.wc_attribute_label( $attribute->get_name() ).':</strong> ';
$values = array();
if ( $attribute->is_taxonomy() ) {
$attribute_taxonomy = $attribute->get_taxonomy_object();
$attribute_values = wc_get_product_terms( $product->get_id(), $attribute->get_name(), array( 'fields' => 'all' ) );
foreach ( $attribute_values as $attribute_value ) {
$value_name = esc_html( $attribute_value->name );
if ( $attribute_taxonomy->attribute_public ) {
$values[] = '<a href="' . esc_url( get_term_link( $attribute_value->term_id, $attribute->get_name() ) ) . '" rel="tag">' . $value_name . '</a>';
} else {
$values[] = $value_name;
}
}
} else {
$values = $attribute->get_options();
foreach ( $values as &$value ) {
$value = make_clickable( esc_html( $value ) );
}
}
echo '<div class="'.$attribute->get_name().'">' . $attribute_label_name . implode( ', ', $values ) . '</div>';
}
}
return ob_get_clean();
}
}
This code goes in function.php file of your active child theme (or active theme). Tested and works
You can achieve it by calling the woocommerce hook inside the shortcode function. But it will call all the functions hooked inside 'woocommerce_after_shop_loop_item'.
I am not sure about your scenario. But better Way is to call the hook directly without calling shortcode.
add_shortcode( 'your-shortcode', 'shortcode-fn' );
function shortcode-fn($atts){
ob_start();
do_action('woocommerce_after_shop_loop_item');
ob_end_clean();
}
add_action( 'woocommerce_after_shop_loop_item', 'show_product_dimensions_loop', 100 );
function show_product_dimensions_loop() {
global $product;
$dimensions = $product->get_dimensions();
if ( ! empty( $dimensions ) ) {
echo '<div class="dimensions"><b>Height:</b> ' . $product->get_height() . get_option( 'woocommerce_dimension_unit' );
echo '<br><b>Width:</b> ' . $product->get_width() . get_option( 'woocommerce_dimension_unit' );
echo '<br><b>Length:</b> ' . $product->get_length() . get_option( 'woocommerce_dimension_unit' );
echo '</div>';
}
}
I haven't tested the Code. So Try to get this idea and make your Tweaks.