if then statement in php function - php

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

Related

Adding multiple tabs for specific products to WooCommerce single product pages

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>';
}

Adding custom tab to single product page for specific product type in WooCommerce

Based on this code, I want to create custom tab for just variable products in WooCommerce 4.4.1.
But unfortunately, this custom tab is added to all product types, is it any way to solve this problem?
If I am wrong please correct me.
add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab' );
function woo_new_product_tab( $tabs ) {
// Adds the new tab for variable product type
global $product;
if( $product->is_type( 'variable' ) ) {
$tabs['test_tab'] = array(
'title' => 'features',
'priority' => 50,
'class' => array('general_tab', 'show_if_variable'),
'callback' => 'woo_new_product_tab_content'
);
}
return $tabs;
}
To find the error you can perform some extra checks and print the product type.
The else conditions can be removed after testing.
function filter_woocommerce_product_tabs( $tabs ) {
// Get the global product object
global $product;
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// Get type
$product_type = $product->get_type();
// Compare
if ( $product_type == 'variable' ) {
$tabs['test_tab'] = array(
'title' => 'features',
'priority' => 50,
'callback' => 'woo_new_product_tab_content'
);
} else {
echo 'DEBUG: ' . $product_type;
}
} else {
echo 'NOT a WC product';
}
return $tabs;
}
add_filter( 'woocommerce_product_tabs', 'filter_woocommerce_product_tabs', 10, 1 );
// Callback
function woo_new_product_tab_content() {
// The new tab content
echo '<h2>New Product Tab</h2>';
echo '<p>Here\'s your new product tab.</p>';
}

adding custom field to woo product page (only for certain product id)

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;
}

Add custom HTML in New Tab on Product Page - WooCommerce

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

Eventbrite API not returning Events

