【问题标题】:Get Woocommerce custom taxonomies in sidebar using a shortcode使用简码在侧边栏中获取 Woocommerce 自定义分类法
【发布时间】:2019-08-08 21:20:14
【问题描述】:

我发现这段代码将产品维度添加到产品循环中:

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>';
    }
}

如何将其转换为简码? 我还需要将自定义属性和分类法显示为颜色。如何达到?

【问题讨论】:

    标签: php woocommerce shortcode dimensions custom-taxonomy


    【解决方案1】:

    尝试以下基于single-product/product-attributes.php Woocommerce 模板的短代码,它将显示产品重量、尺寸和产品属性。您可以使用 id 属性指定产品 ID:

    [product_taxonomies id="37"]
    

    或者用在php代码中:

    echo do_shortcode("[product_taxonomies id='37']");
    

    或不指定产品ID(取当前帖子ID)

    [product_taxonomies]
    

    代码:

    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();
        }
    }
    

    此代码位于您的活动子主题(或活动主题)的 function.php 文件中。测试和工作

    【讨论】:

      【解决方案2】:

      您可以通过在短代码函数中调用 woocommerce 挂钩来实现。但它会调用挂在“woocommerce_after_shop_loop_item”中的所有函数。

      我不确定您的情况。但更好的方法是直接调用钩子而不调用短代码。

      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>';
          }
      }
      

      我没有测试过代码。因此,请尝试了解这个想法并进行调整。

      【讨论】:

        猜你喜欢
        • 2016-10-02
        • 2013-08-12
        • 1970-01-01
        • 2018-11-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多