使用短代码在侧边栏中获取Woocommerce自定义分类

问题描述 投票:1回答:2

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

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
2个回答
0
投票

请尝试以下基于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文件中。经过测试和工作


0
投票

你可以通过调用短代码函数中的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>';
    }
}

我没有测试过该代码。因此,尝试获得这个想法,并做出你的调整。

© www.soinside.com 2019 - 2024. All rights reserved.