简体   繁体   English

在产品单页上显示自定义和标准产品属性 WooCommerce

[英]Show custom AND standard products atrributes on Product Single page WooCommerce

I have searched all over for this, and I have found a great code to get the standard product attributes displayed, but the custom ones (that do not start with pa_) are not being displayed.我已经为此进行了全面搜索,并且找到了一个很好的代码来显示标准产品属性,但是没有显示自定义属性(不以 pa_ 开头)。

First things first, the setup: I have created a product single template with Elementor and normally I can just add the module additional information and I am done.首先,设置:我已经使用 Elementor 创建了一个产品单一模板,通常我可以添加模块的附加信息,我就完成了。 I've done it before.我以前做过。 :) :)

But now I have created a lay-out where these product attributes are being displayed in an accordion in which you cannot place this module.但是现在我创建了一个布局,其中这些产品属性显示在您无法放置此模块的手风琴中。

So after a loooooong search online I have found a snippet made by helgatheviking to create a shortcode to call all the product attributes:因此,在网上搜索了很久之后,我发现了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' );

After adding the shortcode [display_attributes] in the accordion, I get all the standard WC attributes shown that have been created via the standard way: products -> attributes -> create new.在手风琴中添加短代码 [display_attributes] 后,我得到了所有通过标准方式创建的标准 WC 属性:产品 -> 属性 -> 新建。 So that is great!那太好了!

But not the custom attributes, that you create inside the productpage itself -> Product data -> custom product attribute -> add (screenshot of what I mean: https://gyazo.com/f2619c7f11d295f4897b38f08d76453b )但不是您在产品页面本身内创建的自定义属性-> 产品数据-> 自定义产品属性-> 添加(我的意思的屏幕截图: https://gyazo.com/f2619c7f11d295f4897b38f08d76453b

Screenshot product page custom attributes are not being shown on front: https://gyazo.com/4c126f7082c6a856d1cf30b712daa37f屏幕截图产品页面自定义属性未显示在前面: https://gyazo.com/4c126f7082c6a856d1cf30b712daa37f

I think this is because that these custom product attributes do not contain the pa_ part in front of the meta value like for instance pa_type-speaker (self-created standard attribute).我认为这是因为这些自定义产品属性不包含元值前面的 pa_ 部分,例如 pa_type-speaker(自创标准属性)。

How can I get the custom attributes also shown?如何获得还显示的自定义属性? I have tried to remove the pa_ from the code above, but that does not work and I cannot find what to do next.我试图从上面的代码中删除 pa_,但这不起作用,我找不到下一步该做什么。 Can you guys help me out here?你们能帮我吗? thx!谢谢!

Elementor has helped me out on this one! Elementor 在这方面帮助了我!

The solution:解决方案:

Open the product single template page, Add the additional information module in a row.打开产品单模板页面,连续添加附加信息模块。 Create a global widget of this object.创建此 object 的全局小部件。 Add the shortcode of this global widget in the tabbed module and your done.在选项卡式模块中添加此全局小部件的简码,您就完成了。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 WooCommerce 单个产品页面上仅显示“有货”相关产品 - Show only "in stock" related products on WooCommerce single product page WooCommerce:仅在单个产品页面上显示价格后缀 - 不适用于相关产品 - WooCommerce: Show price suffix only on single product Page - Not for related products 在单个产品页面上增加相关产品(woocommerce) - Increase related products on single product page (woocommerce) 产品页面在woocommerce单个产品页面中没有显示任何产品 - product page shows no products in woocommerce single product page Woocommerce-单一产品-针对不同产品显示不同内容 - Woocommerce - single product - show different content for different products 在 WooCommerce 单品上动态设置和显示自定义产品变化值 - Set and display a custom product variation value dynamically on WooCommerce single products 显示 WooCommerce 自定义产品属性和单个产品的所有条款 - Display WooCommerce Custom Product Attributes and all terms on single products WooCommerce单品内容为空时隐藏自定义产品标签 - Hide custom product tab if content is empty in WooCommerce single products 在 WooCommerce 的单个产品页面上为低库存产品添加 CSS 类 - Add CSS class for low stock products on single product page in WooCommerce WooCommerce单品页面隐藏“缺货”相关产品 - Hide “out of stock” related products on WooCommerce single product page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM