Add custom text with sale dates after sale price in Woocommerce Single product












1















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 );
}









share|improve this question





























    1















    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 );
    }









    share|improve this question



























      1












      1








      1








      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 );
      }









      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 '18 at 1:19









      LoicTheAztec

      87.2k1364101




      87.2k1364101










      asked Nov 20 '18 at 23:00







      user10551357































          1 Answer
          1






          active

          oldest

          votes


















          0














          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.



          enter image description here






          share|improve this answer


























          • 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











          • @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











          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
          });


          }
          });














          draft saved

          draft discarded


















          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









          0














          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.



          enter image description here






          share|improve this answer


























          • 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











          • @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
















          0














          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.



          enter image description here






          share|improve this answer


























          • 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











          • @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














          0












          0








          0







          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.



          enter image description here






          share|improve this answer















          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.



          enter image description here







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 21 '18 at 11:51

























          answered Nov 21 '18 at 1:19









          LoicTheAztecLoicTheAztec

          87.2k1364101




          87.2k1364101













          • 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











          • @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











          • 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











          • 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


















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          'app-layout' is not a known element: how to share Component with different Modules

          android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

          WPF add header to Image with URL pettitions [duplicate]