WC_AJAX::variation_bulk_action_variable_sale_price_from_regular_price │ private static │ WC 1.0

Bulk action - Set Sale Prices from Regular Prices.

Метод класса: WC_AJAX{}

Хуков нет.

Возвращает

void. Ничего (null).

Использование

$result = WC_AJAX::variation_bulk_action_variable_sale_price_from_regular_price( $variations, $data );
$variations(массив) (обязательный)
List of variations.
$data(массив) (обязательный)
Data to set.

Код WC_AJAX::variation_bulk_action_variable_sale_price_from_regular_price() WC 11.3.0

private static function variation_bulk_action_variable_sale_price_from_regular_price( $variations, $data ) {
	$value = $data['value'] ?? null;
	if ( ! is_scalar( $value ) ) {
		return;
	}

	$value         = (string) $value;
	$is_percentage = '%' === substr( $value, -1 );
	$adjustment    = $is_percentage ? substr( $value, 0, -1 ) : $value;
	if ( ! is_numeric( $adjustment ) || 0 > (float) $adjustment ) {
		return;
	}
	$adjustment = (float) $adjustment;

	foreach ( $variations as $variation_id ) {
		$variation = wc_get_product( $variation_id );
		if ( ! $variation instanceof WC_Product_Variation ) {
			continue;
		}

		$regular_price = $variation->get_regular_price( 'edit' );

		if ( '' === $regular_price || null === $regular_price ) {
			continue;
		}

		$reduction = $is_percentage ? ( (float) $regular_price / 100 ) * $adjustment : $adjustment;

		$variation->set_sale_price( (string) NumberUtil::round( max( 0, (float) $regular_price - $reduction ), wc_get_price_decimals() ) );
		$variation->save();
	}
}