woocommerce_is_purchasable filter is fired 5 times!
-
Hi,
The
woocommerce_is_purchasable
filter gets triggered 5 times for each product on the shop page. In my method isPurchasable I do one database call to fetch relevant information to determine if the product should be purchasable. When I visit the shop page, the backend does 5 database calls per product.For debug purposes I added
echo "Hello";
to my isPurchasable method and I end up seeing 5 “Hello”s under each product.This slows down my site because it is doing a lot of unnecessary database calls.
Why does this filter seem to be triggered 5 times for each product?
Here is the structure of my plugin and how I use the filter hook.
class MyPlugin{ private $controller; public function __construct() { $this->controller = new Controller(); $this->addHooks(); } /* * Method: Adding necessary hooks */ private function addHooks (){ //Determine if the product is purchasable add_filter('woocommerce_is_purchasable', array($this->controller, 'isPurchasable'), 10, 2 ); } } if ( class_exists( 'MyPlugin' ) ){ $myPlugin = new MyPlugin(); }
Viewing 6 replies - 1 through 6 (of 6 total)
Viewing 6 replies - 1 through 6 (of 6 total)
- The topic ‘woocommerce_is_purchasable filter is fired 5 times!’ is closed to new replies.