Thread Starter
Goudie
(@goudie35)
Wow! I nailed it all by myself!
Using the documentation hook and a basic conditional. Here is the complete code used (in functions.php) to add the “Real price” option in the list:
add_filter('seopress_schemas_mapping_select', 'sp_schemas_mapping_select');
function sp_schemas_mapping_select($select) {
$select['Custom'] = [
'realprice' => __('Real price', 'wp-seopress-pro'),
];
return $select;
}
add_filter('seopress_schemas_dyn_variables', 'sp_schemas_dyn_variables');
function sp_schemas_dyn_variables($vars) {
$vars[] = 'realprice';
return $vars;
}
add_filter('seopress_schemas_dyn_variables_replace', 'sp_schemas_dyn_variables_replace');
function sp_schemas_dyn_variables_replace($values) {
global $product;
if( $product->is_on_sale() ) {
$values[] = $product->get_sale_price();
}
else {
$values[] = $product->get_regular_price();
}
return $values;
}
PS: Adding this default variable would be a good idea (maybe I missed something? 😳)
-
This reply was modified 3 years ago by
Goudie.
Thread Starter
Goudie
(@goudie35)
Oops… I see in my logs I have some PHP fatal errors (even if I can’t see them when browsing website 🙄)
PHP Fatal error: Uncaught Error: Call to a member function is_on_sale() on null ...
But it seems working fine … I’m confused
-
This reply was modified 3 years ago by
Goudie.
Thread Starter
Goudie
(@goudie35)
Unfortunately I can’t edit my previous posts. I understood that the problem appears when a product is added to the cart. Here is the code that seems works, however if it could be checked by the creator of the plugin (or someone who knows better than me)…
add_filter('seopress_schemas_mapping_select', 'sp_schemas_mapping_select');
function sp_schemas_mapping_select($select) {
$select['Custom'] = [
'currentprice' => __('Current price', 'wp-seopress-pro'),
];
return $select;
}
add_filter('seopress_schemas_dyn_variables', 'sp_schemas_dyn_variables');
function sp_schemas_dyn_variables($vars) {
$vars[] = 'currentprice';
return $vars;
}
add_filter('seopress_schemas_dyn_variables_replace', 'sp_schemas_dyn_variables_replace');
function sp_schemas_dyn_variables_replace($values) {
global $product;
if ($product) {
if( $product->is_on_sale() ) {
$values[] = $product->get_sale_price();
}
else {
$values[] = $product->get_regular_price();
}
}
return $values;
}
Hi,
schemas is a premium feature.
To respect WordPress.org guidelines, please open a support ticket here (if it’s not already done):
https://www.seopress.org/account/your-tickets/
Thank you for understanding,