1

использую такой код чтоб добавить в вариацую кастомное поле и можно было считать цену на товар через % скидки. Вопрос в том, какой хук отвечает за изменение цены скидки перед сохранением настроек ?

 // -----------------------------------------
// 1. Add custom field input @ Product Data > Variations > Single Variation

add_action( 'woocommerce_variation_options_pricing', 'bbloomer_add_custom_field_to_variations', 10, 3 );

function bbloomer_add_custom_field_to_variations( $loop, $variation_data, $variation ) {
woocommerce_wp_text_input( array(
'id' => 'custom_field[' . $loop . ']',
'class' => 'short',
'label' => __('The Percent of Sales', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, 'custom_field', true )
)
);
}

// -----------------------------------------
// 2. Save custom field on product variation save

add_action( 'woocommerce_save_product_variation', 'bbloomer_save_custom_field_variations', 10, 2 );

function bbloomer_save_custom_field_variations( $variation_id, $i ) {
$custom_field = $_POST['custom_field'][$i];
if ( isset( $custom_field ) ) update_post_meta( $variation_id, 'custom_field', esc_attr( $custom_field ) );

}

// -----------------------------------------
// 3. Store custom field value into variation data

add_filter( 'woocommerce_available_variation', 'bbloomer_add_custom_field_variation_data' );

function bbloomer_add_custom_field_variation_data( $variations ) {
$variations['custom_field'] = '<div class="woocommerce_custom_field">The Percent of Sales: <span>' . get_post_meta( $variations[ 'variation_id' ], 'custom_field', true ) . '</span></div>';
return $variations;
}

UPD: Я так понимаю скидку можно передать при сохранение только таким способом ?

update_post_meta( $variation_id, 'variable_sale_price', '10' );

1 ответ 1

0

Изменить

update_post_meta( $variation_id, 'variable_sale_price', 'Your value' );

На

update_post_meta( $variation_id, '_sale_price', 'Your value' );

Через инспектор смотреть код то у полей input name пишется так "variable_" и название input'a. В коде ссылаться надо без "variable", а просто "_your_input"

Финальный код

add_action( 'woocommerce_variation_options_pricing', 'bbloomer_add_custom_field_to_variations', 10, 3 );

function bbloomer_add_custom_field_to_variations( $loop, $variation_data, $variation ) {

// Text input
    woocommerce_wp_text_input(
        array(
            'id'          => 'variable_sale_percent[' . $loop . ']',
            'label'       => __( 'Percent of sale', 'woocommerce' ),
            'value'       => get_post_meta( $variation->ID, 'variable_sale_percent', true ),
        )
    );

}

// -----------------------------------------
// 2. Save custom field on product variation save

add_action( 'woocommerce_save_product_variation', 'bbloomer_save_custom_field_variations', 10, 2 );

function bbloomer_save_custom_field_variations( $variation_id, $i ) {

$regular_price = $_POST['variable_regular_price'][$i];
$sale_percent = $_POST['variable_sale_percent'][$i];
$percent = ceil(($regular_price/100)*(100 - $sale_percent));


update_post_meta( $variation_id, 'variable_sale_percent', $sale_percent );
if($sale_percent !== ""){
update_post_meta( $variation_id, '_sale_price', $percent );
} else{
update_post_meta( $variation_id, '_sale_price', '' );
}


}

Ваш ответ

Нажимая на кнопку «Отправить ответ», вы соглашаетесь с нашими условиями использования и подтверждаете, что прочитали и поняли наши политику конфиденциальности и нормы поведения.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.