Hide empty custom fields in Ultimate product catelog plugin

Hide empty custom fields in Ultimate product catelog plugin

Category view

Each product has its own attribute set. Once we setup all custom attributes for all products, and enable the visibility for category view, each individual product in category view will show all of them regardless some product may not have the attribute. So we have to hide the fields that are not used for the product.

category-view-improvement

Solution 1

Display the attribute only when it has value.

Code file: /wp-content/plugins/ultimate-product-catalogue/Functions/Shortcodes.php

Function: AddCustomFields($ProductID, $Layout)

Original code:

foreach ($Fields as $Field) {
	$Meta = $wpdb->get_row("SELECT Meta_Value FROM $fields_meta_table_name WHERE Field_ID='" . $Field->Field_ID . "' AND Item_ID='" . $ProductID . "'");
	if (is_object($Meta)) {$Meta_Value = UPCP_Decode_CF_Commas($Meta->Meta_Value);}
	else {$Meta_Value = "";}
	if ($Meta_Value != "" or $Custom_Fields_Blank != "Yes") {
		if ($Field->Field_Type == "file") {
			$CustomFieldString .= $AddBreak . $Field->Field_Name . ": ";
			$CustomFieldString .= "<a href='" . $upload_dir['baseurl'] . "/upcp-product-file-uploads/" .$Meta_Value . "' download>" . $Meta_Value . "</a>";
		}
		else {$CustomFieldString .= $AddBreak . "<span class='upcp-cf-label'>" . $Field->Field_Name . ": </span><span class='upcp-cf-value'>" . str_replace(",", ", ", do_shortcode($Meta_Value) ) . "</span>";}
		$AddBreak = "<br />";
	}
}

Changed code:

foreach ($Fields as $Field) {
	$Meta = $wpdb->get_row("SELECT Meta_Value FROM $fields_meta_table_name WHERE Field_ID='" . $Field->Field_ID . "' AND Item_ID='" . $ProductID . "'");
	if (is_object($Meta)) {$Meta_Value = UPCP_Decode_CF_Commas($Meta->Meta_Value);}
	else {$Meta_Value = "";}
	if ($Meta_Value != "" or $Custom_Fields_Blank != "Yes") {
		if ($Field->Field_Type == "file") {
			$CustomFieldString .= $AddBreak . $Field->Field_Name . ": ";
			$CustomFieldString .= "<a href='" . $upload_dir['baseurl'] . "/upcp-product-file-uploads/" .$Meta_Value . "' download>" . $Meta_Value . "</a>";
		}
		else {
            if($Meta_Value!="")
                $CustomFieldString .= $AddBreak . "<span class='upcp-cf-label'>" . $Field->Field_Name . ": </span><span class='upcp-cf-value'>" . str_replace(",", ", ", do_shortcode($Meta_Value) ) . "</span>";
		}
		$AddBreak = "<br />";
	}
}

Or:

foreach ($Fields as $Field) {
	$Meta = $wpdb->get_row("SELECT Meta_Value FROM $fields_meta_table_name WHERE Field_ID='" . $Field->Field_ID . "' AND Item_ID='" . $ProductID . "'");
	if (is_object($Meta)) {$Meta_Value = UPCP_Decode_CF_Commas($Meta->Meta_Value);}
	else {$Meta_Value = "";}
	if ($Meta_Value != "" or $Custom_Fields_Blank == "Yes") {
		if ($Field->Field_Type == "file") {
			$CustomFieldString .= $AddBreak . $Field->Field_Name . ": ";
			$CustomFieldString .= "<a href='" . $upload_dir['baseurl'] . "/upcp-product-file-uploads/" .$Meta_Value . "' download>" . $Meta_Value . "</a>";
		}
		else {$CustomFieldString .= $AddBreak . "<span class='upcp-cf-label'>" . $Field->Field_Name . ": </span><span class='upcp-cf-value'>" . str_replace(",", ", ", do_shortcode($Meta_Value) ) . "</span>";}
		$AddBreak = "<br />";
	}
}

Solution 2 (with Custom CSS & JS plugin)

