Skip to content

Instantly share code, notes, and snippets.

@al5dy
Created March 31, 2016 19:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save al5dy/e9304d531f1180429230ce0da8178659 to your computer and use it in GitHub Desktop.
Save al5dy/e9304d531f1180429230ce0da8178659 to your computer and use it in GitHub Desktop.
Метабокс цены для 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