douyi2798 2019-03-18 13:28
浏览 178
已采纳

使用短代码在侧边栏中获取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?

  • 写回答

2条回答 默认 最新

  • duan0513 2019-03-18 17:34
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?