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
<?php
namespace SIW\WooCommerce\Import;
use SIW\Util;
class Product_Variations {
protected $product;
protected $tariffs= [];
public function __construct( \WC_Product $product, array $tariffs ) {
$this->product = $product;
$this->tariffs = $tariffs;
}
public function update() {
$variations = $this->product->get_children();
foreach ( $variations as $variation_id ) {
$variation = wc_get_product( $variation_id );
if ( false === $variation ) {
continue;
}
$variation_tariff = $variation->get_attributes()['pa_tarief'];
if ( isset( $this->tariffs[ $variation_tariff ] ) ) {
unset( $this->tariffs[ $variation_tariff ] );
}
else {
$variation->delete( true );
}
}
}
public function create() {
$sale = Util::is_workcamp_sale_active();
$workcamp_sale = siw_get_option( 'workcamp_sale' );
foreach ( $this->tariffs as $slug => $tariff ) {
$variation = new \WC_Product_Variation;
$variation->set_props( [
'parent_id' => $this->product->get_id(),
'attributes' => [ 'pa_tarief' => $slug ],
'virtual' => true,
'regular_price' => $tariff['regular_price'],
'sale_price' => $sale ? $tariff['sale_price'] : null,
'price' => $sale ? $tariff['sale_price'] : $tariff['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,
]);
$variation->save();
}
}
}