如何解决显示WooCommerce单个产品中的可用库存变化简短说明
我有多种产品的可变产品,其中实际上只有几件库存,而其他大多数产品都“有现货”
我希望能够在每个产品页面的简短产品描述中仅显示一个库存中的项目的快速列表,这样客户不必一次一试所有的变体来最终找到缺货了。
我搜索了可以执行此操作但未找到任何内容的插件或代码。
add_action( 'woocommerce_after_shop_loop_item','bb_echo_stock_variations_loop' );
function bb_echo_stock_variations_loop(){
global $product;
if ( $product->get_type() == 'variable' ) {
foreach ( $product->get_available_variations() as $key ) {
$attr_string = array();
foreach ( $key['attributes'] as $attr_name => $attr_value ) {
$attr_string[] = $attr_value;
}
if ( $key['max_qty'] > 0 ) {
echo '<br/>' . implode( ',',$attr_string ) . ': ' . $key['max_qty'] . ' in stock';
} else {
echo '<br/>' . implode(',$attr_string ) . ': out of stock';
}
}
}
}
但是它会在SHOP页面上显示“有现货”的可用变体,我希望它显示在单个产品的简短说明中。
如何在单个产品的简短说明中显示“有现货”的可用变体形式?
解决方法
要以简短说明在产品单页上的库存变化列表中显示,请使用以下内容:
add_filter( 'woocommerce_short_description','display_in_stock_variations_to_short_description' );
function display_in_stock_variations_to_short_description( $excerpt ){
global $product;
if ( ! is_product() || empty($product) || ! is_a( $product,'WC_Product' ) )
return $excerpt;
if( $product->is_type('variable') ) {
// Loop through visible children
foreach( $product->get_children() as $variation_id ) {
$variation = wc_get_product( $variation_id );
// Hide out of stock variations if 'Hide out of stock items from the catalog' is checked.
if ( ! $variation || ! $variation->exists() || ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) && ! $variation->is_in_stock() ) ) {
continue;
}
// Filter 'woocommerce_hide_invisible_variations' to optionally hide invisible variations (disabled variations and variations with empty price).
if ( apply_filters( 'woocommerce_hide_invisible_variations',true,$product->get_id(),$variation ) && ! $variation->variation_is_visible() ) {
continue;
}
$max_qty = 0 < $variation->get_max_purchase_quantity() ? $variation->get_max_purchase_quantity() : $variation->get_stock_quantity();
$term_names = []; // Initializing
// Loop through variation attributes for current varation
foreach ( $variation->get_variation_attributes() as $attribute => $term_slug ) {
// Set the term name in an array
$term_names[] = ucfirst( str_replace( ['-','_'],[' ',' '],$term_slug ) );
}
if ( $max_qty > 0 ) {
$excerpt .= sprintf( '<br/>%s: %s %s',implode(',',$term_names),$max_qty,__('in stock','woocommerce')
);
}
}
}
return $excerpt;
}
// Avoid additional content from product short description to be displayed in variation description
add_filter( 'woocommerce_available_variation','filter_wc_available_variation_desscription',10,3);
function filter_wc_available_variation_desscription( $data,$product,$variation ) {
$max_qty = 0 < $variation->get_max_purchase_quantity() ? $variation->get_max_purchase_quantity() : $variation->get_stock_quantity();
if( $max_qty > 0 )
$data['variation_description'] = get_post_meta( $variation->get_id(),'_variation_description',true );
return $data;
}
代码进入活动子主题(或活动主题)的functions.php文件中。经过测试,可以正常工作。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。