php - add_filter() inside another add_filter()

I'm simply trying to call one (update post description) if the other signals out of stock. They both work perfectly

I'm simply trying to call one (update post description) if the other signals out of stock. They both work perfectly independently:

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);

What am I doing wrong?

I'm simply trying to call one (update post description) if the other signals out of stock. They both work perfectly independently:

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);

What am I doing wrong?

Share Improve this question asked Mar 24, 2019 at 18:57 eddiewastakeneddiewastaken 1111 bronze badge 1
  • I would guess that the call to woocommerce_short_description you're trying to hook into has already happened when you add that hook in woocommerce_get_availability. – mrben522 Commented Mar 24, 2019 at 19:26
Add a comment  | 

1 Answer 1

Reset to default 0

I would use do_action instead of add_filter. While similar, you are hooking into an action verse filtering a function. close, but different.

 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;
 }

Good article on the subject: https://wpsmith/2011/the-difference-between-do_action-add_action-and-add_filter/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745661845a4638883.html

相关推荐

  • php - add_filter() inside another add_filter()

    I'm simply trying to call one (update post description) if the other signals out of stock. They both work perfectly

    22天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信