I have a website about HTML5 games, built with some PHP script.
One of its plugins you can create custom fields for each category page.
I created different custom fields (description text) for each category page.
But when I want to show these fields on category pages, unfortunately each category page shows all custom fields of all categories.
Here is a code I added to category.php file:
<?php
$categories = get_all_categories();
foreach ($categories as $category){
$category_info = $category->get_field('category_info');
if(is_null($category_info)){
$category_info = 'no info';
}
echo $category_info;
}
?>
I want to show only 1 custom field (which is particular to each category) in every category page.
Here is get_all_categories() default function of the script:
function get_all_categories(){
$data = Category::getList();
return $data['results'];
}
Thanks
Related
I have an advanced custom field set up to show on a woocommerce subcategory that allows the user to define a colour via the color picker field.
This field will apply that colour to a number of elements related to that sub category (Styling the sub category thumbnail, the product page itself etc).
Im currently using as per the the ACF documentation this code to pull the field in and display it on the subcategory page:
$term = get_queried_object();
$color = get_field('colour', $term); // Get ACF Field
This works fine until it comes to the parent category for the sub pages. I am unable to call the field in for the sub categories of the parent. I understand I need to use get_terms(). I am unable ot get that working though.
This is some code I found which I have added to the loop on content-product_cat.php. However it just breaks the woocommerce loop. What would I need to do to this code to get the parent category page to show all the child subcategories each with its related color field?
// current term
$current_term = get_queried_object();
// child terms
// this returns an array of terms
$args = array(
'taxonomy' => 'YOUR TAXONOMY HERE',
'parent' => $current_term->term_id,
// you may need other arguments depending on your needs
);
$child_terms = get_terms($args);
// you need to maybe loop through the child terms gotte
// to pick which one you want to use
// I'm assuming that you only want to use the first one
$child_term = false; // set it to false to begin with
// we'll use this later
if ($child_terms) {
$child_term = $child_terms[0];
}
// make a decision
if ($child_term) {
// get field value(s) from child term
$color = get_field('color', $child_term);
} else {
// get field value(s) from current term
$color = get_field('color', $current_term);
}
// do something with the values
echo $color;
I found the solution here:
https://wordpress.stackexchange.com/a/341632
add_action('woocommerce_after_subcategory_title', 'wpse_add_custom_text_under_category_title', 10);
function wpse_add_custom_text_under_category_title($category) {
$term_id = 'product_cat_'.$category->term_id;
the_field('krotki_opis_kategorii', $term_id);
}
From Alex Uleberg:
get_queried_object_id on shop archive page it will return the id of the page and not the category. When you use the hook, the $category object will be passed in through the hook.
I would like to be able to use the same form on several pages, and know which page a submitted form came from. For a long list of reasons, I need to do that based on category.
I found code to add categories to pages, works great.
But I can't figure out how to get Gravity Forms to dynamically populate a field with the category.
I've selected "Allow field to be populated dynamically" on the form, I've set the parameter name to "pagecategory"
Here's what I've got - it does nothing:
//Get Page Category - For Demo Form
add_filter("gform_field_value_pagecategory", "populate_pagecategory");
function populate_pagecategory($value){
global $post;
$value = get_the_category( $post->ID,'metakeyname',true);
return $value;
}
get_the_category() actually returns an array of all category that is assigned to that post. The code below works for me to fetch the name of first category only.
//Get Page Category - For Demo Form
add_filter("gform_field_value_pagecategory", "populate_pagecategory");
function populate_pagecategory($value){
global $post;
$categories = get_the_category( $post->ID,'metakeyname',true);
$value = $categories[0]->cat_name;
return $value;
}
I'm a bit surprised this type of need/question isn't more prevalent. I had to do this same thing, but for a custom taxonomy. In case anyone lands here in need of how to do Mash's answer, but with a custom taxonomy, the following is working for me. My custom taxonomy is "services," which you can see replaces 'metakeyname' in Mash's answer.
//Get Page Custom Taxonomy - For Gravity Forms
add_filter("gform_field_value_pagecategory", "populate_pagecategory");
function populate_pagecategory($value){
global $post;
$terms = get_the_terms( $post->ID,'services',true);
$value = $terms[0]->name;
return $value;
}
I want to display all products in onepage while clicking a single button in main menu and also need to display category in main menu...
thank in advance...!
Display Alll Product in one Page You have to create separate page for displaying all products.
Create Model file catalog\model\catalog\allproduct.php and paste
code this http://pastebin.com/suF5TP3z
Create Controller file catalog\controller\productallproduct.php and
paste below code http://pastebin.com/jZq3hZyc
Create View file
catalog\view\theme\default\template\productallproduct.tpl and paste
below code http://pastebin.com/1HNh3x73
4 . Create Language file catalog\language\en-gb\product\allproduct.php
and paste below code http://pastebin.com/EcyJH7F9
Enable module from back-end You can see link in menu
Display All Category in Menu
Open and edit your category:
Select tab Data:
Find and Check:Top: Display in the top menu bar.
Note : Only works for the top parent categories.You will need to check all the top level categories.It's Opencart bugs.
I suggest don't make any change in the default theme codes and structure. If you already using a custom theme you can easily create a menu and define to all categories included all products under the one menu or button.
Lets get over with in that way :
For example you have 5 categories. Every category includes over ten products or much.
Create a brand (Manifacturer). This brand will be contain just 1 category. Give it a name like "All Products"
Create a category and name it as "All Products".
Create a menu which will display all products and define the menu display as category. Select the "All Products" category which is you just created.
Go to product pages and click edit for each one. Click the "link" tab and the category textbox, choose "All Products" category. Make this simple edit for every product. I tried for you and it takes 4 / 5 seconds.
Go to "Theme Setting" in your theme. Find "Display Products" or "Products in Category" setting. There are always pre-defined display limit in every theme and it goes next with pagination. Make a simple calculation like that ; how many product in your store, 50 ? 100 ?
Setup the display limit bigger than your total product count.
That's it !
Maybe you would like to play menu css a bit. Like i said before, if you already using a custom theme, you dont need to fork your core codes especially the default theme.
Let me know if any help.
The way i did its something like this
Controller : ControllerProductCategory
inside the index function replace
if (isset($this->request->get['limit'])) {
$limit = $this->request->get['limit'];
} else {
$limit = $this->config->get('config_product_limit');
}
with
if (isset($this->request->get['viewall'])) {
$limit = "";
} else if (isset($this->request->get['limit'])) {
$limit = $this->config->get('config_product_limit');
} else {
$limit = $this->config->get('config_product_limit');
}
Also make this change to display all the products without category filter
replace this code
if ($category_info) {
$this->document->setTitle($category_info['meta_title']);
$this->document->setDescription($category_info['meta_description']);
$this->document->setKeywords($category_info['meta_keyword']);
With this code
if (($category_info) || ($category_id == 0)) {
if ($category_id == 0) {
$this->document->setTitle('all products');
$this->document->setDescription('all products');
$this->document->setKeywords('all products');
$this->data['heading_title'] = 'all products';
$category_info['description'] = '';
$category_info['image'] = '';
} else {
$this->document->setTitle($category_info['name']);
$this->document->setDescription($category_info['meta_description']);
$this->document->setKeywords($category_info['meta_keyword']);
$this->data['heading_title'] = $category_info['name'];
}
and at the last create a new category , you can name it to anything you want. and give the link something like this
https://www.yourwebsite.com/yourCategoryname?viewall=viewall?viewall=viewall
You can place this link anywhere you want and it will load all the products without any pagination.
let me know if you still don't understand anything anyehere.
I created a new module to create a section in admin panel. This module section has a sub-section "Associated products" where I can add one or more products to each item in that module.
I am able to get the field values using functions like
$combo->getName()
$combo->getComments()
But I am not able to get the associated products to that item using
$combo->getAssociatedProducts()
What I tried is as follows:
<?php $comboCollection = Mage::getResourceSingleton('combo/combo_collection'); ?>
<?php
foreach ($comboCollection as $combo) {
zend_debug::dump($combo->getAssociatedProducts($combo)); //giving error
}
?>
PS: here $combo is not a product, it is just an item in the created module.
Explanation:
Considering that you have model/collection products for table combo_combo_product. you should get collection for products and then filter it for current combo id of combo collection in this way
foreach ($comboCollection as $combo) {
$associatedCollection = Mage::getResourceSingleton('combo/products_collection');
//$associatedCollection = Mage::getModel('combo/products')->getCollection();
$associatedCollection->addFieldToFilter('combo_id',array('eq' => $combo->getId()));
foreach{$associatedCollection as $item){
print_r($item->getData());
}
}
I used following codes but didn't work for this case:
$_category_detail=Mage::registry('current_category');
echo $_category_detail->getName();
got Fatal error: Call to a member function getName() on a non-object in /app/design/frontend/base/default/template/catalog/product/view.phtml
we make some filters and use below mention code in head.phtml:
$is_product = Mage::registry('product');
if($is_product){
if(is_object(Mage::registry('current_category'))){
$category_name = Mage::registry('current_category')->getName();
}
else{ $category_name = ""; }
}
But this only works if you go from a category to a product. If you visit the product page directly nothing is being displayed
It's because products can be attached to multiple categories. In your situation, when you visit a product page referred from a category page, your session has category information. But if you visit directly product page, Magento can not know which category you came from, so it can not show you a specific category, because your product can have multiple categories.
But in your situation, if your products are attached only one category, you can use this code, it shows first category name of product;
$categoryIds = $_product->getCategoryIds();
if(count($categoryIds) ){
$firstCategoryId = $categoryIds[0];
$_category = Mage::getModel('catalog/category')->load($firstCategoryId);
echo $_category->getName();
}
<?php
$_category_detail=Mage::registry('current_category');
echo $_category_detail->getName(); //gives current category name
echo $_category_detail->getId(); //gives current category id
?>