WooCommerce: Pull Prices from External Database

No modifications/hacks are needed for WooCommerce to do this as it just uses a single add_filter & function to do so (though there are certain conditions that need to be met). Essentially, the product needs to have a price that’s a unique number (products with SKUs that contain unique numbers lends themselves to this nicely). In my case, there’s a product with an SKU of “LA-2400”, but I just care about the 2400 (no other products share that number). I set the price for that product in WooCommerce as “2400”. Then I have a function of run_shortcodes_on_prices() in my theme’s functions.php file. This is executed via add_filter('woocommerce_get_price','run_shortcodes_on_prices');. The function contains code that then sees the unique id prices, pulls the matching product’s price from the database, and returns that value. Here’s my example of this: function run_shortcodes_on_prices($price){ // The prices within WooCommerce are set to the unique numbers from the product SKUs, and then the corresponding shortcode is run to retrieve the price for that product from the external database. switch($price){ case '2400': return do_shortcode('[price product="mini"]'); break; case '6200': return do_shortcode('[price product="regular"]'); break; case '9000': return do_shortcode('[price product="max"]'); break; } } add_filter('woocommerce_get_price','run_shortcodes_on_prices'); This requires that there’s a shortcode that was set up to pull the price from the database based on the product name it was given (though the database call can be made right from this function if the shortcode isn’t needed for use elsewhere). That’s all there is to it! The external price is shown on the store pages, site admin, shopping cart, and the final checkout price. Meanwhile, the unique id number is still shown when editing the product price in the site admin. Originally, a new filter was proposed and modifying a function was considered (see pull request on GitHub), but this method works perfectly without any changes to the WooCommerce code.

2 responses to “WooCommerce: Pull Prices from External Database”

  1. Still do not understand so where do short-codes go ?

    and what i need to pull from the external database ?

    does the regular price matter ?

    1. They’d just go in the regular price field. The shortcode in the price field then gets executed & used as the resulting price.

      Then again, this is from a few years back and WooCommerce has changed quite a bit so some modifications to this proposed setup may be necessary.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.