Support » Plugin: WooCommerce » If description tab empty show custom field

  • Support,

    I’ve looked extensively and cannot work this out.

    If there is nothing in the description field, I want to show a custom field.

    We’ve linked WooCommerce with LightSpeed and it’s a bit of a mess how the data has been pulled in, but that’s for another day.

    At the moment, if the description field is empty, it hides the tab which is great for most cases.

    However, on my instance I want to say that if the Description is empty then load my custom field.

    I have created a function to show the custom field (which works fine, pulls in what I need if not in the if statement)

    function showSomething() {
    
     global $post;
     $post_id = $post->ID;
     $postMeta = get_post_meta($post_id, '_wclsi_ls_obj', true);
     $CustomFieldValues = $postMeta->CustomFieldValues->CustomFieldValue->value;
    
      print_r ($CustomFieldValues);
    
    }

    Then I thought I would simply do a basic if statement as the below but doesn’t work with the function.

    <?php if(empty($tabs['description'])) { ?> <?php showSomething(); ?><?php } ?>

    If I change the condition to true, it works (but obviously in the wrong section.

    <?php if(!empty($tabs['description'])) { ?> <footer> <?php showSomething(); ?><?php echo 'something'?> </footer> <?php } ?>

    If I replace the function showSomething in the if statement for a simple echo request, the logic works.

    <?php if(empty($tabs['description'])) { ?> <footer> <?php echo 'hello';  ?></footer> <?php } ?>

    Any thoughts please?

    Many thanks,

    https://wordpress.org/plugins/woocommerce/

Viewing 14 replies - 1 through 14 (of 14 total)
  • Plugin Support Caleb Burks

    (@icaleb)

    Automattic Happiness Engineer

    If I replace the function showSomething in the if statement for a simple echo request, the logic works.

    This shows that there is something wrong with your function. Make sure the post meta key is correct, and then you should double check what you are doing when assigning a value to $CustomFieldValues. That part doesn’t look correct.

    This type of customization is outside the realm of support we can provide here though. If you can’t quite figure out what code you need, you will need to hire a developer:

    http://jobs.wordpress.net/
    https://codeable.io/

    Caleb, thanks very much for your reply.

    Say for instance if I swap my function to <?php the_content(); ?> then the issue is still there i’m afraid and behaving as it would with the function I created.

    <?php if(empty($tabs['description'])) { ?> <?php the_content(); ?><?php } ?>

    Additionally, how can I turn it off so that if something is empty in a field, hide the tab?

    Many thanks,

    Basically this is what I am trying to do;

    <?php
    
    if (empty($tabs['description'])) {
    
      //if description tab empty then show the contents of a custom function
      <?php showSomething((); ?>
    
    } else {
      //show the contents from the description tab
      <?php the_content(); ?>
    
    }; 
    
    ?>

    🙂

    Plugin Author Mike Jolley

    (@mikejolley)

    global $post;
    
    if ( empty( $post->post_content ) ) {
    
    ......

    Hey Mike,

    Thank you very much for your response.

    Not working sadly.

    I am placing the code in the description tab.

    <?php
    /**
     * Description tab
     *
     * @author 		WooThemes
     * @package 	WooCommerce/Templates
     * @version     2.0.0
     */
    
    if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
    
    global $woocommerce, $post ;
    
    ?>
    
    <?php
    
    if( empty( $post->post_content ) ) { ?>
    
    // show custom function //
    
    <?php } else { ?>
    
    <?php the_content(); ?>
    
    <?php } ?>

    Just doesn’t like it.

    If you look at the screen grab below, it shows what I should be expecting, but it’s simply showing the description as blank on a product which is empty, when it should be showing the bespoke function (custom field)

    http://grab.by/RHFe

    Thanks again, really appreciate it. Can send you some beers 🙂

    Thank you,

    Plugin Support Caleb Burks

    (@icaleb)

    Automattic Happiness Engineer

    Caleb,

    Thank you for the reply. I had read otherwise, I did look on WooCommerce’s documentation but it wasn’t immediately obvious. > https://gist.github.com/bhongy/6761732

    If that is the case, any thoughts on how I can best achieve what I am looking for? Logically should not be hard and on raw PHP it would be super easy to implement but I just can’t get it working on WP.

    If you were looking to change the description contents if empty for some custom text/function, how would you go about it?

    Thanks,

    Gabi.

    Plugin Support Caleb Burks

    (@icaleb)

    Automattic Happiness Engineer

    You need to override this function in your child theme: https://github.com/woothemes/woocommerce/blob/master/includes/wc-template-functions.php#L1105 – and get rid of the if ( $post->post_content ) { conditional.

    Then you can do the conditional code inside of the template.

    Caleb,

    Thanks for your reply.

    This is the code that I wrote earlier, trying to get around not being able to query the_content() if empty.

    /* Creating a new tab called Description2 this is where we are going to add the custom field.
    	If this is true, it will then call cke_custom_tab_content() and execute, ideally then hiding the description tab, as the custom field superseeds it's information, but we can't have two tabs.
    */
    add_filter( 'woocommerce_product_tabs', 'bhww_woo_extra_tabs' );
    
    	function bhww_woo_extra_tabs( $tabs ) {
    
    		global $post, $post_id;
    		$modifiedtabs = get_post_meta( $post->ID, '_wclsi_ls_obj', true );
    
    		if ( !empty( $modifiedtabs ) ) {
    
    			$tabs['tab1'] = array(
    				'title'    => __( 'Custom-Field-Description', 'woocommerce' ),
    				'priority' => 15,
    				'callback' => 'cke_custom_tab_content'
    
    			);
    			//unset( $tabs['description'] );
    		}
    
    		return $tabs;
    
    	}
    
    /* In here we need to somehow remove the description tab if $mansku is not empty
    	 (trying to get around not being able to query the_content() if empty.
    */
    	function cke_custom_tab_content() {
    
    		  global $post;
    		  $post_id = $post->ID;
    
    		  $postMeta = get_post_meta($post_id, '_wclsi_ls_obj', true);
    		  $mansku = $postMeta->manufacturerSku;
    
    		if ( !empty( $mansku ) ) {
    
    			echo $mansku;
    
    			} else {
    
                echo 'empty';
    
    		};  
    
       }
    Plugin Support Caleb Burks

    (@icaleb)

    Automattic Happiness Engineer

    Still, I would override the woocommerce_default_product_tabs function. I don’t think you are using the callback correctly there.

    Alternatively, you can just remove the description tab entirely and create your own version of it.

    If you can’t quite figure out the exact code you need, you’ll need to hire another developer:

    http://jobs.wordpress.net/
    https://codeable.io/

    Thanks for the reply.

    I’ll double check the callback, the expected output without the function works ok.

    It’s an interesting issue, logically should be relatively simple.

    I guess I am missing something.

    As for those job boards, I have been burnt several times on there, not been a great experience.

    Thanks

    <?php
    function woocommerce_default_product_tabs( $tabs = array() ) {
    global $product, $post;
    // Description tab – shows product content

    $tabs[‘description’] = array(
    ‘title’ => __( ‘Description’, ‘woocommerce’ ),
    ‘priority’ => 10,
    ‘callback’ => ‘woocommerce_product_description_tab’,
    );

    return $tabs;
    }
    ?>

    put previous snippet in child theme functions.php file

    arsnovum

    (@arsnovum)

    I had the situation that I like to change the name of a TAB depending on language.

    This is my solution:

    if (empty($tabs['description'])) {
      //if description tab empty then show the contents of a custom function
    	$tabs['additional_information']['title'] = __( $tabtext1 );	// Rename the additional information tab
    	$tabs['additional_information']['priority'] = 5;	// Rename the additional information tab
    	return $tabs;
    }
    else {
      //show the contents from the description tab
    	$tabs['description']['priority'] = 15;	// Rename the additional information tab
    	$tabs['description']['title'] = __( $tabtext2 );	// Rename the description tab
    	$tabs['additional_information']['title'] = __( $tabtext1 );	// Rename the additional information tab
    	$tabs['additional_information']['priority'] = 5;	// Rename the additional information tab
    	return $tabs;
    }

    I put this code into functions.php

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘If description tab empty show custom field’ is closed to new replies.