The default list of variables (<http://docs.mainwp.com/available-wordpress-seo-yoast-variables/>) did not suffice, as there is no link with Woocommerce.
Luckely it is very easy to add some code to your custom theme. Here is how I did it:
-
Add a functions.php file to your (custom) theme (if it does not exist yet).
-
write your custom function which returns the value you want (i.c. the price of the product)
/**
* retrieve_price_replacement()
* Retrieves the price of the current product
*
* @return string
*/
function retrieve_price_replacement() {
// Check if the class exists, or we could get errors
if (class_exists('WC_Product')) {
$product = new WC_Product(get_the_ID());
if (isset($product->price)) {
return "{$product->price} EUR";
}
}
// Fallback: return empty string
return '';
}
- Register your custom function as a variable
/**
* register_custom_extra_replacements()
*
* Register extra SEO replacements for the page title
*/
function register_custom_extra_replacements() {
wpseo_register_var_replacement( '%%price%%', 'retrieve_price_replacement', 'advanced', 'Get the price for the loaded product' );
}
- Hook the register function on to the action “wpseo_register_extra_replacements”
add_action('wpseo_register_extra_replacements', 'register_custom_extra_replacements');
You can now easily add extra variables by adding them to the register_custom_extra_replacements() function.
All code above goes into the functions.php file.
Happy coding!





