1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
<?php
namespace SIW\Batch;
use SIW\Util;
class Update_Workcamp_Tariffs extends Job {
protected $action = 'update_workcamp_tariffs';
protected $name = 'bijwerken tarieven';
protected $category = 'groepsprojecten';
protected function select_data() {
$args = [
'return' => 'ids',
'limit' => -1,
];
$products = wc_get_products( $args );
return $products;
}
protected function task( $product_id ) {
$product = wc_get_product( $product_id );
if ( false == $product ) {
return false;
}
$tariffs = siw_get_data( 'workcamps/tariffs' );
$sale = Util::is_workcamp_sale_active();
$workcamp_sale = siw_get_option( 'workcamp_sale' );
$variations = $product->get_children();
$is_updated = false;
foreach ( $variations as $variation_id ) {
$variation = wc_get_product( $variation_id );
$variation_tariff = $variation->get_attributes()['pa_tarief'];
$tariff = $tariffs[ $variation_tariff ] ?? 'regulier';
$regular_price = $tariff['regular_price'];
$sale_price = $tariff['sale_price'];
$variation->set_props([
'regular_price' => $regular_price,
'sale_price' => $sale ? $sale_price : null,
'price' => $sale ? $sale_price : $regular_price,
'date_on_sale_from' => $sale ? date( 'Y-m-d 00:00:00', strtotime( $workcamp_sale['start_date'] ) ) : null,
'date_on_sale_to' => $sale ? date( 'Y-m-d 23:59:59', strtotime( $workcamp_sale['end_date'] ) ) : null,
]);
if ( ! empty( $variation->get_changes() ) ) {
$variation->save();
$is_updated = true;
}
}
if ( $is_updated ) {
$this->increment_processed_count();
}
return false;
}
}