Иногда необходимо добавить дополнительное поле в данные товара, в блок с ценой:
Для этого можно воспользоваться следующим кодом:
add_action( 'woocommerce_product_options_pricing', 'wc_desc_product_field' );
function wc_desc_product_field() {
woocommerce_wp_text_input( array( 'id' => 'desc_price', 'class' => 'custom-price', 'label' => 'Подробности цены', 'type' => 'text' ) );
}
add_action( 'save_post', 'wc_desc_save_product' );
function wc_desc_save_product( $product_id ) {
if (wp_verify_nonce($_POST['_inline_edit'], 'inlineeditnonce'))
return;
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
if ( isset( $_POST['desc_price'] ) ) {
update_post_meta( $product_id, 'desc_price', $_POST['desc_price'] );
} else delete_post_meta( $product_id, 'desc_price' );
}
В шаблоне WordPress сохраненное поле выводится следующим образом:
<?php if(get_post_meta($post->ID, 'desc_price')[0]){
echo ' '.get_post_meta($post->ID, 'desc_price')[0];
}?>
Обновлено: 31.10.2019
Вам помогла эта статья? Оцените!