Created
September 8, 2022 16:00
-
-
Save fatihtoprak/a3bd5ed19f87d9dcaf0e9f10141fbd39 to your computer and use it in GitHub Desktop.
Add / display Custom meta field on WooCommerce General Product Tab
This file contains hidden or 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 | |
add_action('woocommerce_product_options_general_product_data', 'addCustomMetaDataProductGeneralTab' ); | |
function addCustomMetaDataProductGeneralTab() { | |
woocommerce_wp_text_input( | |
[ | |
'id' => '__optimist_color', | |
'label' => 'Product Color', | |
'description' => 'Add product color', | |
] | |
); | |
woocommerce_wp_text_input( | |
[ | |
'id' => '__optimist_stone_details', | |
'label' => 'Stone Details', | |
'description' => 'Add stone details', | |
] | |
); | |
woocommerce_wp_text_input( | |
[ | |
'id' => '__optimist_main_details', | |
'label' => 'Main Details', | |
'description' => 'Add main details', | |
] | |
); | |
} | |
add_action( 'woocommerce_process_product_meta', 'saveCustomMetaDataProductGeneralTab' ); | |
function saveCustomMetaDataProductGeneralTab( $post_id ) | |
{ | |
if ( ! empty( $_POST['__optimist_color'] ) ) { | |
update_post_meta( $post_id, '__optimist_color', esc_attr( $_POST['__optimist_color'] ) ); | |
} | |
if ( ! empty( $_POST['__optimist_stone_details'] ) ) { | |
update_post_meta( $post_id, '__optimist_stone_details', esc_attr( $_POST['__optimist_stone_details'] ) ); | |
} | |
if ( ! empty( $_POST['__optimist_main_details'] ) ) { | |
update_post_meta( $post_id, '__optimist_main_details', esc_attr( $_POST['__optimist_main_details'] ) ); | |
} | |
} | |
add_action('woocommerce_product_meta_end','displayCustoProductMetaOnFrontEnd' ); | |
function displayCustoProductMetaOnFrontEnd() | |
{ | |
global $post; | |
$postId = $post->ID; | |
$__color = get_post_meta( $postId, '__optimist_color' ,true); | |
$__stone = get_post_meta( $postId, '__optimist_stone_details' ,true); | |
$__main = get_post_meta( $postId, '__optimist_main_details' ,true); | |
if(strlen($__color)) | |
{ | |
echo '<span class="tagged_as"><span class="qodef-woo-meta-label">Color:</span><span class="qodef-woo-meta-value">'.$__color.'</span></span>'; | |
} | |
if(strlen($__stone)) | |
{ | |
echo '<span class="tagged_as"><span class="qodef-woo-meta-label">Stone Details:</span><span class="qodef-woo-meta-value">'.$__stone.'</span></span>'; | |
} | |
if(strlen($__main)) | |
{ | |
echo '<span class="tagged_as"><span class="qodef-woo-meta-label">Main Details:</span><span class="qodef-woo-meta-value">'.$__main.'</span></span>'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment