我只是想打电话给一个(更新帖子描述),如果另一个信号缺货。它们都能完美地独立工作:
function single_product_short_description( $post_excerpt ){
global $product;
$product_id = method_exists( $product, \'get_id\' ) ? $product->get_id() : $product->id;
if ( is_single( $product_id ) )
$post_excerpt = \'<p class="some-class">\' . __( "Out of stock short desc here", "woocommerce" ) . \'</p>\';
return $post_excerpt;
}
function custom_get_availability( $availability, $_product ) {
global $product, $bar, $progress;
$stock = $product->get_total_stock();
$progress = 100-$stock;
$bar = do_shortcode(\'[wp_progress_bar text="Tickets Sold - " pc=\'.$progress.\']\');
if ( $_product->is_in_stock() ) {
$availability[\'availability\'] = __($bar, \'woocommerce\');
}
if ( !$_product->is_in_stock() ) {
add_filter( \'woocommerce_short_description\', \'single_product_short_description\', 10, 1 );
}
return $availability;
}
add_filter( \'woocommerce_get_availability\', \'custom_get_availability\', 1, 2);
我做错了什么?
SO网友:Debbie Kurth
我将使用do\\u操作而不是add\\u过滤器。虽然类似,但您正在连接到过滤函数的动作诗。接近,但不同。
function custom_get_availability( $availability, $_product ) {
global $product, $bar, $progress;
$stock = $product->get_total_stock();
$progress = 100-$stock;
$bar = do_shortcode(\'[wp_progress_bar text="Tickets Sold - " pc=\'.$progress.\']\');
if ( $_product->is_in_stock() ) {
$availability[\'availability\'] = __($bar, \'woocommerce\');
}
if ( !$_product->is_in_stock() ) {
do_action( \'woocommerce_short_description\', single_product_short_description\' );
}
return $availability;
}
function single_product_short_description( $post_excerpt )
{
global $product;
remove_action( \'woocommerce_short_description\', single_product_short_description\' );
$product_id = method_exists( $product, \'get_id\' ) ? $product->get_id() : $product->id;
if ( is_single( $product_id ) )
$post_excerpt = \'<p class="some-class">\' . __( "Out of stock short desc here", "woocommerce" ) . \'</p>\';
return $post_excerpt;
}
关于这个主题的好文章:
https://wpsmith.net/2011/the-difference-between-do_action-add_action-and-add_filter/