Created
May 23, 2020 11:20
-
-
Save damiencarbery/846705e6caa064d49c86ac873ee64d6f to your computer and use it in GitHub Desktop.
Add custom tab to Product Data area of Edit Product page - Add a custom field to Edit Product page. Add a custom tab to organise it. https://www.damiencarbery.com/2020/05/add-custom-tab-to-product-data-area-of-edit-product-page/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Add custom tab to Product Data area of Edit Product page | |
Plugin URI: https://www.damiencarbery.com/2020/05/add-custom-tab-to-product-data-area-of-edit-product-page/ | |
Description: Add a custom field to Edit Product page. Add a custom tab to organise it. | |
Author: Damien Carbery | |
Version: 0.1 | |
*/ | |
// Add a new 'Care Information' tab to Product Data area of Edit Product page. | |
add_filter( 'woocommerce_product_data_tabs', 'dcwd_care_instruction_product_data_tab' ); | |
function dcwd_care_instruction_product_data_tab( $tabs ) { | |
$tabs['where'] = array( | |
'label' => 'Care Information', | |
'target' => 'care_instruction_product_data', | |
'priority' => 90, | |
); | |
return $tabs; | |
} | |
// Return an array of the options. This is used in multiple functions and | |
// is used to validate/sanitise the submitted value. | |
function dcwd_care_instructions() { | |
$care_instructions = array( | |
'cold' => 'Wash at 30°C', | |
'warm' => 'Wash at 40°C', | |
'hot' => 'Wash at 60°C', | |
); | |
return $care_instructions; | |
} | |
// Add the Delivery Option tab contents. | |
add_action( 'woocommerce_product_data_panels', 'care_instruction_data_panel', 100 ); | |
function care_instruction_data_panel() { | |
?> | |
<div id="care_instruction_product_data" class="panel woocommerce_options_panel"> | |
<div class="options_group care_instruction"> | |
<?php | |
woocommerce_wp_radio( array( | |
'id' => '_care_instruction', | |
'label' => 'Care instruction', | |
'options' => dcwd_care_instructions(), | |
) ); | |
?> | |
</div> | |
</div> | |
<?php | |
} | |
// Save Care Information radio field to post meta. | |
// Save code is based on: https://businessbloomer.com/woocommerce-display-rrp-msrp-manufacturer-price/ | |
add_action( 'save_post_product', 'dcwd_save_care_instruction' ); | |
function dcwd_save_care_instruction( $product_id ) { | |
global $pagenow, $typenow; | |
if ( 'post.php' !== $pagenow || 'product' !== $typenow ) return; | |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return; | |
if ( isset( $_POST['_care_instruction'] ) ) { | |
if ( $_POST['_care_instruction'] ) { | |
$care_instructions = dcwd_care_instructions(); | |
// Verify that the submitted value is a valid one. | |
if ( array_key_exists( $_POST['_care_instruction'], $care_instructions ) ) { | |
update_post_meta( $product_id, '_care_instruction', $_POST['_care_instruction'] ); | |
} | |
} | |
} else { | |
delete_post_meta( $product_id, '_care_instruction' ); | |
} | |
} | |
// Add to the "Additional Information" tab on the single product page. | |
add_action( 'woocommerce_product_additional_information', 'dcwd_display_care_instruction', 10 ); | |
function dcwd_display_care_instruction( $product ) { | |
if ( 'variable' != $product->get_type() && $care_instruction = get_post_meta( $product->get_id(), '_care_instruction', true ) ) { | |
$care_instructions = dcwd_care_instructions(); | |
printf( '<p>Care instruction: %s</p>', $care_instructions[ $care_instruction ] ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment