How to Customize WooCommerce Store with Hooks

Contents

Introduction

WooCommerce is a powerful e-commerce plugin for WordPress, offering extensive functionality out of the box. Yet every store has unique requirements: custom product displays, specialized checkout fields, bespoke email templates, or integration with third-party services. Achieving these customizations without modifying core files relies on WordPress hooks—actions and filters.

This article explores how to leverage WooCommerce hooks to tailor your store’s behavior and appearance. We’ll cover:

  • Understanding actions vs. filters
  • Locating available WooCommerce hooks
  • Common customization examples
  • Best practices and performance considerations

Understanding Actions and Filters

Actions let you add or execute custom code at specified points.
Filters allow you to modify data before it’s rendered or saved.

Action Hook Syntax


add_action(woocommerce_before_shop_loop, my_custom_function, 10)
function my_custom_function() {
  echo ltdiv class=noticegtWelcome to our sale!lt/divgt
}
  

Filter Hook Syntax


add_filter(woocommerce_product_get_price, adjust_product_price, 10, 2)
function adjust_product_price(price, product) {
  return price  0.9 // 10% discount
}
  

Locating WooCommerce Hooks

WooCommerce core files, templates, and documentation list hundreds of hooks. Two primary resources:

Alternatively, search template files in wp-content/plugins/woocommerce/templates for do_action and apply_filters calls.

Practical Customization Examples

1. Customize the Product Loop

Add a badge to sale items in the shop page:


add_action(woocommerce_before_shop_loop_item_title, add_sale_badge, 15)
function add_sale_badge() {
  global product
  if (product-gtis_on_sale()) {
    echo ltspan style=background:#e54a4acolor:#fffpadding:3px 6pxfont-size:0.9emborder-radius:3pxgtSale!lt/spangt
  }
}
  

2. Modify Checkout Fields

Reorder and rename billing fields:


add_filter(woocommerce_checkout_fields, customize_checkout_fields)
function customize_checkout_fields(fields) {
  // Change label and priority
  fields[billing][billing_phone][label] = Mobile Number
  fields[billing][billing_phone][priority] = 25

  // Remove company field
  unset(fields[billing][billing_company])

  return fields
}
  

3. Add a Custom Fee on Checkout


add_action(woocommerce_cart_calculate_fees, add_custom_handling_fee)
function add_custom_handling_fee() {
  if (is_admin() ampamp !defined(DOING_AJAX)) return
  fee = 5
  WC()->cart-gtadd_fee(Handling Fee, fee)
}
  

4. Customize Email Templates

Inject a message into order emails:


add_action(woocommerce_email_after_order_table, add_email_promo)
function add_email_promo(order, sent_to_admin, plain_text, email) {
  echo ltp style=font-style:italiccolor:#555gtThank you for your purchase! Use code NEXT10 for 10% off next order.lt/pgt
}
  

Advanced Techniques

Removing Core Hooks

To disable a default feature, remove its hook. Example: remove related products:


remove_action(woocommerce_after_single_product_summary, woocommerce_output_related_products, 20)
  

Using Priority to Control Execution Order

Lower priority numbers run earlier. Leverage this to ensure your code runs before or after other plugins:


add_action(woocommerce_before_checkout_form, early_notice, 5)
add_action(woocommerce_before_checkout_form, late_notice, 50)
  

Best Practices

  • Use a child theme or custom plugin: Never modify WooCommerce core or parent theme files directly.
  • Prefix your functions: Avoid naming collisions by using a unique prefix, e.g., acme_.
  • Sanitize and validate: Always sanitize input/output, especially on checkout or user-submitted data.
  • Optimize performance: Avoid heavy database queries or loops inside hooks that run frequently.
  • Document your code: Add inline comments and maintain a changelog for future reference.

Hook Reference Table

Hook Type Description
woocommerce_before_shop_loop Action Executes before the product loop on archive/shop pages.
woocommerce_after_single_product_summary Action Renders after the single product summary (tabs, upsells, related).
woocommerce_product_get_price Filter Modifies the product’s price before display.
woocommerce_cart_calculate_fees Action Allows adding custom fees during cart calculations.

Further Reading

© 2024 Your Company Name. All rights reserved.



Acepto donaciones de BAT's mediante el navegador Brave 🙂



Leave a Reply

Your email address will not be published. Required fields are marked *