Skip to content

Instantly share code, notes, and snippets.

@turtlepod
Last active August 7, 2017 06:07
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 turtlepod/18fd03b3a74c888f80e88ab296813567 to your computer and use it in GitHub Desktop.
Save turtlepod/18fd03b3a74c888f80e88ab296813567 to your computer and use it in GitHub Desktop.
<?php
/**
* WC Meta Data ~ How to do this in WooCommerce 3 ?
*
* @link https://stackoverflow.com/questions/45540351/woocommerce-3-crud-how-to-do-this
*
* @link https://github.com/woocommerce/woocommerce/wiki/CRUD-Objects-in-3.0
*/
add_action( 'plugins_loaded', function() {
if ( ! class_exists( 'WooCommerce' ) ) {
return;
}
// Add tabs.
add_filter( 'woocommerce_product_data_tabs', function( $tabs ) {
$tabs['test'] = array(
'label' => 'TEST',
'target' => 'my_test_data',
'class' => array(),
'priority' => 1,
);
return $tabs;
} );
// Add panels.
add_action( 'woocommerce_product_data_panels', function() {
?>
<div id="my_test_data" class="panel woocommerce_options_panel">
<?php woocommerce_wp_text_input( array(
'id' => '_my_test_data',
'label' => 'Test Input',
'description' => 'Lorem Ipsum',
'value' => get_post_meta( get_the_id(), '_my_test_data', true ),
'placeholder' => '',
'type' => 'text',
) ); ?>
</div>
<?php
} );
// Save.
add_action( 'woocommerce_process_product_meta', function( $post_id, $post ) {
if ( isset( $_POST['_my_test_data'] ) ) {
update_post_meta( $post_id, '_my_test_data', $_POST['_my_test_data'] );
}
}, 10, 2 );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment