I have created a Category's attribute that I want to use on the frontend. I tried to access it the same way as we do for products but it doesn't seem to be working. How can I show the custom attribute on the frontend? Any guesses?
Thanks
Try this:
$cat_attr = $this->getCurrentCategory()->getAttributes();
if(array_key_exists('short_description', $cat_attr)):
$_shortDescription=$cat_attr['short_description']->getFrontend()->getValue($_category);
endif;
First thing I would do is do the following code on your category object
print_r($category->debug());
This will show you if that attribute is being loaded. If you don't see your attribute, you can go back to the controller where the object is being loaded and add it to your select by adding one of these lines:
->addAttributeToSelect('your_attribute')
That should load your attribute for you.
I had a custom attribute and got it shown in frontend this way;
$category_id = '10';
$attribute_code = 'category_categorycolor';
$category = Mage::getModel('catalog/category')->load($category_id);
echo $category->getResource()->getAttribute($attribute_code)->getFrontend()->getValue($category);
Quite Simple
<?php foreach ($this->getStoreCategories() as $cat): ?>
<?php $_category=Mage::getModel("catalog/category")->load($cat->getId()); ?>
now you should use getImage method to retrive your img attribute
<img src="<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . 'catalog/category/' . $_category->getImage() ?>" />
<?php endforeach ?>
Related
Got a Custom Post Type called Post Modules or the slug of post-modules. In that I have created using Advanced Custom Fields a number of reusable modules to allow me to insert content in another CPT. Each module is basically a post.
What I want to do is grab the slug of the post within post-modules and display it as a class within my section. So it would be something like:
<section class="post-module-name"></section>
The title of the post would be acceptable as well.
So I came up with the following:
<section class="toolkit-module scroll-section <?php echo $post->post_title; ?> <?php echo $module_type; ?>" data-index="<?php echo $i; ?>" data-module-id="<?php echo $module_id; ?>" data-module-type="<?php echo $module_type; ?>">Content Goes Here</section>
But that pulls the title of the post that displays all these modules and not the module slugs / names themselves.
So if I have a post called "Videos" and I have two modules called let's say music-videos and sports-video, I want to incorporate music-videos in my first section's class and sports-videos in my second.
Is there some way I can pull that post slug coming from the post type and name of the module I'm actually pulling the data out of?
Check out wp_query. You’ll need to use that to access custom post types.
https://developer.wordpress.org/reference/classes/wp_query/
Yes if you already have a WP_Post object you can use it as follows.
echo $post->post_type; // Outputs the actual type of a WP_Post object
echo $post->post_name; // Outputs the slug of the post;
You can take a look at all the properties here, in your specifc case you need something like:
<section class="toolkit-module scroll-section <?php echo $post->post_name; ?> <?php echo $post->post_type; ?>" data-index="<?php echo $i; ?>" data-module-id="<?php echo $post->ID; ?>" data-module-type="<?php echo $post->post_type; ?>">Content Goes Here</section>
It looks like if I use php echo $module_id; I can display the ID number of the module, which at the very least will show something unique. Still I can't locate any documentation anywhere about how to access ACF modules beyond using module_type and module_id.
Found the solution. This did it.
$module_slug = $module->post_name;
then to display it:
<?php echo $module_slug; ?>
I am trying to display some attributes on the product view page on a magento 2 website. However i am not able to get the values to echo onto the page. I have tried using
$block->getData('price')
and
$block->getAttributeText('name')
I am trying to call upon the price value and also a custom text attribute and display/use via phtml file.
Appreciate the help. Thanks
Try this:
<?php $_product = $block->getProduct();
echo $_product->getPrice();
echo $_product->getAttributeText('color');
?>
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($singleproductdata['entity_id']);
$attributes = $product->getAttributes();
foreach ($attributes as $attribute) {
echo $attribute->getAttributeCode();
echo $attribute->getFrontend()->getValue($product);
}
A quick option it is to choose a template with in Magento_Catalog/templates/product/view/form.phtml and add the following code. Otherwise you can build your own block and get it from the the catalog_product_view.xml
<?php
$attribute = $_product->getResource()->getAttribute('your-att-code');
if ($attribute)
{
$attr_value = $attribute ->getFrontend()->getValue($_product);
echo $attr_value;
}
?>
I have displayed attribute filters in my catalog page. My client Requirement is, In some categories, attribute filter has no products that time i need to hide the empty attribute label. How can i achieve this Please help me.
"I need Product count for particular attribute that is shape,color and watts like that how can i get all products count by attribute, if i get count i will control the attribute label using that."
My code in catalog/layer/view.phtml:
<?php if($this->canShowBlock()): ?>
<?php // echo $this->getStateHtml() ?>
<?php if($this->canShowOptions()): ?>
<?php $_filters = $this->getFilters() ?>
<?php if($_filter->getItemsCount()):
<?php echo $this->__($_filter->getName()) ?>
<?php echo $_filter->getCount();?>
<?php echo $_filter->getHtml(); ?>
<?php endif;?>
<?php endif;?>
<?php endforeach; ?>
//Get product collection from "catalog/product" model
$collection = Mage::getModel('catalog/product')->getCollection();
//Fetch Most popular Featured products
$collection->addAttributeToSelect('most_popular');
$collection->addAttributeToSelect('featured_prod');
//You can check your query by:
$collection->getSelect();
Hope this may help you.... :)
I'm looking to get a product on a home page slider in magento to link to the category it is in... so far i have:
<?php
$allIds = $product->getCategoryIds();
foreach($allIds as $categoryId) {
$category = Mage::getModel('catalog/category')->load($categoryId);
?>
<?php echo$category->getName() ?><br/>
<?php
}
?>
(this runs within a foreach item) This provides me with the categories (which is great), however the:
<?php echo $category->getCategoryUrl() ?>
Does not seem to link to the correct place (it actually doesn't give me anything). Can anyone help on the matter?
if you want to show only one category link, you don't need to load categories in the loop:
$category = $product->getCategory();
$url = $category->getUrl();
Update: I just realized that the 1st line may not work on the homepage. But you still do not need the loop:
$category = $product->getCategoryCollection()->getFirstItem();
try this
<?php echo Mage::helper('catalog/category')->getCategoryUrl($category);?>
You will find everything related to displaying categories and subcategories here Display Categories and SubCategories in Magento. Hope this will be helpfull..
I want to create a page in Magento that shows a visual representation of the categories.. example
CATEGORY
product 1
product 2
ANOTHER CATEGORY
product 3
My problem is, their database is organised very differently to what I've seen in the past. They have tables dedicated to data types like varchar, int, etc. I assume this is for performance or similar.
I haven't found a way to use MySQL to query the database and get a list of categories. I'd then like to match these categories to products, to get a listing of products for each category. Unfortunately Magento seems to make this very difficult.
Also I have not found a method that will work from within a page block.. I have created showcase.phtml and put it in the XML layout and it displays and runs its PHP code. I was hoping for something easy like looping through $this->getAllCategories() and then a nested loop inside with something like $category->getChildProducts().
Can anyone help me?
From code found in an SEO related class (Mage_Catalog_Block_Seo_Sitemap_Category)
$helper = Mage::helper('catalog/category');
$collection = $helper->getStoreCategories('name', true, false);
$array = $helper->getStoreCategories('name', false, false);
Try to forget that it's a database that's powering your store, and instead concentrate on using the objects that the Magento system provides.
For example, I had no no idea how to get a list of categories. However, I grepped through the Mage codebase with
grep -i -r -E 'class.+?category'
Which returned a list of around 30 classes. Scrolling through those, it was relatively easy to guess which objects might have methods or need to make method calls that would grab the categories.
Hey something like this might help you, I've adapted it slightly to answer your question more specifically.
$h3h3=Mage::getModel('catalog/category')->load(5); // YOU NEED TO CHANGE 5 TO THE ID OF YOUR CATEGORY
$h3h3=$h3h3->getProductCollection();
foreach($h3h3->getAllIds() as $lol)
{
$_product=Mage::getModel('catalog/product')->load($lol);
print $_product->getName()."<br/>";
}
I adapted this from Paul Whipp's website:
SELECT e.entity_id AS 'entity_id', vn.value AS 'name'
FROM catalog_category_entity e
LEFT JOIN catalog_category_entity_varchar vn
ON e.entity_id = vn.entity_id AND vn.attribute_id = 33
ORDER BY entity_id;
This will provide you with the catalog category IDs.
Here's a quick example
$categories = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('name')
->addAttributeToSelect('url_key')
->addAttributeToSelect('my_attribute')
->setLoadProductCount(true)
->addAttributeToFilter('is_active',array('eq'=>true))
->load();
Thanks a lot. Really helps. To get the game, make a loop and then getName()
foreach ($collection as $cat):
echo $cat->getName();
endforeach;
I used this in /app/design/frontend/default/default/template/catalog/product/feature.xml
<?php
/**
* Home page Featured Product list template
*
* #see Mage_Catalog_Block_Product_List
*/
?>
<?php
if (!is_null($this->_productCollection)) {
$_origCollection = $this->_productCollection;
$this->setCollection(null);
}
$this->setCategoryId(16);
$_productCollection=$this->getLoadedProductCollection() ?>
<?php if($_productCollection->count()): ?>
<div class="featured-products">
<h2><?php echo $this->__('Featured Products') ?></h2>
<?php foreach ($_productCollection as $_product): ?>
<div>
<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>">
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(150, 50); ?>" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" />
<h3 class="product-name"><?php echo $this->htmlEscape($_product->getName())?></h3>
<?php echo nl2br($this->htmlEscape($_product->getShortDescription())) ?>
</a>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
category Listing block:
<?php
$categories = Mage::getModel('catalog/category')->load(2)->getChildren();
$catIds = explode(',',$cats);
?>
<ul>
<?php foreach($catIds as $catId): ?>
<li>
<?php
$category = Mage::getModel('catalog/category')->load($catId);
echo $category->getName();
$subCats = Mage::getModel('catalog/category')->load($category->getId())->getChildren();
$subCatIds = explode(',',$subCats);
?>
<?php if(count($subCatIds) > 1):?>
<ul>
<?php foreach($subCatIds as $subCat) :?>
<li>
<?php
$subCategory = Mage::getModel('catalog/category')->load($subCat);
echo $subCategory->getName();
?>
</li>
<?php endforeach;?>
</ul>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
I made this little video on how I create custom category listing blocks with Magento.
I am sure there are better ways of achieving this or even something I could have done better, but it's just my method. I only created this it in hopes that it helps explain somethings to some people out there.
Magento Custom Category Listing Tutorial