Created
December 28, 2023 17:51
-
-
Save anisur2805/eb4563e776ddee5beee4e9d6b76d8b1b to your computer and use it in GitHub Desktop.
WooCommerce Every product add custom email and phone number. If no one is provide will display default one on Single Product Page underneath Title
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 | |
// Preview Backend: https://i.imgur.com/hyLOnlI.png | |
// Preview Frontend: https://i.imgur.com/hyLOnlI.png | |
// Add new tab in the backend. | |
add_filter( 'woocommerce_product_data_tabs', 'sfc_add_product_data_to_tab' ); | |
function sfc_add_product_data_to_tab( $default_tabs ) { | |
global $post; | |
$tabs = array( | |
'sfc_phone_email' => array( | |
'label' => __( 'Phone & Email', 'storefront' ), | |
'target' => 'sfc_phone_email', | |
'class' => 'sfc_phone_email', | |
'priority' => 10, | |
) | |
); | |
return array_merge( $default_tabs, $tabs ); | |
} | |
// Add new tab content in the backend. | |
add_action( 'woocommerce_product_data_panels', 'sfc_data_panels' ); | |
function sfc_data_panels(){ | |
global $woocommerce, $post; | |
$sfc_phone_number = get_post_meta( $post->ID, '_sfc_phone_number', true ); | |
$sfc_phone_email = get_post_meta( $post->ID, '_sfc_phone_email', true ); | |
$sfc_phone_number = isset( $sfc_phone_number ) ? sanitize_text_field( $sfc_phone_number ) : ''; | |
$sfc_phone_email = isset( $sfc_phone_email ) ? sanitize_email( $sfc_phone_email ) : ''; | |
?> | |
<div id="sfc_phone_email" class="panel woocommerce_options_panel"> | |
<div class="options_group pricing show_if_simple show_if_external"> | |
<p class="form-field"> | |
<label for="sfc_phone_number">Phone Number: </label> | |
<input type="text" class="short wc_input_phone" name="sfc_phone_number" id="sfc_phone_number" value="<?php echo esc_attr( $sfc_phone_number ); ?>" /> | |
</p> | |
<p class="form-field"> | |
<label for="sfc_phone_email">Email: </label> | |
<input type="text" class="short wc_input_email" name="sfc_phone_email" id="sfc_phone_email" value="<?php echo esc_attr( $sfc_phone_email ); ?>" /> | |
</p> | |
</div> | |
</div> | |
<?php | |
} | |
// Save content in the backend. | |
add_action( 'save_post', 'sfc_save_product_data', 10, 3 ); | |
function sfc_save_product_data( $post_id, $post, $update ) { | |
if ( isset( $_POST['sfc_phone_number'] ) ) { | |
update_post_meta( $post_id, '_sfc_phone_number', sanitize_text_field( $_POST['sfc_phone_number'] ) ); | |
} | |
if ( isset( $_POST['sfc_phone_email'] ) ) { | |
update_post_meta( $post_id, '_sfc_phone_email', sanitize_email( $_POST['sfc_phone_email'] ) ); | |
} | |
} | |
// Display content in the backend. | |
add_action( 'woocommerce_before_add_to_cart_form', 'sfc_display_phone_email_after_product_title' ); | |
function sfc_display_phone_email_after_product_title() { | |
global $post; | |
$sfc_phone_number = get_post_meta( $post->ID, '_sfc_phone_number', true ); | |
$sfc_phone_email = get_post_meta( $post->ID, '_sfc_phone_email', true ); | |
// Sanitize and set default values. | |
$sfc_phone_number = ( ! empty( $sfc_phone_number ) ) ? sanitize_text_field( $sfc_phone_number ) : '01600000000'; | |
$sfc_phone_email = ( ! empty( $sfc_phone_email ) ) ? sanitize_email( $sfc_phone_email ) : '[email protected]'; | |
// Display phone number if set. | |
printf('<div class="sfc_phone_email"><div><a class="sfc_phone_number" href="tel:%s"><i class="fa fa-phone mr-4"></i>%s</a></div>', esc_attr( $sfc_phone_number ), esc_html( $sfc_phone_number ) ); | |
printf('<div><a class="sfc_phone_email" href="mailto:%s"><i class="fa fa-envelope mr-4"></i>%s</a></div></div>', esc_attr( $sfc_phone_email ), esc_html( $sfc_phone_email ) ); | |
} |
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
.sfc_phone_email i { | |
margin-right: 10px; | |
} | |
.sfc_phone_email a { | |
color: #111; | |
text-decoration: none; | |
margin-bottom: 6px; | |
display: inline-block; | |
transition: all 0.3s ease; | |
font-size: 16px; | |
font-weight: 600; | |
} | |
.sfc_phone_email a:hover { | |
color: #f00; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
[Preview Backend]


[Preview Frontend]