So a site I am working on for a client, http://atasteofartistry.com, needs to have EventBrite integration. It is a wordpress install running a Genesis theme.
Right now, in functions.php, I have this:
//* EventBrite
require 'Eventbrite.php';
$eb_client = new Eventbrite( array('app_key'=>'(REDACTED)',
'user_key'=>'(REDACTED)'));
$arr = array('user'=>'atasteofartistry#gmail.com',
'event_statuses'=>'live,started'
);
e$events = $eb_client->user_list_events($arr);
and I have the call in a widget listed like this:
<h1>My Event List:</h1>
<?= Eventbrite::eventList( $events, 'eventListRow'); ?>
</div>
On the site, I am not getting the events. The Eventbrite.php API client is initializing, because I get the "No Events" message, but I just cannot pass anything through to it.
I've been staring at it for 8 hours now and need help. So HELP!
I'm using the Eventbrite PHP library from Github.
https://github.com/ryanjarvinen/eventbrite.php
How to fix it would be great, but I can't even figure out how to troubleshoot it.
Many thanks,
Joe
FUNCTIONS.PHP
<?php
//* Start the engine
include_once( get_template_directory() . '/lib/init.php' );
//* Setup Theme
include_once( get_stylesheet_directory() . '/lib/theme-defaults.php' );
//* EventBrite
require 'Eventbrite.php';
add_action( 'genesis_before', 'eventbrite_event_list');
function eventbrite_event_list() {
$eb_client = new Eventbrite( array('app_key'=>'ECOC6TZS3JJVXLJ2MX',
'user_key'=>'1409014384112928178221'));
$arr = array('user'=>'atasteofartistry#gmail.com',
'event_statuses'=>'live,started'
);
try {
$events = $eb_client->user_list_events();
} catch ( Exception $e ) {
// Be sure to plan for potential error cases
// so that your application can respond appropriately
//var_dump($e);
$events = array();
}
}
//* Set Localization (do not remove)
load_child_theme_textdomain( 'daily-dish', apply_filters( 'child_theme_textdomain', get_stylesheet_directory() . '/languages', 'daily-dish' ) );
//* Child theme (do not remove)
define( 'CHILD_THEME_NAME', __( 'Daily Dish Pro Theme', 'daily-dish' ) );
define( 'CHILD_THEME_URL', 'http://my.studiopress.com/themes/daily-dish/' );
define( 'CHILD_THEME_VERSION', '1.0' );
//* Enqueue scripts and styles
add_action( 'wp_enqueue_scripts', 'daily_dish_enqueue_scripts_styles' );
function daily_dish_enqueue_scripts_styles() {
wp_enqueue_script( 'daily-dish-responsive-menu', get_bloginfo( 'stylesheet_directory' ) . '/js/responsive-menu.js', array( 'jquery' ), '1.0.0' );
wp_enqueue_style( 'dashicons' );
wp_enqueue_style( 'daily-dish-google-fonts', '//fonts.googleapis.com/css?family=Lato:400,700,900|Playfair+Display:400|Tangerine:400,700', array(), CHILD_THEME_VERSION );
}
add_filter( 'genesis_seo_title', 'custom_genesis_seo_title', 10, 1 );
/**
* Replace Site Title text entered in Settings > Reading with custom HTML.
* #author Sridhar Katakam
* #link http://sridharkatakam.com/replace-site-title-text-custom-html-genesis/
*
* #param string original title text
* #return string modified title HTML
*/
function custom_genesis_seo_title( $title ) {
$title = '<h1 itemprop="headline" class="site-title"><a title="Homepage" href="' . get_bloginfo('url') . '">a taste of <span class="artistry">ARTISTRY</span></a></h1>';
return $title;
}
//* Add HTML5 markup structure
add_theme_support( 'html5', array( 'search-form', 'comment-form', 'comment-list', 'gallery', 'caption', ) );
//* Add viewport meta tag for mobile browsers
add_theme_support( 'genesis-responsive-viewport' );
//* Add support for custom background
add_theme_support( 'custom-background', array(
'default-attachment' => 'fixed',
'default-color' => 'ffffff',
'default-image' => get_stylesheet_directory_uri() . '/images/bg.png',
'default-repeat' => 'repeat',
'default-position-x' => 'left',
) );
//* Add new image sizes
add_image_size( 'daily-dish-featured', 720, 470, TRUE );
add_image_size( 'daily-dish-archive', 340, 200, TRUE );
add_image_size( 'daily-dish-sidebar', 100, 100, TRUE );
//* Add support for custom header
add_theme_support( 'custom-header', array(
'header-selector' => '.site-title a',
'header-text' => false,
'height' => 80,
'width' => 400,
) );
/** Enable shortcodes to be used in widgets */
add_filter( 'widget_text', 'shortcode_unautop');
add_filter( 'widget_text', 'do_shortcode');
//* Unregister the header right widget area
unregister_sidebar( 'header-right' );
//* Remove navigation meta box
add_action( 'genesis_theme_settings_metaboxes', 'daily_dish_remove_genesis_metaboxes' );
function daily_dish_remove_genesis_metaboxes( $_genesis_theme_settings_pagehook ) {
remove_meta_box( 'genesis-theme-settings-nav', $_genesis_theme_settings_pagehook, 'main' );
}
//* Remove output of primary navigation right extras
remove_filter( 'genesis_nav_items', 'genesis_nav_right', 10, 2 );
remove_filter( 'wp_nav_menu_items', 'genesis_nav_right', 10, 2 );
//* Reposition the secondary navigation menu
remove_action( 'genesis_after_header', 'genesis_do_subnav' );
add_action( 'genesis_before_header', 'genesis_do_subnav' );
//* Hook before header widget area above header
add_action( 'genesis_before', 'daily_dish_before_header' );
function daily_dish_before_header() {
genesis_widget_area( 'before-header', array(
'before' => '<div class="before-header" class="widget-area"><div class="wrap">',
'after' => '</div></div>',
) );
}
add_theme_support( 'genesis-structural-wraps', array(
'header',
'nav',
'subnav',
'site-inner',
'footer-widgets',
'footer'
) );
//* Customize the entry meta in the entry header
add_filter( 'genesis_post_info', 'daily_dish_single_post_info_filter' );
function daily_dish_single_post_info_filter( $post_info ) {
$post_info = '[post_date] [post_author_posts_link] [post_comments] [post_edit]';
return $post_info;
}
//* Customize the entry meta in the entry footer
add_filter( 'genesis_post_meta', 'daily_dish_post_meta_filter' );
function daily_dish_post_meta_filter($post_meta) {
$post_meta = '[post_categories before=""] [post_tags before=""]';
return $post_meta;
}
//* Remove comment form allowed tags
add_filter( 'comment_form_defaults', 'daily_dish_remove_comment_form_allowed_tags' );
function daily_dish_remove_comment_form_allowed_tags( $defaults ) {
$defaults['comment_notes_after'] = '';
return $defaults;
}
//* Walled Garden
// function walled_garden()
// {
// if( ! is_user_logged_in() )
// wp_redirect( '/wp-login.php' );
// }
// add_action( 'get_header', 'walled_garden' );
//* Add images to Site Header
add_action( 'genesis_site_title','add_header_images',8);
function add_header_images() {
?>
<span><img src="../wp-content/themes/daily-dish-pro/images/wine-glass.png" class="header-glass" /></span>
<span><img src="../wp-content/themes/daily-dish-pro/images/splatter.png" class="splatter" /></span>
<?php
}
//* Modify the size of the Gravatar in the author box
add_filter( 'genesis_author_box_gravatar_size', 'daily_dish_author_box_gravatar' );
function daily_dish_author_box_gravatar( $size ) {
return 180;
}
//* Modify the size of the Gravatar in the entry comments
add_filter( 'genesis_comment_list_args', 'daily_dish_comments_gravatar' );
function daily_dish_comments_gravatar( $args ) {
$args['avatar_size'] = 96;
return $args;
}
//* Hook before footer widget area below footer widgets
add_action( 'genesis_before_footer', 'daily_dish_before_footer_widgets', 5 );
function daily_dish_before_footer_widgets() {
genesis_widget_area( 'before-footer-widgets', array(
'before' => '<div class="before-footer-widgets" class="widget-area"><div class="wrap">',
'after' => '</div></div>',
) );
}
//* Hook after footer widget area below footer
add_action( 'genesis_after', 'daily_dish_after_footer' );
function daily_dish_after_footer() {
genesis_widget_area( 'after-footer', array(
'before' => '<div class="after-footer" class="widget-area"><div class="wrap">',
'after' => '</div></div>',
) );
}
//* Add support for 3-column footer widgets
add_theme_support( 'genesis-footer-widgets', 3 );
//* Add support for after entry widget
add_theme_support( 'genesis-after-entry-widget-area' );
//* Relocate after entry widget
remove_action( 'genesis_after_entry', 'genesis_after_entry_widget_area' );
add_action( 'genesis_after_entry', 'genesis_after_entry_widget_area', 5 );
//* Register widget areas
genesis_register_sidebar( array(
'id' => 'before-header',
'name' => __( 'Before Header', 'daily-dish' ),
'description' => __( 'This is the before header widget area.', 'daily-dish' ),
) );
genesis_register_sidebar( array(
'id' => 'home-top',
'name' => __( 'Home - Top', 'daily-dish' ),
'description' => __( 'This is the top section of the homepage.', 'daily-dish' ),
) );
genesis_register_sidebar( array(
'id' => 'home-middle',
'name' => __( 'Home - Middle', 'daily-dish' ),
'description' => __( 'This is the middle section of the homepage.', 'daily-dish' ),
) );
genesis_register_sidebar( array(
'id' => 'home-bottom',
'name' => __( 'Home - Bottom', 'daily-dish' ),
'description' => __( 'This is the bottom section of the homepage.', 'daily-dish' ),
) );
genesis_register_sidebar( array(
'id' => 'before-footer-widgets',
'name' => __( 'Before Footer Widgets', 'daily-dish' ),
'description' => __( 'This is the before footer widgets section.', 'daily-dish' ),
) );
genesis_register_sidebar( array(
'id' => 'after-footer',
'name' => __( 'After Footer', 'daily-dish' ),
'description' => __( 'This is the after footer section.', 'daily-dish' ),
) );

Categories