Skip to content

Instantly share code, notes, and snippets.

@emulsion-io
Created October 15, 2025 09:26
Show Gist options
  • Save emulsion-io/7ce3519c3b33092defeff59207fe6280 to your computer and use it in GitHub Desktop.
Save emulsion-io/7ce3519c3b33092defeff59207fe6280 to your computer and use it in GitHub Desktop.
Allows setting prices not updated by the WP cron.
<?php /* Template Name: Debug produit */ ?>
<?php
/**
* Allows setting prices not updated by the WP cron.
*
* The script runs a loop of 1,000 items at a time to display the promotional price on the correct date.
*
* The second part does the opposite — it removes the promotional price on the correct date.
*
*/
$data_store = WC_Data_Store::load( 'product' );
/*
$product_ids = $data_store->get_starting_sales();
echo "Produit a mettre en promo : " . count($product_ids) . '<br>';
$product_ids = $data_store->get_ending_sales();
echo "Produit a retire de la promo : " . count($product_ids) . '<br>';
*/
// exit;
// Sales which are due to start.
$product_ids = $data_store->get_starting_sales();
echo count($product_ids) . '<br>';
//exit;
if ( $product_ids ) {
do_action( 'wc_before_products_starting_sales', $product_ids );
$counter = 0;
foreach ( $product_ids as $product_id ) {
if ($counter >= 1000) {
break;
}
$product = wc_get_product( $product_id );
if ( $product ) {
$sale_price = $product->get_sale_price();
if ( $sale_price ) {
$product->set_price( $sale_price );
$product->set_date_on_sale_from( '' );
} else {
$product->set_date_on_sale_to( '' );
$product->set_date_on_sale_from( '' );
}
$product->save();
}
$counter++;
}
do_action( 'wc_after_products_starting_sales', $product_ids );
WC_Cache_Helper::get_transient_version( 'product', true );
delete_transient( 'wc_products_onsale' );
}
// Sales which are due to end.
$product_ids = $data_store->get_ending_sales();
echo count($product_ids) . '<br>';
//exit;
if ( $product_ids ) {
do_action( 'wc_before_products_ending_sales', $product_ids );
$counter = 0;
foreach ( $product_ids as $product_id ) {
if ($counter >= 1000) {
break;
}
$product = wc_get_product( $product_id );
if ( $product ) {
$regular_price = $product->get_regular_price();
$product->set_price( $regular_price );
$product->set_sale_price( '' );
$product->set_date_on_sale_to( '' );
$product->set_date_on_sale_from( '' );
$product->save();
}
$counter++;
}
do_action( 'wc_after_products_ending_sales', $product_ids );
WC_Cache_Helper::get_transient_version( 'product', true );
delete_transient( 'wc_products_onsale' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment