Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Метабокс цены для CPT, который позволяет добавить любую запись из CPT в WC корзину.
function gift_register_meta_box() {
add_meta_box(
'mdf_gift_meta_box',
__( 'Цена', 'tochkawp' ),
'mdf_gift_meta_box_handler',
'gift',
'normal',
'high'
);
}
add_action( 'add_meta_boxes', 'gift_register_meta_box' );
function mdf_gift_meta_box_handler( $post ) {
wp_nonce_field( 'mdf_gift_save_meta_box', 'mdf_gift_meta_box_nonce' );
$date = get_post_meta( $post->ID, '_price', true ) ?>
<input type="text" name="_regular_price" value="<?php echo esc_attr( $date ) ?>">
<?php }
function mdf_gift_save_meta_box( $post_id ) {
if ( ! isset( $_POST['mdf_gift_meta_box_nonce'] ) ) {
return;
}
if ( ! wp_verify_nonce( $_POST['mdf_gift_meta_box_nonce'], 'mdf_gift_save_meta_box' ) ) {
return;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if ( isset( $_POST['post_type'] ) && 'gift' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
}
if ( ! isset( $_POST['_regular_price'] ) ) {
return;
}
$date_p1_plus = sanitize_text_field( $_POST['_regular_price'] );
update_post_meta( $post_id, '_price', $date_p1_plus );
}
add_action( 'save_post', 'mdf_gift_save_meta_box' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment