IT Blog

  • Blog
  • Technology
    • Technology
    • Architecture
    • CMS
    • CRM
    • Web
    • DotNET
    • Python
    • Database
    • BI
    • Program Language
  • Users
    • Login
    • Register
    • Forgot Password?
  • ENEN
    • 中文中文
    • ENEN
Experience IT
In a World of Technology, People Make the Difference.
  1. Home
  2. Technology
  3. CMS
  4. Wordpress
  5. Plugin
  6. Hide empty custom fields in Ultimate product catelog plugin

Hide empty custom fields in Ultimate product catelog plugin

2018-10-28 886 Views 0 Like 0 Comments

Table of Contents

  • Category view
    • Solution 1
    • Or:
    • Solution 2 (with Custom CSS & JS plugin)
    • Solution 3
  • Product detail
    • Solution

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

 

Loading

error
fb-share-icon
Tweet
fb-share-icon
IT Team
Author: IT Team

Tags: None
Last updated:2018-10-28

IT Team

This person is lazy and left nothing

Like
< Previous
Next >

Comments

Cancel reply
Newest Hotspots Random
Newest Hotspots Random
Rich editor not working Making web page scroll down automatically Getting data from Dapper result All Unicode Chars How to keep and display contact form 7 data Common Regular Expressions
Audio Symbols Fading effect in html page New team member have no permission to access - Dynamics 365 Symbols: Stars Data visualization with Flask Generating Test Data with SQL Scripts
Categories
  • Architecture
  • BI
  • C#
  • CSS
  • Database
  • DotNET
  • Hosting
  • HTML
  • JavaScript
  • PHP
  • Program Language
  • Python
  • Security
  • SEO
  • Technology
  • Web
  • Wordpress

COPYRIGHT © 2021 Hostlike IT Blog. All rights reserved.

This site is supported by Hostlike.com