Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Create WooCommerce meta tab. বাংলা টিউটোরিয়ালঃ https://obiplabon.im/755/working-with-woocommerce-metabox-and-metadata/
<?php
/**
* Add awesome tab to tab list
*
* @param array $tabs Tabs list
* @return array
*/
function op_add_awesome_tab( $tabs ) {
$tabs['awesome-tab'] = array(
'label' => esc_html__( 'Awesome Tab', 'text-domain' ),
'target' => 'awesome-tab',
'class' => array(),
'priority' => 80,
);
return $tabs;
}
add_filter( 'woocommerce_product_data_tabs', 'op_add_awesome_tab' );
<?php
/**
* Add awesome tab to tab list
*
* @param array $tabs Tabs list
* @return array
*/
function op_add_awesome_tab( $tabs ) {
$tabs['awesome-tab'] = array(
'label' => esc_html__( 'Awesome Tab', 'text-domain' ),
'target' => 'awesome-tab',
'class' => array(),
'priority' => 80,
);
return $tabs;
}
add_filter( 'woocommerce_product_data_tabs', 'op_add_awesome_tab' );
/**
* Render awesome tab fields
*
* @return void
*/
function op_render_awesome_tab() {
global $product_object; ?>
<div id="awesome-tab" class="panel woocommerce_options_panel hidden">
<div id="awesome-tab_product_data_inner">
<?php
woocommerce_wp_text_input(
array(
'id' => '_awesome_text_1',
'label' => esc_html__( 'Awesome Text 1', 'text-domain' ),
'placeholder' => esc_attr__( 'Add some awesome text.', 'text-domain' ),
'description' => esc_html__( 'Add some awesome text.', 'text-domain' ),
'desc_tip' => true,
'type' => 'text',
'value' => $product_object->get_meta( '_awesome_text_1', true ),
)
);
woocommerce_wp_text_input(
array(
'id' => '_awesome_text_2',
'label' => esc_html__( 'Awesome Text 2', 'text-domain' ),
'placeholder' => esc_attr__( 'Add some awesome text.', 'text-domain' ),
'description' => esc_attr__( 'Add some awesome text.', 'text-domain' ),
'desc_tip' => true,
'type' => 'text',
'value' => $product_object->get_meta( '_awesome_text_2', true ),
)
);
?>
</div>
</div>
<?php
}
add_action( 'woocommerce_product_data_panels', 'op_render_awesome_tab' );
/**
* Save awesome tab field data
* @param object $product WC product object
* @return void
*/
function op_save_awesome_tab_data( $product ) {
if ( isset( $_POST['_awesome_text_2'] ) ) {
$product->update_meta_data( '_awesome_text_2', sanitize_text_field( $_POST['_awesome_text_2'] ) );
}
if ( isset( $_POST['_awesome_text_1'] ) ) {
$product->update_meta_data( '_awesome_text_1', sanitize_text_field( $_POST['_awesome_text_1'] ) );
}
}
add_action( 'woocommerce_admin_process_product_object', 'op_save_awesome_tab_data' );
<?php
/**
* Render awesome tab fields
*
* @return void
*/
function op_render_awesome_tab() {
global $product_object; ?>
<div id="awesome-tab" class="panel woocommerce_options_panel hidden">
<div id="awesome-tab_product_data_inner">
<?php
woocommerce_wp_text_input(
array(
'id' => '_awesome_text_1',
'label' => esc_html__( 'Awesome Text 1', 'text-domain' ),
'placeholder' => esc_attr__( 'Add some awesome text.', 'text-domain' ),
'description' => esc_html__( 'Add some awesome text.', 'text-domain' ),
'desc_tip' => true,
'type' => 'text',
'value' => $product_object->get_meta( '_awesome_text_1', true ),
)
);
woocommerce_wp_text_input(
array(
'id' => '_awesome_text_2',
'label' => esc_html__( 'Awesome Text 2', 'text-domain' ),
'placeholder' => esc_attr__( 'Add some awesome text.', 'text-domain' ),
'description' => esc_attr__( 'Add some awesome text.', 'text-domain' ),
'desc_tip' => true,
'type' => 'text',
'value' => $product_object->get_meta( '_awesome_text_2', true ),
)
);
?>
</div>
</div>
<?php
}
add_action( 'woocommerce_product_data_panels', 'op_render_awesome_tab' );
<?php
/**
* Save awesome tab field data
* @param object $product WC product object
* @return void
*/
function op_save_awesome_tab_data( $product ) {
if ( isset( $_POST['_awesome_text_2'] ) ) {
$product->update_meta_data( '_awesome_text_2', sanitize_text_field( $_POST['_awesome_text_2'] ) );
}
if ( isset( $_POST['_awesome_text_1'] ) ) {
$product->update_meta_data( '_awesome_text_1', sanitize_text_field( $_POST['_awesome_text_1'] ) );
}
}
add_action( 'woocommerce_admin_process_product_object', 'op_save_awesome_tab_data' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment