I am trying to Add a new tab on single product page. My below codes works well:-
add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab' );
function woo_new_product_tab( $tabs ) {
// Adds the new tab
$tabs['desc_tab'] = array(
'title' => __( 'Additional Information', 'woocommerce' ),
'priority' => 50,
'callback' => 'woo_new_product_tab_content'
);
}
I want to insert some html data in the 'title' =>
For example:
add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab' );
function woo_new_product_tab( $tabs ) {
// Adds the new tab
$tabs['desc_tab'] = array(
'title' => __( 'Additional Information <img src="'.$image.'"/>', 'woocommerce' ),
'priority' => 50,
'callback' => 'woo_new_product_tab_content'
);
}
The above codes outputs the full html source instead the image.
Any help will be really appreciated.
Thanks
Here you can:
add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab' );
function woo_new_product_tab( $tabs ) {
// Adds the new tab
$tabs['desc_tab'] = array(
'title' => __( 'Additional Information', 'woocommerce' ),
'priority' => 50,
'callback' => 'woo_new_product_tab_content'
);
}
function woo_new_product_tab_content() {
// The new tab content
echo '<p>Lorem Ipsum</p>';
}
I know that this thread is old, but I have had the same problem. You can solve it with a custom filter for the tab title.
So you have this:
add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab' );
function woo_new_product_tab( $tabs ) {
// Adds the new tab
$tabs['desc_tab'] = array(
'title' => __( 'Additional Information <img src="'.$image.'"/>', 'woocommerce' ),
'priority' => 50,
'callback' => 'woo_new_product_tab_content'
);
}
Now you got a tab called "desc_tab". The problem is that WooCommerce does not escape the HTML part of it, but you can use a filter, so WooCommerce translates the tab title to HTML:
add_filter('woocommerce_product_desc_tab_tab_title', 'decode_html_tab', 10, 2);
function decode_html_tab($title, $key) {
$title = html_entity_decode( $title, ENT_QUOTES );
return $title;
}
You have to pay attention to the title of the filter. It is a composition of 'woocommerce_' + the tab title + '_tab_title'.
I hope
Related
I'm trying to show extra product tabs on specific products using $post_id
I'm not sure if it's done it right? this way works for other small snippets i've wrote.
The product tabs work if i have them display on all product, but i want to limit some to specific products.
My code attempt:
add_filter( 'woocommerce_product_tabs', 'artwork_product_tab' );
function artwork_product_tab( $tabs, $post_id ) {
if( in_array( $post_id, array( 8125 ) ) ){
// Adds the new tab
return $tabs['artwork_guidelines'] = array(
'title' => __( 'Artwork Guidelines', 'woocommerce' ),
'priority' => 50,
'callback' => 'artwork_product_tab_content'
);
}
$tabs['standard_sizes'] = array(
'title' => __( 'Standard Sizes', 'woocommerce' ),
'priority' => 60,
'callback' => 'standard_sizes_product_tab_content'
);
return $tabs;
}
Any help is appreciated!
$post_id is not passed to the woocommerce_product_tabs filter hook.
You can use global $product & $product->get_id() instead.
So you get:
function filter_woocommerce_product_tabs( $tabs ) {
global $product;
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// Get product ID
$product_id = $product->get_id();
// Compare
if ( in_array( $product_id, array( 8125, 30, 815 ) ) ) {
$tabs['artwork_guidelines'] = array(
'title' => __( 'Artwork Guidelines', 'woocommerce' ),
'priority' => 50,
'callback' => 'artwork_product_tab_content'
);
$tabs['standard_sizes'] = array(
'title' => __( 'Standard Sizes', 'woocommerce' ),
'priority' => 60,
'callback' => 'standard_sizes_product_tab_content'
);
}
}
return $tabs;
}
add_filter( 'woocommerce_product_tabs', 'filter_woocommerce_product_tabs', 100, 1 );
// New Tab contents
function artwork_product_tab_content() {
echo '<p>artwork_product_tab_content</p>';
}
function standard_sizes_product_tab_content() {
echo '<p>standard_sizes_product_tab_content</p>';
}
I created a custom tab (My Favorites) for my Woocommerce my account page and placed the below code in my child theme's functions.php file. When I did that, the custom tab is ordered at the bottom of the rest of the tabs.
So I was wondering, is a way to reorder it further up the list of tabs?
Specifically I would like to reorder my custom tab (My Favorites) above the "Account Details" tab.
image link
function add_myfavorites_endpoint() {
add_rewrite_endpoint( 'myfavorites', EP_ROOT | EP_PAGES );
}
add_action( 'init', 'add_myfavorites_endpoint' );
function myfavorites_query_vars( $vars ) {
$vars[] = 'myfavorites';
return $vars;
}
add_filter( 'query_vars', 'myfavorites_query_vars', 0 );
function add_myfavorites_link_my_account( $items ) {
$items['myfavorites'] = 'My Favorites';
return $items;
}
add_filter( 'woocommerce_account_menu_items', 'add_myfavorites_link_my_account' );
function myfavorites_content() {
echo '<h3>My Favorites</h3><p>';
echo do_shortcode( ' [my_content_shortcode] ' );
}
add_action( 'woocommerce_account_myfavorites_endpoint', 'myfavorites_content' );
// Rename, re-order my account menu items
function fwuk_reorder_my_account_menu() {
$neworder = array(
'dashboard' => __( 'Dashboard', 'woocommerce' ),
'orders' => __( 'Previous Orders', 'woocommerce' ),
'wishlist-link' => __( 'Wishlist', 'woocommerce' ),
'edit-address' => __( 'Addresses', 'woocommerce' ),
'edit-account' => __( 'Account Details', 'woocommerce' ),
'customer-logout' => __( 'Logout', 'woocommerce' ),
);
return $neworder;
}
add_filter ( 'woocommerce_account_menu_items', 'fwuk_reorder_my_account_menu' );
I need to add custom tab on product page, but i want it show it only for some product ids.
Here my code for adding the custom tab on front end.
add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab' );
function woo_new_product_tab( $tabs ) {
$tabs['desc_tab'] = array(
'title' => __( 'Additional Information', 'woocommerce' ),
'priority' => 50,
'callback' => 'woo_new_product_tab_content'
);
return $tabs;
}
and then i should add below function too for displaying the content.
function woo_new_product_tab_content() {
echo '<p>some text</p>';
}
but now these function work for all product ids. I want to load this two function only for some product id, could someone help me?
I tired to add if(is_product() && get_the_id() == 8) on both functions but $tabs not appear on others product ids page , ( on product id "8" $tabs worked well )
the problem of that code wast about to forget return $tabs
if(is_product()) {
//if(is_product())) {
return $tabs;
}
return $tabs;
}
so the final code could be like this.
function woo_new_product_tab( $tabs ) {
if(condition) {
$tabs['desc_tab'] = array(
'title' => __( 'Additional Information', 'woocommerce' ),
'priority' => 50,
'callback' => 'woo_new_product_tab_content'
);
return $tabs;
}
return $tabs;
}
I'm trying to use the php code in the header but it does not work.
This is the code I am using.
add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab' );
function woo_new_product_tab( $tabs ) {
$tabs['test_tab'] = array(
'title' => __( '<span>Hot</span>' . 'Title' , 'woocommerce' ),
'priority' => 50,
'callback' => 'woo_new_product_tab_content'
);return $tabs;}
How can you use html for "Title"
It seems that printing "html" code does not work
when I used my code the title was printed as: "<span> Hot </ hot> Title"
Please help me, thanks.
Add this code in functions.php file.
add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab' );
function woo_new_product_tab( $tabs ) {
$tabs['test_tab'] = array(
'title' => __( 'Title' , 'woocommerce_tab_custom_title' ),
'priority' => 50,
'callback' => 'woo_new_product_tab_content'
);
return $tabs;
}
You should try to hook into wp_head using add_action like the following example:
add_action('wp_head', 'woo_new_product_tab');
function woo_new_product_tab() {
<?php
<title>
Hot
</title>
?>
}
I am a PHP beginner. I have trouble to making a basic if/then statement on function.
I add a woocommerce product tab 'Food_paring', I want to disable the tab when the field 'food_pairing" is empty/not set.
original code:
add_filter( 'woocommerce_product_tabs', 'new_product_tab' );
function new_product_tab( $tabs ) {
/* Adds the new tab */
$tabs['test_tab'] = array(
'title' => __( 'Food Pairing', 'woocommerce' ),
'priority' => 50,
'callback' => 'food_pairing_tab_content'
);
return $tabs; /* Return all tabs including the new New Custom Product Tab to display */
}
function food_pairing_tab_content() {
/* The new tab content */
echo '<h2>Food Pairing</h2><p id="tab-food-pairing">', get_post_meta( get_the_ID(), 'food_pairing', true ), '</p>';
}
Please check below code. Before add tab check if product meta has value or not global $post; if(get_post_meta($post->ID, 'food_pairing', true )){..}
add_filter( 'woocommerce_product_tabs', 'new_product_tab' );
function new_product_tab( $tabs ) {
global $post;
/* Adds the new tab */
if(get_post_meta($post->ID, 'food_pairing', true ))
{
$tabs['test_tab'] = array(
'title' => __( 'Food Pairing', 'woocommerce' ),
'priority' => 50,
'callback' => 'food_pairing_tab_content'
);
}
return $tabs; /* Return all tabs including the new New Custom Product Tab to display */
}
function food_pairing_tab_content() {
/* The new tab content */
echo '<h2>Food Pairing</h2><p id="tab-food-pairing">', get_post_meta( get_the_ID(), 'food_pairing', true ), '</p>';
}
add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab' );
function woo_new_product_tab( $tabs ) {
global $woocommerce;
$tabs['desc_tab'] = array(
'title' => __( 'Ingredients', 'woocommerce' ),
'priority' => 50,
'callback' => 'woo_new_product_tab_content' ,
);
return $tabs;
}
function woo_new_product_tab_content() {
// The new tab content
echo '<p>Lorem Ipsum</p>';
echo $prod_id = get_the_ID();
echo'<p>'.get_post_meta($prod_id,'ingredients',true).'</p>';
}
this code is properly working... should try . Good Luck