Display Custom Fields according to Category - if-statement

I added Custom Fields to my WooCommerce Single Product Page, with this Code:
// DISPLAY CUSTOM FIELDS IN WOOCOMMERCE SINGLE PRODUCT PAGE
add_action('woocommerce_single_product_summary', 'wpsh_single_posts_custom_meta_fields', 10 );
function wpsh_single_posts_custom_meta_fields(){
$post_id = get_the_ID();
$post = get_post( $post_id );
$month_year = get_post_meta( $post->ID, 'month_year' ); // Date field
$art_size = get_post_meta( $post->ID, 'art_size' ); // Text field
// Date field
if(!empty($month_year)){
echo '<p class="creation-year"><span style="font-weight: bold; color: #4cbb17;">PRODUCT YEAR</span>: ' . $month_year[0] . '</p>';
}
// Text field
if(!empty($art_size)){
echo '<p class="art-dimension"> <span style="font-weight: bold; color: #4cbb17;">ART SIZE</span>: ' . $art_size[0] . '</p>';
}
echo '</p>';
}
--- and it gave me this result: https://snipboard.io/dVOLKv.jpg
But two things are missing from my Code and I don't know how to add them:
1.) Show those Custom Fields according to Category.Meaning that, show:
a.) ART SIZE and PRODUCT YEAR if Category is equal to Artworks.
b.) CRT PRICE and PRODUCT YEAR if Category is Equal to Bags.
c.) CRAFT SIZE, MATERIAL and WEIGHT if Category is Equal to Crafts.
2.) Hide Custom Fields if value is equal to Null. Meaning that, if these fields are not filled in, with a value, they should not appear.
How do I add these extra rules to my Code above?
Or, can anyone help me with a better Code that could be more flexible than the one I already have?
Regards.

You can use get_the_terms() to get the category list then filter out based on category id what to display.
Try out this sample code and replace the category id.
add_action('woocommerce_single_product_summary', 'wpsh_single_posts_custom_meta_fields', 10);
function wpsh_single_posts_custom_meta_fields()
{
$post_id = get_the_ID();
$post = get_post($post_id);
$month_year = get_post_meta($post->ID, 'month_year', true); // Date field
$art_size = get_post_meta($post->ID, 'art_size', true); // Text field
$term_obj_list = get_the_terms( $post->ID, 'product_cat' );
if(is_array($term_obj_list)){
foreach($term_obj_list as $term){
if($term->term_id == 22){ //here 22 is your category id
if ($month_year && !empty($month_year)) {
echo '<p class="creation-year"><span style="font-weight: bold; color: #4cbb17;">PRODUCT YEAR</span>: ' . $month_year . '</p>';
}
if ($art_size && !empty($art_size)) {
echo '<p class="art-dimension"> <span style="font-weight: bold; color: #4cbb17;">ART SIZE</span>: ' . $art_size . '</p>';
}
}
}
}
}

Related

Show all Category & Sub- Category in a particular page like as sitemap

I am New to opencart. I Just want to show all Category & Sub- Category in a
particular page like as sitemap, Which is liked by my Boss.
Check out this code. Just paste it in your desired controller.
$data['categories'] = array();
$categories = $this->model_catalog_category->getCategories(0);
foreach ($categories as $category) {
echo $category['name'] . '<br/>';
$children_data = array();
$children = $this->model_catalog_category->getCategories($category['category_id']);
foreach ($children as $child) {
echo ' --- ' . $child['name'] . '<br/>';
}
}

Grails comparing a list within a list in a gsp

