[英]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.