на основе этого великого поста Я создал несколько строк кода, чтобы изменить вариант продукта с переменной величиной.
// Generating dynamically the product "sale price"
add_filter( 'woocommerce_product_get_sale_price', 'custom_dynamic_sale_price', 10, 2 );
add_filter( 'woocommerce_product_variation_get_sale_price', 'custom_dynamic_sale_price', 10, 2 );
function custom_dynamic_sale_price( $sale_price, $product ) {
if( $product->get_id() != 4096 ) return $price_html;;
$rate = 0.5;
if( empty($sale_price) || $sale_price == 0 )
return $product->get_regular_price() * $rate;
else
return $sale_price;
};
// Displayed formatted regular price + sale price
add_filter( 'woocommerce_get_price_html', 'custom_dynamic_sale_price_html', 20, 2 );
function custom_dynamic_sale_price_html( $price_html, $product ) {
if( $product->get_id() != 4096 ) return $price_html;;
if( $product->is_type('variable') ) return $price_html;
$price_html = wc_format_sale_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ), wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) ) ) . $product->get_price_suffix();
return $price_html;
}
add_action( 'woocommerce_before_calculate_totals', 'set_cart_item_sale_price', 20, 1 );
function set_cart_item_sale_price( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
/*
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
*/
// Iterate through each cart item
foreach( $cart->get_cart() as $cart_item ) {
if ( $cart_item['data']->get_id() == 4096) {
$price = $cart_item['data']->get_sale_price(); // get sale price
$cart_item['data']->set_price( $price ); // Set the sale price
}
}
}
Это почти работает :), когда я смотрю в корзину,общая сумма верна, но в обзоре стоимость доставки уже была добавлена:
и в ней указано «бесплатная доставка». (что может сбить с толку клиента)
Также на странице категории цена не обновляется. он по-прежнему говорит: «Этот вариант доступен от 16 €».
У вас есть идея?
спасибо!