• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
WooCommerce a2z

WooCommerce a2z

WooCommerce

  • Home
  • Plugins
  • Blocks
  • Shortcodes
  • APIs
  • Classes
  • Files
  • Hooks
  • Sitemap
  • Blog
Home / APIs / _wc_save_product_price() – Save product price.

You appear to be a bot. Output may be restricted

Description

Save product price.

This is a private function (internal use ONLY) used until a data manipulation api is built.

Usage

_wc_save_product_price( $product_id, $regular_price, $sale_price, $date_from, $date_to );

Parameters

$product_id
( int ) required –
$regular_price
( float ) required –
$sale_price
( float ) optional –
$date_from
( string ) optional –
$date_to
( string ) optional –

Returns

void

Source

File name: woocommerce/includes/wc-deprecated-functions.php


Lines:

1 to 39 of 39
function _wc_save_product_price( $product_id, $regular_price, $sale_price = '', $date_from = '', $date_to = '' ) {
  wc_doing_it_wrong( '_wc_save_product_price()', 'This function is not for developer use and is deprecated.', '3.0' );

  $product_id    = absint( $product_id );
  $regular_price = wc_format_decimal( $regular_price );
  $sale_price    = '' === $sale_price ? '' : wc_format_decimal( $sale_price );
  $date_from     = wc_clean( $date_from );
  $date_to       = wc_clean( $date_to );

  update_post_meta( $product_id, '_regular_price', $regular_price );
  update_post_meta( $product_id, '_sale_price', $sale_price );

  // Save Dates
  update_post_meta( $product_id, '_sale_price_dates_from', $date_from ? strtotime( $date_from ) : '' );
  update_post_meta( $product_id, '_sale_price_dates_to', $date_to ? strtotime( $date_to ) : '' );

  if ( $date_to && ! $date_from ) {
    $date_from = strtotime( 'NOW', current_time( 'timestamp' ) );
    update_post_meta( $product_id, '_sale_price_dates_from', $date_from );
  }

  // Update price if on sale
  if ( '' !== $sale_price && '' === $date_to && '' === $date_from ) {
    update_post_meta( $product_id, '_price', $sale_price );
  } else {
    update_post_meta( $product_id, '_price', $regular_price );
  }

  if ( '' !== $sale_price && $date_from && strtotime( $date_from ) < strtotime( 'NOW', current_time( 'timestamp' ) ) ) {
    update_post_meta( $product_id, '_price', $sale_price );
  }

  if ( $date_to && strtotime( $date_to ) < strtotime( 'NOW', current_time( 'timestamp' ) ) ) {
    update_post_meta( $product_id, '_price', $regular_price );
    update_post_meta( $product_id, '_sale_price_dates_from', '' );
    update_post_meta( $product_id, '_sale_price_dates_to', '' );
  }
}
 

 View on GitHub View on Trac

Published: 6th April 2020 | Last updated: 21st August 2020

Primary Sidebar

Information

Function name: _wc_save_product_price
Plugin ref: WooCommerce
Version: 5.1.0
Sourcefile: includes/wc-deprecated-functions.php
File ref: includes/wc-deprecated-functions.php
API type: private
Deprecated?: No
API Letters: _,P,S,W

Footer

WooCommerce a2z
WooCommerce a2z
WooCommerce
WordPress 5.7
WordPress a2z
WordPress core a2z
Genesis Theme Framework a2z
Jetpack a2z
WordPress develop tests
Easy Digital Downloads a2z
WooCommerce a2z
Yoast SEO a2z
WordPress Blocks

Site:  woocommerce.wp-a2z.org
© Copyright WooCommerce a2z 2014-2021. All rights reserved.


Website designed and developed by Herb Miller
Proudly powered by WordPress and oik plugins

  • Home
  • Blog
  • Sitemap
  • Sites