在产品单页上显示自定义和标准产品的致辞WooCommerce

时间:2020-04-30 09:35:49

标签: php wordpress woocommerce custom-taxonomy elementor

我已经在所有位置进行搜索,并且找到了不错的代码来显示标准产品属性,但是未显示自定义属性(不以pa_开头)。

首先,设置: 我已经使用Elementor创建了一个产品单一模板,通常我只需添加模块附加信息即可。我以前做过:)

但是现在我创建了一个布局,其中这些产品属性显示在手风琴中,您不能在其中放置此模块。

因此,在进行了一次在线搜索之后,我发现了snippet made by helgatheviking,可以创建一个短代码来调用所有产品属性:

/**
 * Attributes shortcode callback on productpage.
 */
function so_39394127_attributes_shortcode( $atts ) {

    global $product;

    if( ! is_object( $product ) || ! $product->has_attributes() ){
        return;
    }

    // parse the shortcode attributes
    $args = shortcode_atts( array(
        'attributes' => array_keys( $product->get_attributes() ), // by default show all attributes
    ), $atts );

    // is pass an attributes param, turn into array
    if( is_string( $args['attributes'] ) ){
        $args['attributes'] = array_map( 'trim', explode( '|' , $args['attributes'] ) );
    }

    // start with a null string because shortcodes need to return not echo a value
    $html = '';

    if( ! empty( $args['attributes'] ) ){

        foreach ( $args['attributes'] as $attribute ) {

            // get the WC-standard attribute taxonomy name
            $taxonomy = strpos( $attribute, 'pa_' ) === false ? wc_attribute_taxonomy_name( $attribute ) : $attribute;

            if( taxonomy_is_product_attribute( $taxonomy ) ){

                // Get the attribute label.
                $attribute_label = wc_attribute_label( $taxonomy );

                // Build the html string with the label followed by a clickable list of terms.
                // Updated for WC3.0 to use getters instead of directly accessing property.
                $html .= get_the_term_list( $product->get_id(), $taxonomy, '<li class="prod-attr">' . $attribute_label . ': ' , ', ', '</li>' );
            }

        }

        // if we have anything to display, wrap it in a <ul> for proper markup
        // OR: delete these lines if you only wish to return the <li> elements
        if( $html ){
            $html = '<ul class="product-attributes">' . $html . '</ul>';
        }

    }

    return $html;
}
add_shortcode( 'display_attributes', 'so_39394127_attributes_shortcode' );

在手风琴中添加了简码[display_attributes]之后,我得到了所有显示的通过标准方法创建的标准WC属性:产品->属性->创建新属性。太好了!

不是您在productpage本身内部创建的自定义属性->产品数据->自定义产品属性->添加(我的意思是https://gyazo.com/f2619c7f11d295f4897b38f08d76453b的屏幕截图)

屏幕截图产品页面自定义属性未显示在前面:https://gyazo.com/4c126f7082c6a856d1cf30b712daa37f

我认为这是因为这些自定义产品属性在元值前面不包含pa_部分,例如pa_type-speaker(自行创建的标准属性)。

如何获得同时显示的自定义属性?我试图从上面的代码中删除pa_,但是那行不通,而且我找不到下一步要做什么。你们可以帮我吗?谢谢!

1 个答案:

答案 0 :(得分:0)

Elementor帮了我这个忙!

解决方案:

打开产品单个模板页面,连续添加其他信息模块。创建此对象的全局窗口小部件。在选项卡式模块中添加此全局窗口小部件的简码,然后完成。 :)