Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Conditionally run function based on custom meta value?

Instead of conditionally adding an add_action() call, you can just add it and run the function it hooks to—its callback—conditionally instead.

For example:

add_action( 'woocommerce_product_additional_information', 'wpse407073_callback' );
add_action( 'woocommerce_after_shop_loop_item', 'wpse407073_callback' );

function wpse407073_callback( $product = null ) {
    if ( empty( $product ) ) {
        global $post;
        $product = $post;
    }
    if ( 'X' === get_post_meta( $product->ID, 'my-custom-meta-field', true ) ) {
        echo '<img src="image.jpg" alt="Image Name">';
    }
}

A few points:

  • Having looked quickly at WooCommerce’s code, woocommerce_after_shop_loop_item doesn’t pass anything to the callback, and woocommerce_product_additional_information passed 1 parameter, $product, which I’m assuming is a custom post. That’s why I added a check for an empty $product in the callback.
  • This code hasn’t been tested, and is meant as a possible starting point, not a finished product.
  • If you have questions about WooCommerce’s hooks, you should check with WooCommerce’s documentation and/or their support team.

Related Posts:

  1. How to update custom field in WooCommerce
  2. How to add custom checkboxes from loop to WooCommerce product variation options?
  3. acf_form() – “On Update” action [closed]
  4. WooCommerce: Can’t use wc_get_products for custom REST API endpoints
  5. How to get rid of the hover zoom in WooCommerce single products
  6. What’s the difference between WC() and $woocommerce
  7. Hook and send Woocommerce data after click Place Order button
  8. WooCommerce conditional meta query
  9. WooCommerce: How to display item meta data in email confirmation using woocommerce_order_item_meta_end
  10. Get updated meta data after save_post hook
  11. Show only geolocated user country into Woocommerce checkout country fields
  12. Better post meta efficiency?
  13. post meta data clearing on autosave
  14. Extend Woocommerce rest api routes fails
  15. Display info from custom fields in all images’ HTML
  16. Update meta values with AJAX
  17. Remove action on product archive page [closed]
  18. The correct way to override WooCommerce product gallery from a plugin [closed]
  19. display available size on hover, in woocommerce shop page [closed]
  20. Customize WooCommerce orders displayed shipping
  21. How can I show an empty WooCommerce cart? [closed]
  22. Is there a Woocommerce hook that fires when applying a coupon but before checking if it’s valid?
  23. What is the action hook for an order that fails on frontend checkout in WooCommerce?
  24. How to unhook a function in Woocommerce Template?
  25. WooCommerce: Adding Order Item Meta Data That’s Hidden
  26. Using a filter with multiple parameters and $this
  27. Remove Order List Row Link in WooCommerce Admin?
  28. Get rid of product images mobile swipe functionality from WooCommerce single product [closed]
  29. Which hook should I use to capture $_POST(‘password’) via profile update and password reset
  30. Conditionally hide or show woocommerce product variation in fontend by custom field
  31. Edit WooCommerce product content based on category
  32. Bulk remove post meta
  33. How to create html block to display extra information on woocommerce single product page
  34. woocommerce_package_rates not fired everytime
  35. differentiate hooks and filters on the same page [closed]
  36. WP_Query with meta_query for children
  37. ‘profile_update’ hook alternative for WooCommerce user meta data
  38. How to add a user custom field into Woocommerce emails?
  39. Woocommerce custom meta fields, sort by date
  40. How to VAR_DUMP a $variable during checkout process (Is my product meta callable?)
  41. Add Stripe Connect data to wc_stripe_payment_request
  42. Redirect customer to login page (with other signup plugin) if user not logged in when proceeding to checkout
  43. How to subscribe free subscription on user registration in woocommerce subscription plugin?
  44. How to list all WC products by their attributes values?
  45. Showing the WordPress standard text area on a Woocommerce Single Product Page?
  46. Woocommerce Moving Review Below add to cart breaks button
  47. How to upload multiple image using wp_insert_post
  48. WooCommerce order complete email logic
  49. Set default value field meta_Value [closed]
  50. How to override Woocommerce functionality in Reports section?
  51. how can use woocommerce add to cart popup?
  52. How to Update the Order-Items While Editing an Order on the WooCommerce Admin Order Screen [closed]
  53. WooCommerce Hook: Content get’s displayed twice and not AFTER the order total
  54. Is it possible to update the total price on the checkout page in woocommerce from a custom field
  55. New field on checkout is shown but it’s not saved on the order details [closed]
  56. Adding Product Name on Admin Panel Order list and User’s My-Account Order List
  57. How to re-arrange this hooked content?
  58. update_post_meta not work [closed]
  59. How to exclude a taxonomy from shop & search page wooCommerce?
  60. WooCommerce order refund get qty refunded
  61. Redirect no product url’s to static url
  62. Adding a custom Shipping methods field to an order
  63. What Hook/Action is performed when a field in checkout form is changed in WooCommerce
  64. How to change a WooCommerce Subscription Deposit and Monthly Payment? [closed]
  65. How can I add a so i can show sale price under normal price
  66. password_reset doesnt work
  67. WooCommerce checkout page price break down table hook
  68. How to output woocommerce products to a page, style and modify the html structure
  69. Filter default_content only for products
  70. Woocommerce – Adding row to cart table
  71. Conditional action hook
  72. How can I override wp_price woocommerce function in my theme
  73. WooCommerce Bookings Plugin Not Saving Order ID
  74. Add a custom class to the body tag using custom fields
  75. manage_shop_order_posts_custom_column() hook is Not working In Functions.php file in my WordPress active theme
  76. Display custom product option in admin order details
  77. Next/Previous Product with custom order by price & products inside the product category [closed]
  78. WooCommerce Meta
  79. How to alter taxes amounts when updating an order
  80. How to check if woocommerce_order_item_name is use in email?
  81. Hooks For Design variation in woo commerce single product page
  82. How to add woocommerce products header title in woocommerce_breadcrumb hook to function.php
  83. Use ID as slug for product category by default in WooCommerce?
  84. What is the problem with these lines of code?
  85. Can a woocommerce hook alter the UI of a website?
  86. Order shipped by which driver[hook for woocoomerce order staus changed and popup in admin panel ] [closed]
  87. Updating order meta to have a meta entry from an item in the order
  88. Insert a custom field as a second email address to a completed order in WooCommerce
  89. dakon show vender address on checkout page
  90. Hide a shipping method if cart total is higher than an amount
  91. How can i hide a specific class when cart subtotal is above a certain amount
  92. Add second price option based on payment method at cart item price
  93. Use a woocommerce hook to create a table and populate it with a query
  94. Custom fields in the billing address section woocommerce
  95. Woocommerce send metadata to stripe
  96. How to modify username before logging in?
  97. How to add field new in account bacs in woocommerce?
  98. WooCommerce Webhook Action When a New Product Review was Submitted/Created
  99. Woocommerce: hide “Price display suffix” when the VAT is zero
  100. Link post thumbnail image when automatically creating Woocommerce product from a WordPress post
Categories woocommerce-offtopic Tags conditional-content, custom-field, hooks, post-meta, woocommerce-offtopic
What’s more important for supporting concurrent users on an elearning site with dynamic content, CPUs or RAM?
wp.media Uncaught TypeError: Cannot read properties of undefined (reading ‘state’)

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Post Navigation Elementor
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • WPFacet multiple loop displaying duplicate content
  • Get the name of the template/*html file used
  • Fix: WordPress.DB.PreparedSQL.NotPrepared Plugin Check (PCP)
  • Removing unnecessary CSS and JS code from wp_head()
  • hexagonal image gallery. Am I on the right track? [closed]
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress