Skip to content

Instantly share code, notes, and snippets.

@damiencarbery
Last active September 8, 2022 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save damiencarbery/091c749ba9bfe985cd84af0a114e5aff to your computer and use it in GitHub Desktop.
Save damiencarbery/091c749ba9bfe985cd84af0a114e5aff to your computer and use it in GitHub Desktop.
Add custom product info to Additional Information tab - Use CMB2 to add a custom metabox to WooCommerce product then add data to Additional Information tab on product page. https://www.damiencarbery.com/2020/03/add-custom-product-info-to-additional-information-tab/
<?php
/*
Plugin Name: Add custom product info to Additional Information tab
Plugin URI: https://www.damiencarbery.com/2020/03/add-custom-product-info-to-additional-information-tab/
Description: Use CMB2 to add a custom metabox to WooCommerce product then add data to Additional Information tab on product page.
Author: Damien Carbery
Author URI: https://www.damiencarbery.com
Version: 0.4
*/
add_action( 'cmb2_admin_init', 'dcwd_wc_book_info_fields_metabox' );
function dcwd_wc_book_info_fields_metabox() {
$cmb = new_cmb2_box( array(
'id' => 'book_info',
'title' => 'Book Information',
'object_types' => array( 'product', ), // Post type
'context' => 'side',
'priority' => 'default',
'show_names' => true, // Show field names on the left
) );
$cmb->add_field( array(
'name' => 'ISBN13',
'id' => 'isbn13',
'type' => 'text',
/*'attributes' => array(
'required' => 'required',
),*/
) );
$cmb->add_field( array(
'name' => 'ISBN10',
'id' => 'isbn10',
'type' => 'text',
/*'attributes' => array(
'required' => 'required',
),*/
) );
$cmb->add_field( array(
'name' => 'Pages',
'id' => 'pages',
'type' => 'text_small',
'attributes' => array(
'type' => 'number',
'pattern' => '\d*',
//'required' => 'required',
),
'sanitization_cb' => 'absint',
'escape_cb' => 'absint',
) );
$cmb->add_field( array(
'name' => 'Author',
'id' => 'author',
'type' => 'text',
/*'attributes' => array(
'required' => 'required',
),*/
) );
$cmb->add_field( array(
'name' => 'Publisher',
'id' => 'publisher',
'type' => 'text',
/*'attributes' => array(
'required' => 'required',
),*/
) );
$cmb->add_field( array(
'name' => 'Publish Date',
'id' => 'publish_date',
'type' => 'text_date',
/*'attributes' => array(
'required' => 'required',
),*/
) );
}
// Add the book information to the Additional Information tab.
add_action( 'woocommerce_product_additional_information', 'dcwd_book_info_in_additional_info_tab', 20 );
function dcwd_book_info_in_additional_info_tab( $product ) {
$product_post_meta = get_post_meta( $product->get_id() );
// Post meta field => Label.
$book_info_fields = array( 'isbn13' => 'ISBN13', 'isbn10' => 'ISBN10',
'pages' => 'Pages', 'author' => 'Author',
'publisher' => 'Publisher', 'publish_date' => 'Publish Date' );
$book_info = array();
foreach ( $book_info_fields as $key => $label ) {
if ( array_key_exists( $key, $product_post_meta ) ) {
// Display the label and then the data.
$book_info[] = sprintf( '<strong>%s</strong>: %s', $label, $product_post_meta[ $key ][ 0 ] );
}
}
echo '<p>', implode( '<br />', $book_info ), '</p>';
}
// Verify that CMB2 plugin is active.
add_action( 'admin_notices', 'verify_cmb2_active' );
function verify_cmb2_active() {
if ( ! defined( 'CMB2_LOADED' ) ) {
$plugin_data = get_plugin_data( __FILE__ );
$plugin_name = $plugin_data['Name'];
?>
<div class="notice notice-warning is-dismissible"><p>Plugin <strong><?php echo $plugin_name; ?></strong> requires <a href="https://wordpress.org/plugins/cmb2/">CMB2 plugin</a>.</p></div>
<?php
//error_log( 'CMB2 is not active.' );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment