In this tutorial, we will learn how to periodically retrieve the current gold price from an external data source and update the prices of the affected products in WooCommerce. We will use the MetalpriceAPI, a free API source that offers live and historical metal rates in over 150+ currencies.
Step 1: Sign up for a free API key at MetalpriceAPI
Before we can start retrieving the current gold price from MetalpriceAPI, we need to sign up for a free API key. Go to https://metalpriceapi.com/ ↗ and click on “GET FREE API KEY” on the homepage. Fill in the required details and submit the form to receive your API key via email.
Step 2: Install and activate the Code Snippets plugin
We will be using the Code Snippets plugin to add the code that will retrieve the current gold price from MetalpriceAPI and update the prices of the gold products in WooCommerce. The Code Snippets plugin allows us to add custom code snippets without modifying the theme files or the WordPress core.
To install the Code Snippets plugin, go to Plugins > Add New in the WordPress dashboard. Search for “Code Snippets” and click on “Install Now” and then “Activate”.
Step 3: Add the custom code snippet
Now we can add the custom code snippet that will retrieve the current gold price from MetalpriceAPI and update the prices of the gold products in WooCommerce. Go to Snippets > Add New in the WordPress dashboard. Give the snippet a name (e.g., “Update Gold Prices”) and paste the following code in the code editor:
// Change product price based on gold price
add_filter( 'woocommerce_product_get_price', 'update_product_price_based_on_gold_price', 10, 2 );
add_filter( 'woocommerce_product_variation_get_price', 'update_product_price_based_on_gold_price', 10, 2 );
add_filter( 'woocommerce_get_regular_price', 'update_product_price_based_on_gold_price', 10, 2 );
add_filter( 'woocommerce_get_sale_price', 'update_product_price_based_on_gold_price', 10, 2 );
function update_product_price_based_on_gold_price( $price, $product ) {
// Check if the product is a gold product
if ( has_term( 'gold', 'product_cat', $product->get_id() ) ) {
// Get the current market value of gold per gram from MetalpriceAPI
$api_url = 'https://api.metalpriceapi.com/v1/latest?base=USD';
$response = wp_remote_get( $api_url );
$body = wp_remote_retrieve_body( $response );
$data = json_decode( $body, true );
$gold_price = $data['rates']['XAU'];
// Get the weight of the gold from the product attributes
$weight = (float) get_post_meta( $product->get_id(), 'weight', true );
// Calculate the new price of the gold set
$new_price = ($weight * $gold_price);
// Set the new price as the product price
$price = $new_price;
}
return $price;
}
This code uses the MetalpriceAPI API to retrieve the current market value of gold per gram in USD. It then checks if the product is a gold product by checking if it has the “gold” term assigned to it. If it is a gold product, the code retrieves the weight of the gold from the product attributes and calculates the new price of the gold set based on the current gold price. Finally, it sets the new price as the product price.
Step 4: Save and activate the code snippet
After you have pasted the code snippet in the code editor, click on “Save Changes and Activate” to activate the code snippet. The new code snippet will now be added to the list of active code snippets.
Step 5: Test the code
To test the code, create a new gold product or edit an existing gold product in WooCommerce. Assign the “gold” term to the product and add a weight value in the product attributes. Save the product and check if the price has been updated based on the current gold price in MetalpriceAPI.
Conclusion
In this tutorial, we have learned how to periodically retrieve the current gold price from an external data source and update the prices of the affected products in WooCommerce. We have used the MetalpriceAPI, a free API source that offers live and historical metal rates in over 150+ currencies. By following these steps, you can keep your gold product prices up-to-date with the current market value of gold.