Add N/A to it with stylesheet. This solution does not hide the field, but remedy the empty value once solution 1 change being override by updating the plugin.

span.upcp-cf-value:empty:before{
    content:' N/A';
}

showing-NA-if-empty-value

Solution 3

Hide the element using JavaScript (with Custom CSS & JS plugin).

$=jQuery;
$(document).ready(function(){
   var fld = $('.upcp-cf-value');
   fld.each(function(a,b){
     if(b.innerText=='')
       b.parentElement.hidden=true;
   });
});

Apparently, the solution 3 is the best solution. The solution 1 is not recommended due to plugin updates override; second one would confuse the users.

Product detail

The invalid attributes for certain product has no value, we need to hide them from product detail page.

product detail improvement

Solution

Display the attribute only when it has value.

Code file: /wp-content/plugins/ultimate-product-catalogue/Functions/Shortcodes.php

Function: SingleProductPage ( )

Original code:

foreach ($Fields as $Field) {
	$Value = $wpdb->get_row("SELECT Meta_Value FROM $fields_meta_table_name WHERE Item_ID='" . $Product->Item_ID . "'and Field_ID='" . $Field->Field_ID ."'");
	if ($Custom_Fields_Blank != "Yes" or $Value->Meta_Value != "") {
		if (is_object($Value)) { $Meta_Value = UPCP_Decode_CF_Commas($Value->Meta_Value);}
		else {$Meta_Value = "";}
		if ($Field->Field_Type == "file") {
			$ProductString .= "<div class='upcp-tab-title'>" . $Field->Field_Name . ":</div>";
			$ProductString .= "<a href='" . $upload_dir['baseurl'] . "/upcp-product-file-uploads/" . $Meta_Value . "' download>" . $Meta_Value . "</a><br>";
		}
		elseif ($Field->Field_Type == "checkbox") {$ProductString .= "<div class='upcp-tab-title'>" . $Field->Field_Name . ":</div><span>" . str_replace(",", ", ", $Meta_Value) . "</span><br>";}
		elseif ($Field->Field_Type == "link") {$ProductString .= "<div class='upcp-tab-title'>" . $Field->Field_Name . ":</div><span><a href='" . $Meta_Value . "'>" . $Meta_Value . "</a></span><br>";}
		else {$ProductString .= "<div class='upcp-tab-title'>" . $Field->Field_Name . ":</div><span>" . do_shortcode($Meta_Value) . "</span><br>";}
	}
}

Changed code:

foreach ($Fields as $Field) {
	$Value = $wpdb->get_row("SELECT Meta_Value FROM $fields_meta_table_name WHERE Item_ID='" . $Product->Item_ID . "'and Field_ID='" . $Field->Field_ID ."'");
	if ($Custom_Fields_Blank != "Yes" and $Value->Meta_Value != "") {
		if (is_object($Value)) { $Meta_Value = UPCP_Decode_CF_Commas($Value->Meta_Value);}
		else {$Meta_Value = "";}
		if ($Field->Field_Type == "file") {
			$ProductString .= "<div class='upcp-tab-title'>" . $Field->Field_Name . ":</div>";
			$ProductString .= "<a href='" . $upload_dir['baseurl'] . "/upcp-product-file-uploads/" . $Meta_Value . "' download>" . $Meta_Value . "</a><br>";
		}
		elseif ($Field->Field_Type == "checkbox") {$ProductString .= "<div class='upcp-tab-title'>" . $Field->Field_Name . ":</div><span>" . str_replace(",", ", ", $Meta_Value) . "</span><br>";}
		elseif ($Field->Field_Type == "link") {$ProductString .= "<div class='upcp-tab-title'>" . $Field->Field_Name . ":</div><span><a href='" . $Meta_Value . "'>" . $Meta_Value . "</a></span><br>";}
		else {$ProductString .= "<div class='upcp-tab-title'>" . $Field->Field_Name . ":</div><span>" . do_shortcode($Meta_Value) . "</span><br>";}
	}
}

Reference: Product Catalogue plugin issue fixes

 

 395 total views

Author: Albert

Leave a Reply