在 Woocommerce 中选择变体时显示产品属性术语描述的短代码

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

我正在寻找一种方法来创建 wordpress 短代码来显示 woocommerce 变量产品所选属性的术语描述。

例如在带有颜色变化和徽标变化的 T 恤上 我希望能够显示 2 个术语的描述:所选颜色和所选徽标的描述,一个位于另一个之下。

我依靠这段代码来了解如何创建短代码: 显示为 WooCommerce 产品设置的所有产品属性的短代码

并在此代码上检索和显示属性描述,但我似乎无法将两者结合起来。 如何使用 woocommerce 显示属性变体描述?

预先感谢您的帮助和建议。

LeoM

php wordpress woocommerce shortcode
2个回答
0
投票

通过此代码,我设法显示产品上使用的变体的所有描述:

add_shortcode( 'woo_attribute_descriptions', 'woo_attribute_descriptions_shortcode' );
function woo_attribute_descriptions_shortcode( $atts ) {
    global $product;
    $atts = shortcode_atts( array('product_id' => $product->get_id(),), 
    $atts, 'woo_attribute_descriptions' );
    $product_id = $atts['product_id'];
    $product = wc_get_product( $product_id );
    $attributes = $product->get_attributes();
    $output = '';
    foreach ( $attributes as $attribute ) {
        $name = wc_attribute_label( $attribute->get_name() );
        $terms = wc_get_product_terms( $product_id, $attribute->get_name(), array( 'fields' => 'all' ) );
        foreach ( $terms as $term ) {
            $output .= '<p>' . wpautop($term->description) . '</p>';
        }
    }
return $output;}

如何只显示所选变体的描述?

谢谢!


0
投票

我正在寻找相同的选项:

使用简单的简码在变体描述字段中自动显示变体选定的属性。

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