Сохранение пользовательских полей для различных продуктов
В настоящее время я использую WooCommerce для WordPress и пытаюсь добавить пользовательские поля для продукта Variations. Проведя некоторые исследования, я нашел некоторый код и попытался его изменить.
Это мой полный код: https://gist.github.com/alphadc/da163cc95cfd1cede34a
add_action( 'woocommerce_product_after_variable_attributes', 'variable_fields', 10, 2 );
add_action( 'woocommerce_product_after_variable_attributes_js', 'variable_fields_js' );
add_action( 'woocommerce_process_product_meta_variable', 'save_variable_fields', 10, 1 );
add_action( 'woocommerce_process_product_meta_variable-subscription' , 'save_variable_fields' , 10 , 1 ) ;
function variable_fields( $loop, $variation_data ) {?>
<tr>
<td>
<?php
woocommerce_wp_textarea_input(
array(
'id' => '_weightdesc['.$loop.']',
'label' => __( 'Weight Description', 'woocommerce' ),
'placeholder' => '',
'description' => __( 'Enter the custom value here.', 'woocommerce' ),
'value' => $variation_data['_weightdesc'][0],
)
);
?>
</td>
</tr>
<?php }
function variable_fields_js()?>
<tr>
<td>
<?php
woocommerce_wp_textarea_input(
array(
'id' => '_weightdesc[ + loop + ]',
'label' => __( 'My Textarea', 'woocommerce' ),
'placeholder' => '',
'description' => __( 'Enter the custom value here.', 'woocommerce' ),
'value' => $variation_data['_weightdesc'][0],
)
);
?>
</td>
</tr>
<?php }
function save_variable_fields( $post_id ) {
if (isset( $_POST['variable_sku'] ) ) :
$variable_sku = $_POST['variable_sku'];
$variable_post_id = $_POST['variable_post_id'];
// Textarea
$_weightdesc = $_POST['_weightdesc'];
for ( $i = 0; $i < sizeof( $variable_sku ); $i++ ) :
$variation_id = (int) $variable_post_id[$i];
if ( isset( $_weightdesc[$i] ) ) {
update_post_meta( $variation_id, '_weightdesc', stripslashes( $_weightdesc[$i] ) );
}
endfor;
endif;
}
Поле отображается на моем бэкэнде, но когда я попытался сохранить значение, оно не работает. Я тоже пытался его изменить, но все еще не получалось.
Я нашел этот код из несколько источников, из которых один из них пришел: http://www.remicorson.com/woocommerce-custom-fields-for-variations/#comment-14159
Я думаю, что это из-за обновления WooCommerce (я использую 2.3.5).
Кто-нибудь может мне помочь, пожалуйста?
3 answers
Хорошо, основываясь на ответах по ссылке выше (где я получил старый код и есть люди, которые помогают ответить), я ввел код модификации для своего веб-сайта. Я попробовал, и это работает как заклинание.
Изменение:
add_action( 'woocommerce_product_after_variable_attributes', 'variable_fields', 10, 2 );
В:
add_action( 'woocommerce_variation_options', 'variable_fields', 10, 3 );
И изменить:
'value' => $variation_data['_weightdesc'][0],
В:
'value' => get_post_meta($variation->ID, '_weightdesc', true)
Я знаю, что этот пост старый, но чтобы этот вопрос был обновлен:
По состоянию на WooCommerce 2.4.4
woocommerce_process_product_meta_variable
больше не работает, и его необходимо изменить на woocommerce_save_product_variation
Итак,
Изменение:
add_action( 'woocommerce_process_product_meta_variable', 'save_variable_fields', 10, 1 );
В:
add_action( 'woocommerce_save_product_variation', 'save_variable_fields', 10, 1 );
function variation_settings_fields( $loop, $variation_data, $variation ) {
// Text Field
woocommerce_wp_text_input(
array(
'id' => '_text_field[' . $variation->ID . ']',
'label' => __( 'My Text Field', 'woocommerce' ),
'placeholder' => '',
'desc_tip' => 'true',
'description' => __( 'Enter the custom value here.', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_text_field', true )
)
);
}
add_action( 'woocommerce_product_after_variable_attributes', 'variation_settings_fields', 10, 3 );
function save_variation_settings_fields( $post_id ) {
// Text Field
$text_field = $_POST['_text_field'][ $post_id ];
if ( ! empty( $text_field ) ) {
update_post_meta( $post_id, '_text_field', esc_attr( $text_field ) );
}
}