Performance: WooCommerce hooks for customization
This affects page load speed and interaction responsiveness by controlling when and how WooCommerce features are added or modified in the page rendering process.
<?php add_action('woocommerce_before_single_product', 'custom_function'); function custom_function() { echo '<div>Optimized content</div>'; } ?>
<?php add_action('woocommerce_before_single_product', 'custom_function'); function custom_function() { for ($i=0; $i<1000; $i++) { echo '<div>Extra content</div>'; } } ?>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Heavy HTML in hooks | Large DOM increase | Multiple reflows | High paint cost | [X] Bad |
| Minimal HTML in hooks | Small DOM increase | Single reflow | Low paint cost | [OK] Good |
| Unconditional asset enqueue | No DOM change | No reflows | Blocks rendering | [X] Bad |
| Conditional asset enqueue | No DOM change | No reflows | Non-blocking | [OK] Good |
add_action to add functions to action hooks.add_action with correct parameters; others use wrong functions or names.add_filter('woocommerce_product_get_price', function($price) { return $price * 0.9; });woocommerce_product_get_price modifies the product price before display.add_action('woocommerce_before_cart', 'display_message');
function display_message() {
echo 'Welcome to your cart!';
}add_action to add new content; filters modify existing data, so add_filter is not suitable here.Pass the test and this pattern moves unknown