Add custom text with sale dates after sale price in Woocommerce Single product
I have this code which adds a text after the sale price and it's generating an error in the log saying "doing it wrong" even though there is no PHP error in the functions file.
Any idea why it's "complaining" ? Here's the code:
add_filter( 'woocommerce_get_price_html', 'sale_dates_after_price', 100, 2 );
function sale_dates_after_price( $price, $product ) {
$sales_price_from = get_post_meta( $product->id, '_sale_price_dates_from', true );
$sales_price_to = get_post_meta( $product->id, '_sale_price_dates_to', true );
if ( is_single() && $product->is_on_sale() && $sales_price_to != "" ) {
$sales_price_date_from = date( "j M y", $sales_price_from );
$sales_price_date_to = date( "j M y", $sales_price_to );
$price = str_replace( '</ins>', ' </ins> <span class="sale-dates"><b>offer valid between ' . $sales_price_date_from . ' and ' . $sales_price_date_to . '</b></span>', $price );
}
return apply_filters( 'woocommerce_get_price', $price );
}
php wordpress woocommerce product price
add a comment |
I have this code which adds a text after the sale price and it's generating an error in the log saying "doing it wrong" even though there is no PHP error in the functions file.
Any idea why it's "complaining" ? Here's the code:
add_filter( 'woocommerce_get_price_html', 'sale_dates_after_price', 100, 2 );
function sale_dates_after_price( $price, $product ) {
$sales_price_from = get_post_meta( $product->id, '_sale_price_dates_from', true );
$sales_price_to = get_post_meta( $product->id, '_sale_price_dates_to', true );
if ( is_single() && $product->is_on_sale() && $sales_price_to != "" ) {
$sales_price_date_from = date( "j M y", $sales_price_from );
$sales_price_date_to = date( "j M y", $sales_price_to );
$price = str_replace( '</ins>', ' </ins> <span class="sale-dates"><b>offer valid between ' . $sales_price_date_from . ' and ' . $sales_price_date_to . '</b></span>', $price );
}
return apply_filters( 'woocommerce_get_price', $price );
}
php wordpress woocommerce product price
add a comment |
I have this code which adds a text after the sale price and it's generating an error in the log saying "doing it wrong" even though there is no PHP error in the functions file.
Any idea why it's "complaining" ? Here's the code:
add_filter( 'woocommerce_get_price_html', 'sale_dates_after_price', 100, 2 );
function sale_dates_after_price( $price, $product ) {
$sales_price_from = get_post_meta( $product->id, '_sale_price_dates_from', true );
$sales_price_to = get_post_meta( $product->id, '_sale_price_dates_to', true );
if ( is_single() && $product->is_on_sale() && $sales_price_to != "" ) {
$sales_price_date_from = date( "j M y", $sales_price_from );
$sales_price_date_to = date( "j M y", $sales_price_to );
$price = str_replace( '</ins>', ' </ins> <span class="sale-dates"><b>offer valid between ' . $sales_price_date_from . ' and ' . $sales_price_date_to . '</b></span>', $price );
}
return apply_filters( 'woocommerce_get_price', $price );
}
php wordpress woocommerce product price
I have this code which adds a text after the sale price and it's generating an error in the log saying "doing it wrong" even though there is no PHP error in the functions file.
Any idea why it's "complaining" ? Here's the code:
add_filter( 'woocommerce_get_price_html', 'sale_dates_after_price', 100, 2 );
function sale_dates_after_price( $price, $product ) {
$sales_price_from = get_post_meta( $product->id, '_sale_price_dates_from', true );
$sales_price_to = get_post_meta( $product->id, '_sale_price_dates_to', true );
if ( is_single() && $product->is_on_sale() && $sales_price_to != "" ) {
$sales_price_date_from = date( "j M y", $sales_price_from );
$sales_price_date_to = date( "j M y", $sales_price_to );
$price = str_replace( '</ins>', ' </ins> <span class="sale-dates"><b>offer valid between ' . $sales_price_date_from . ' and ' . $sales_price_date_to . '</b></span>', $price );
}
return apply_filters( 'woocommerce_get_price', $price );
}
php wordpress woocommerce product price
php wordpress woocommerce product price
edited Nov 21 '18 at 1:19
LoicTheAztec
85k136095
85k136095
asked Nov 20 '18 at 23:00
user10551357
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Your code is outdated with some errors and mistakes… As you are using the formatted sale price, there is a much better hook for that. Try the following:
add_filter( 'woocommerce_format_sale_price', 'sale_dates_after_price', 10, 3 );
function sale_dates_after_price ( $price, $regular_price, $sale_price ) {
global $product;
if( ! is_a($product, 'WC_Product') )
return $price;
$date_from = $product->get_date_on_sale_from();
$date_to = $product->get_date_on_sale_to();
if( is_product() && ! ( empty($date_from) && empty($date_from) ) ) {
$date_from = date( "j M y", strtotime($date_from) );
$date_to = date( "j M y", strtotime($date_to) );
$price .= ' <span class="sale-dates"><strong>offer valid between '.$date_from.' and '.$date_to.'</strong></span>';
}
return $price;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
thank you @LoicTheAztec! Thank you.
– user10551357
Nov 21 '18 at 10:36
after updating and going to cart it's complaining about this line:$date_from = $product->get_date_on_sale_from();
and saying that it's aFatal error: Uncaught Error: Call to a member function get_date_on_sale_from() on null
. Any ideas why?
– user10551357
Nov 21 '18 at 11:15
@WooDevil I have updated the code
– LoicTheAztec
Nov 21 '18 at 11:52
thank you, very kind. Working fine now.
– user10551357
Nov 21 '18 at 11:58
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402889%2fadd-custom-text-with-sale-dates-after-sale-price-in-woocommerce-single-product%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your code is outdated with some errors and mistakes… As you are using the formatted sale price, there is a much better hook for that. Try the following:
add_filter( 'woocommerce_format_sale_price', 'sale_dates_after_price', 10, 3 );
function sale_dates_after_price ( $price, $regular_price, $sale_price ) {
global $product;
if( ! is_a($product, 'WC_Product') )
return $price;
$date_from = $product->get_date_on_sale_from();
$date_to = $product->get_date_on_sale_to();
if( is_product() && ! ( empty($date_from) && empty($date_from) ) ) {
$date_from = date( "j M y", strtotime($date_from) );
$date_to = date( "j M y", strtotime($date_to) );
$price .= ' <span class="sale-dates"><strong>offer valid between '.$date_from.' and '.$date_to.'</strong></span>';
}
return $price;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
thank you @LoicTheAztec! Thank you.
– user10551357
Nov 21 '18 at 10:36
after updating and going to cart it's complaining about this line:$date_from = $product->get_date_on_sale_from();
and saying that it's aFatal error: Uncaught Error: Call to a member function get_date_on_sale_from() on null
. Any ideas why?
– user10551357
Nov 21 '18 at 11:15
@WooDevil I have updated the code
– LoicTheAztec
Nov 21 '18 at 11:52
thank you, very kind. Working fine now.
– user10551357
Nov 21 '18 at 11:58
add a comment |
Your code is outdated with some errors and mistakes… As you are using the formatted sale price, there is a much better hook for that. Try the following:
add_filter( 'woocommerce_format_sale_price', 'sale_dates_after_price', 10, 3 );
function sale_dates_after_price ( $price, $regular_price, $sale_price ) {
global $product;
if( ! is_a($product, 'WC_Product') )
return $price;
$date_from = $product->get_date_on_sale_from();
$date_to = $product->get_date_on_sale_to();
if( is_product() && ! ( empty($date_from) && empty($date_from) ) ) {
$date_from = date( "j M y", strtotime($date_from) );
$date_to = date( "j M y", strtotime($date_to) );
$price .= ' <span class="sale-dates"><strong>offer valid between '.$date_from.' and '.$date_to.'</strong></span>';
}
return $price;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
thank you @LoicTheAztec! Thank you.
– user10551357
Nov 21 '18 at 10:36
after updating and going to cart it's complaining about this line:$date_from = $product->get_date_on_sale_from();
and saying that it's aFatal error: Uncaught Error: Call to a member function get_date_on_sale_from() on null
. Any ideas why?
– user10551357
Nov 21 '18 at 11:15
@WooDevil I have updated the code
– LoicTheAztec
Nov 21 '18 at 11:52
thank you, very kind. Working fine now.
– user10551357
Nov 21 '18 at 11:58
add a comment |
Your code is outdated with some errors and mistakes… As you are using the formatted sale price, there is a much better hook for that. Try the following:
add_filter( 'woocommerce_format_sale_price', 'sale_dates_after_price', 10, 3 );
function sale_dates_after_price ( $price, $regular_price, $sale_price ) {
global $product;
if( ! is_a($product, 'WC_Product') )
return $price;
$date_from = $product->get_date_on_sale_from();
$date_to = $product->get_date_on_sale_to();
if( is_product() && ! ( empty($date_from) && empty($date_from) ) ) {
$date_from = date( "j M y", strtotime($date_from) );
$date_to = date( "j M y", strtotime($date_to) );
$price .= ' <span class="sale-dates"><strong>offer valid between '.$date_from.' and '.$date_to.'</strong></span>';
}
return $price;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Your code is outdated with some errors and mistakes… As you are using the formatted sale price, there is a much better hook for that. Try the following:
add_filter( 'woocommerce_format_sale_price', 'sale_dates_after_price', 10, 3 );
function sale_dates_after_price ( $price, $regular_price, $sale_price ) {
global $product;
if( ! is_a($product, 'WC_Product') )
return $price;
$date_from = $product->get_date_on_sale_from();
$date_to = $product->get_date_on_sale_to();
if( is_product() && ! ( empty($date_from) && empty($date_from) ) ) {
$date_from = date( "j M y", strtotime($date_from) );
$date_to = date( "j M y", strtotime($date_to) );
$price .= ' <span class="sale-dates"><strong>offer valid between '.$date_from.' and '.$date_to.'</strong></span>';
}
return $price;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
edited Nov 21 '18 at 11:51
answered Nov 21 '18 at 1:19
LoicTheAztec
85k136095
85k136095
thank you @LoicTheAztec! Thank you.
– user10551357
Nov 21 '18 at 10:36
after updating and going to cart it's complaining about this line:$date_from = $product->get_date_on_sale_from();
and saying that it's aFatal error: Uncaught Error: Call to a member function get_date_on_sale_from() on null
. Any ideas why?
– user10551357
Nov 21 '18 at 11:15
@WooDevil I have updated the code
– LoicTheAztec
Nov 21 '18 at 11:52
thank you, very kind. Working fine now.
– user10551357
Nov 21 '18 at 11:58
add a comment |
thank you @LoicTheAztec! Thank you.
– user10551357
Nov 21 '18 at 10:36
after updating and going to cart it's complaining about this line:$date_from = $product->get_date_on_sale_from();
and saying that it's aFatal error: Uncaught Error: Call to a member function get_date_on_sale_from() on null
. Any ideas why?
– user10551357
Nov 21 '18 at 11:15
@WooDevil I have updated the code
– LoicTheAztec
Nov 21 '18 at 11:52
thank you, very kind. Working fine now.
– user10551357
Nov 21 '18 at 11:58
thank you @LoicTheAztec! Thank you.
– user10551357
Nov 21 '18 at 10:36
thank you @LoicTheAztec! Thank you.
– user10551357
Nov 21 '18 at 10:36
after updating and going to cart it's complaining about this line:
$date_from = $product->get_date_on_sale_from();
and saying that it's a Fatal error: Uncaught Error: Call to a member function get_date_on_sale_from() on null
. Any ideas why?– user10551357
Nov 21 '18 at 11:15
after updating and going to cart it's complaining about this line:
$date_from = $product->get_date_on_sale_from();
and saying that it's a Fatal error: Uncaught Error: Call to a member function get_date_on_sale_from() on null
. Any ideas why?– user10551357
Nov 21 '18 at 11:15
@WooDevil I have updated the code
– LoicTheAztec
Nov 21 '18 at 11:52
@WooDevil I have updated the code
– LoicTheAztec
Nov 21 '18 at 11:52
thank you, very kind. Working fine now.
– user10551357
Nov 21 '18 at 11:58
thank you, very kind. Working fine now.
– user10551357
Nov 21 '18 at 11:58
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402889%2fadd-custom-text-with-sale-dates-after-sale-price-in-woocommerce-single-product%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown