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



我已经到处搜索了这个,我找到了一个很好的代码来显示标准的产品属性,但自定义的(不是以pa_开头的(没有显示。

首先,设置:我已经用Elementor创建了一个产品单一模板,通常我只需要添加模块的附加信息,我就完成了。我以前做过。:(

但现在我已经创建了一个布局,在这个布局中,这些产品属性被显示在手风琴中,而你不能在手风琴中放置这个模块。

因此,在网上搜索后,我发现了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' );

在手风琴中添加了shortcode[display_attributes]后,我得到了所有通过标准方式创建的标准WC属性:products->attributes->create new。太棒了!

但不是自定义属性,您在产品页面本身->产品数据->自定义产品属性->添加(我的意思是:https://gyazo.com/f2619c7f11d295f4897b38f08d76453b)

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

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

如何才能同时显示自定义属性?我试图从上面的代码中删除pa_,但这不起作用,我找不到下一步该怎么办。你们能帮我吗?thx!

Elementor帮了我一把!

解决方案:

打开产品单一模板页面,在行中添加附加信息模块。创建此对象的全局小部件。在选项卡模块中添加此全局小部件的快捷代码,然后完成。:(

最新更新