I have a one-to-many relationship table where I am grabbing a list of members by a committee id. When the output is displayed I get the correct amount of members but the corresponding committee position type is being displayed as a list and not the individual position for that committee.
So I came up with the idea of comparing the committee position list with a param that I pass.
When I put in the if tag, nothing is displayed. When I remote the tag, I can see the output of my list and the param that I am pasting into it. I have used == and .equals() but get that same output of nothing.
What an I missing?
Legacy DB Using Oracle 11g, and Grails 2.3.3
Here is an example of my list within a list.
Here is my GSP code
<thead>
<tr class="revheaddark-c">
<!-- display all of the current members information -->
<th width="33%" class="revheaddark-t"><g:message code="trustee.lastName.label" default="Name" /></th>
<th width="33%" class="revheaddark-t"><g:message code="trustee.committees.label" default="Officers of the Board" /></th>
<th width="33%" class="revheaddark-t"><g:message code="trustee.hospital.label" default="Trustee Membership" /></th>
</tr>
</thead>
<g:each in="${trusteeInstanceList}" status="i" var="trusteeInstance">
<tr class="${i % 2 == 0 ? 'even' : 'cellshade'}">
<td><g:link action="contactInfo" params="[contactId:'${trusteeInstance.id}']" id="${trusteeInstance.id}">${fieldValue(bean: trusteeInstance, field: "salutation")}${fieldValue(bean: trusteeInstance, field: "firstName")} ${fieldValue(bean: trusteeInstance, field: "middleName")} ${fieldValue(bean: trusteeInstance, field: "lastName")}</g:link></td>
enter code here
<td><g:each in="${trusteeInstance.membership}">
<g:if test="${it.committees.id == params.committee }">
<%--<tr><td>--%>
${params.committee }, ${it.position }, ${it.committees.id }
<%--</td></tr>--%>
</g:if>
<td><g:each in="${trusteeInstance.board}">
<g:if test="${it.boardName.next() == null}">${it.boardName} </g:if>
<g:else>${it.boardName}, </g:else>
</g:each></td>
</tr>
</g:each>
Here is my controller
def members(){
params.max = Math.min(params.max ? params.int('max'): 15, 100)
def listCommittees = Committees.findAll("from Committees as c where c.hospital.id = '1'")
def indexSearch = Trustee.withCriteria(){
//search by First letter of lastName
if(params.letter){
ilike("lastName", "${params.letter}%")
}
//search by lastName
if(params.lastName){
ilike("lastName", "%${params.lastName}%")
}
//search by firstName
if(params.firstName){
ilike("firstName", "%${params.firstName}%")
}
//search by hospitalName
// if(params.hospital){
// board{
// eq "hospital.id", params.long('hospital')
// }
// }
//search by committeeId
if(params.committee){
//display only members will the board id
membership{
eq "committees.id", params.long('committee')
}
}
//search by Type
if(params.mType){
membership{
eq("memberType", "${params.mType}")
}
}
//search by boardName
if(params.boardId){
//display only members with the boardName
board{
eq("id", params.long('boardId'))
}
}
like("is_Trustee","Y")
order("lastName", "asc")
}
//def memberTrustee = TrusteeMembership.findAll("from TrusteeMembership as tm where tm.committee.id = 'params.committee'")
respond Hospitals.list(params), model:[hospitalsInstanceCount: Hospitals.count(),
trusteeInstanceList : indexSearch,
//memberList: memberTrustee,
sideMenu: Hospitals.list(max:1, order:"desc")]
}
Here are the domains.
class TrusteeMembership implements Serializable{
String position
String memberType
static belongsTo = [trustee:Trustee, committees:Committees]//
static constraints = {
position nullable:true
memberType nullable:true, unique:true
}
static mapping = {
table 'BOT_TRUSTEE_COMMITTEES'
version false
id composite: ['trustee','committees']
trustee column:'TRUSTEE_ID'
committees column: 'COMMITTEE_ID'
position column:'POSITION'
memberType column:'TYPE'
}
package trusteedbtest
class Trustee {
String salutation
String firstName
String middleName
String lastName
static hasMany = [board:Boards, membership:TrusteeMembership]
static constraints = {
salutation nullable: true
firstName nullable: true
middleName nullable: true
lastName nullable: true
}
//map to the existing DB table
static mapping = {
table 'BOT_TRUSTEE'
id column:'TRUSTEE_ID'
salutation column: 'SALUTATION'
firstName column: 'FIRST_NAME'
middleName column: 'MIDDLE_INITIAL'
lastName column: 'LAST_NAME'
}
}

right align dynamic column of datatype number in iggrid

I am using Ignite UI grid.
The columns is dynamically build from the database like this:-
$.post('/Main/GetColumns',function(data){
$("#mygrid").igGrid({
columns: data,
width: "100%",
height: "100%",
})
});
The problem is that i dont know which of the column will be of datatype number since data is comming from database for columns and i have to right align the numeric columns.
The only code i have found is
args.owner.element.find("tr td:nth-child(3)").css("text-align", "right");
to set 3rd column as right align.
Since i dont know the column order, i am only left to check for datatype and right align the column,
Is there any way to align column on basis of datatype or any other method to do this?
The data type if the column is used for it's representation(formatting) and editing behavior, but there's no extra markup generated that you can use to target with styling.
However, you are building column definitions server side, where you know exactly what type each column is while creating its definition, no?
Update: It's been a while since the original answer and for future reference you can use the columnCssClass to apply your class to the actual TD rather than the template. The latter is still a valid option for advanced tinkering.
Easiest way I can think of is through Column templates - this way you can add whatever styling / formatting to the columns. For example, based of whatever logic you need, you return some columns as:
{
key: 'status',
dataType: 'bool',
headerText: 'Status',
template: '<div class="rightAlign"> ${status} </div>'
}
You apply "text-align:right;" though the class and skip adding template for columns that should be with default look. Since this definition is generated on the server (imagine my example uses Node.js :P ) you can have those templates static, or create them differently each time - it's up to you.
JSFiddle: http://jsfiddle.net/damyanpetev/wsZ8c/
Note: Make sure you use a block (div,p) in this case as you need something that will take up the entire grid cell in order to align text inside.
If that solution doesn't fit, you will have to go through columns and apply styling on the client in a similar way you were thinking of.
Here is how I dynamically align the text in the columns in the infragistics igHierarchicalGrid according to their data types:
$("#grid1").on("iggriddatarendered", function (event, args) {
var columns = $("#grid1").igHierarchicalGrid("option", "columns");
//var RDate = $("#grid1").igHierarchicalGrid("getCellValue", 1, 1);
var columnIndex = 0;
var trtd = 2;
for (var idx = 0; idx < columns.length; idx++) {
if (columns[idx].dataType == "number" || columns[idx].dataType == "double")
args.owner.element.find("tr td:nth-child(" + trtd + ")").css("text-align", "right");
if (columns[idx].dataType == "string" || columns[idx].dataType == "date")
args.owner.element.find("tr td:nth-child(" + trtd + ")").css("text-align", "left");
columnIndex++;
trtd = columnIndex + 2;
}
});
As you see I am starting with vartd = 2 and this is because there are 2 elements in the table
(I use hierachcical grid) before the columns in the grid are available. You must debug and investigate if in your case
the columns of the grid are coming after the second DOM element or after the first.
In easy way you can add css into columnCssClass property and applied into grid where you were define column information
Style:
<style>
.right-align {
text-align: right;
}
.left-align {
text-align: left;
}
.center-align {
text-align: center;
}
</style>
and grid code snippet:
{ headerText: 'Option', key: "Option", dataType: "string", width: "10%", hidden: true },
{ headerText: 'ID', key: "Program_Id", dataType: "string", width: "10%", columnCssClass: "right-align" },
{ headerText: 'Desc', key: "Program_Des", dataType: "string", width: "10%", columnCssClass: "left-align" },
{ headerText: 'Status', key: "program_Status", dataType: "Bool", width: "10%", columnCssClass: "center-align" },

How to get values from Extjs template

I am using a Rowexpander inside a ExtjsGrid. RowExpander's template has text area which is used to get values from the user.
Below is my code.How can I read the value
var expander = new Ext.ux.grid.RowExpander({
tpl : new Ext.Template(
'<p><b></b><div class="abc"> <input type="textarea" id = "hans_" name ="hans_" value = "{comment}"</ div></p><p></p><p>{promptMsg}</p>'
),
listeners:
{
expand: function(ex, record, body, rowIndex){
},
collapse: function(ex, record, body, rowIndex){
}
}
});
Solved ...
Below is the solution
Give a dynamic names to textarea of template
'<p><b></b><div> <textarea rows="2" cols="100" id = "{qnNum}" name ="{qnNum}" > {comment} </textarea><b></b></ div></p><p></p>'
Read the values
document.getElementById(record.data.qnNum);

get category products by category id in opencart

hi i want to customize the feature module that as category is set through admin display the relevant product instead of featured module
when ever i select the category it displays category products what will be the code of controller which get category n give products array to that category
below is the view code of category select input
<td><select name="category_id">
<?php foreach($categories as $category) { ?>
<option value="<?php echo $category['category_id'];?>"><?php echo $category['name'];?></option>
<?php } ?>
</select>
$categories = $this->model_catalog_product->getProductCategories(
$this->request->get['product_id']);
foreach($categories as $category_id) {
// do something
}
Add the getProductCategories function into model/catalog/product.php
public function getProductCategories($product_id) {
$product_category_data = array();
$query = $this->db->query("SELECT * FROM " . DB_PREFIX .
"product_to_category WHERE product_id = '" . (int)$product_id . "'");
foreach ($query->rows as $result) {
$product_category_data[] = $result['category_id'];
}
return $product_category_data;
}