تغییر تاریخ پایان فروش ویژه محصولات ووکامرسی به صورت یکجا
/**
* BATCH PROCESSING SCRIPT (NO LOGGING): Updates WooCommerce sale dates for SIMPLE and VARIABLE products
* from 2025-05-29 to 2025-05-30. Processes in batches.
* Runs until all products are processed.
*/
function batch_no_log_update_all_product_sale_dates() {
$script_option_prefix = 'batch_no_log_update_29_to_30_may_2025'; // Prefix for all options
$batch_size = 50; // Number of products per batch
// 1. Check if overall process is already complete
$overall_complete_option = $script_option_prefix . '_overall_complete';
if (get_option($overall_complete_option) === 'yes') {
return; // Already done
}
// 2. Check for WooCommerce
if (!function_exists('wc_get_products')) {
return; // WooCommerce not active
}
// 3. Define Dates (UTC Timestamps)
$target_day_start_timestamp_utc = strtotime('2025-05-29 00:00:00');
$target_day_end_timestamp_utc = strtotime('2025-05-29 23:59:59');
$new_date_sale_to_utc_timestamp = strtotime('2025-05-30 23:59:59');
$simple_complete_option = $script_option_prefix . '_simple_complete';
$simple_offset_option = $script_option_prefix . '_simple_offset';
$variable_complete_option = $script_option_prefix . '_variable_complete';
$variable_offset_option = $script_option_prefix . '_variable_offset';
// --- Stage 1: Process Simple Products ---
$simple_products_complete = get_option($simple_complete_option, 'no');
if ($simple_products_complete !== 'yes') {
$current_offset_simple = (int) get_option($simple_offset_option, 0);
$simple_products_args = [
'type' => 'simple',
'limit' => $batch_size,
'offset' => $current_offset_simple,
'status' => 'publish',
'orderby'=> 'ID',
'order' => 'ASC',
];
$simple_products = wc_get_products($simple_products_args);
if (empty($simple_products)) {
update_option($simple_complete_option, 'yes');
$simple_products_complete = 'yes';
} else {
foreach ($simple_products as $product) {
/** @var WC_Product_Simple $product */
$date_on_sale_to_obj = $product->get_date_on_sale_to('edit');
$current_sale_to_ts_utc = 0;
if ($date_on_sale_to_obj instanceof WC_DateTime) {
$current_sale_to_ts_utc = $date_on_sale_to_obj->getTimestamp();
} else {
continue;
}
$raw_sale_price_meta = $product->get_meta('_sale_price', true); // Still need this for the condition
$has_sale_price = ($raw_sale_price_meta !== '' && $raw_sale_price_meta !== null);
$has_valid_current_sale_end_date_ts = ($current_sale_to_ts_utc > 0);
$is_target_date = ($current_sale_to_ts_utc >= $target_day_start_timestamp_utc && $current_sale_to_ts_utc <= $target_day_end_timestamp_utc);
if ($has_sale_price && $has_valid_current_sale_end_date_ts && $is_target_date) {
$product->set_date_on_sale_to($new_date_sale_to_utc_timestamp);
$product->save();
}
}
update_option($simple_offset_option, $current_offset_simple + count($simple_products));
wp_cache_flush(); // Flush cache after a batch
return; // Exit after processing one batch of simple products
}
}
// --- Stage 2: Process Variable Products ---
$variable_products_complete = get_option($variable_complete_option, 'no');
if ($simple_products_complete === 'yes' && $variable_products_complete !== 'yes') {
$current_offset_variable = (int) get_option($variable_offset_option, 0);
$variable_products_args = [
'type' => 'variable',
'limit' => $batch_size,
'offset' => $current_offset_variable,
'status' => 'publish',
'orderby'=> 'ID',
'order' => 'ASC',
];
$variable_products = wc_get_products($variable_products_args);
if (empty($variable_products)) {
update_option($variable_complete_option, 'yes');
$variable_products_complete = 'yes';
} else {
foreach ($variable_products as $product_var_parent) {
/** @var WC_Product_Variable $product_var_parent */
$variations = $product_var_parent->get_children();
if (empty($variations)) {
continue;
}
foreach ($variations as $variation_id) {
$variation_obj = wc_get_product($variation_id);
if (!$variation_obj || !$variation_obj->is_purchasable()) {
continue;
}
/** @var WC_Product_Variation $variation_obj */
$raw_var_sale_price_meta = $variation_obj->get_meta('_sale_price', true);
$var_date_on_sale_to_obj = $variation_obj->get_date_on_sale_to('edit');
$current_var_sale_to_ts_utc = 0;
if ($var_date_on_sale_to_obj instanceof WC_DateTime) {
$current_var_sale_to_ts_utc = $var_date_on_sale_to_obj->getTimestamp();
} else {
continue;
}
$var_has_sale_price = ($raw_var_sale_price_meta !== '' && $raw_var_sale_price_meta !== null);
$var_has_valid_current_sale_end_date_ts = ($current_var_sale_to_ts_utc > 0);
$var_is_target_date = ($current_var_sale_to_ts_utc >= $target_day_start_timestamp_utc && $current_var_sale_to_ts_utc <= $target_day_end_timestamp_utc);
if ($var_has_sale_price && $var_has_valid_current_sale_end_date_ts && $var_is_target_date) {
$variation_obj->set_date_on_sale_to($new_date_sale_to_utc_timestamp);
$variation_obj->save();
}
}
}
update_option($variable_offset_option, $current_offset_variable + count($variable_products));
wp_cache_flush(); // Flush cache after a batch
return; // Exit after processing one batch of variable products (parents)
}
}
// --- Stage 3: Finalization ---
if ($simple_products_complete === 'yes' && $variable_products_complete === 'yes') {
update_option($overall_complete_option, 'yes');
update_option($overall_complete_option . '_timestamp', date('Y-m-d H:i:s T'));
// Optionally delete offset and stage-complete options
delete_option($simple_complete_option);
delete_option($simple_offset_option);
delete_option($variable_complete_option);
delete_option($variable_offset_option);
}
wp_cache_flush();
}
// Ensure this is the ONLY action hook for this purpose. Remove/comment out others.
add_action('wp_loaded', 'batch_no_log_update_all_product_sale_dates');
توضیحات:
در صورتی که تعدادی از محصولات شما تاریخ پایان فروش ویژه خاصی دارند (به طور مثال در تاریخ 29/5) و میخواهید تمام محصولات را به صورت خودکار اپدیت بفرمایید، همچنین برای تاریخ های دیگر نیز میتوانید متغیر های قسمت Define Dates را تغییر دهید.
نحوه کار کد به صورت کلی به نحوه زیر میباشد:
ابتدا تمام محصولات ساده لیست شده و برای شرط برابر بودن تاریخ فروش ویژه چک میشوند، سپس اگر شرایط برای محصول درست باشد تاریخ فروش ویژه محصول به تاریخ مد نظر اپدیت میشود.
سپس برای تمام محصولات متغیر و والد های انها نیز همین مورد انجام میشود.
صرفا برای از دسترس خارج نشدن سایت در سایت هایی که بیش از 500 محصول دارند و منابع سرور پایینی هم دارند $batch_size تعریف شده مقدار محصولاتی هست که در هر بار اجرای کد بررسی میشود و درکل محصولات را دسته بندی کرده و در هر اجرا یک دسته را بررسی و آپدیت میکند.
این کد را میتوانید در افزونه Code Snippets اجرا بفرمایید
شما میتوانید سوالات خود را از طریق ایمیل پشتیبانی – تماس با ما – یا در قسمت نظرات سوال خود را بپرسید.
موفق باشید
A.J
پست های مرتبط:
فروشگاه سورسا:
سورسا ، یک خانواده!
شما میتوانید از سورس های آماده به راحتی و با کپی پیست در پروژه خود استفاده بفرمایید
بله! سورسا به عنوان اولین و برترین مرجع سورس کد های آماده، تمامی سورس کد ها در زبان های مختلف را به صورت رایگان در اختیار شما قرار میدهد.
اگر سورس مد نظر شما تفاوتی با سورس فعلی دارد یا اینکه درخواست سورس دیگری را دارید میتوانید با کارشناسان سورسا در ارتباط باشید.
سورسا به عنوان مرجع سورس در تلاش است سورس کد ها و آموزش های تمامی زبان های برنامه نویسی مانند GO C++ Python C PHP SQL JS و… را تحت پوشش قرار داد