Magento Category id array on attribute - php

I have a site running on Magento and i have build some hand made code onto the view.phtml and list.phtml
<?php $category = Mage::getModel('catalog/layer')->getCurrentCategory();?>
<?php if($category->getId()==80): ?>
Command what to do
<?php else: ?>
Else command this
<?php endif; ?>
What i would like to make is to check more categories ( like eg. 80,81 ) before echo it.
How do i have to change the code?

Use in_array()
<?php if(in_array($category->getId(), array(80, 81, ...))): ?>
If you want different code for the various categories:
<?php if($category->getId()==80): ?>
...
<?php elseif($category->getId()==81): ?>
...

Related

After removing foreach, no data is displayed

Enrol table:
From this table I try get value from active_shop
Here I have code:
<?php
$get_config_details = $this->db->get('enrol',$per_page, $this->uri->segment(1));
?>
<?php if ($get_config_details->num_rows() > 0):
foreach($get_config_details->result_array() as $get_config_detail):
$course_details = $this->crud_model->get_course_by_id($get_config_detail['course_id'])->row_array();?>
<?php echo $get_config_detail['active_shop']; ?>
<?php endforeach; ?>
<?php endif; ?>
Now I get the result: 1
But I copied this function from another function. I don't need the foreach function and if and so much code here. I need some simple code to squeeze this table and echo the result. Can anyone help me to correct this code?

Magento category static block not getting displayed

I am unable to make magento display static CMS block under category > display settings.
Contents of app/design/frontend/base/default/template/catalog/category/view.phtml seem to be correct as per similar question on SE. Here you go:
<?php if($this->isContentMode()): ?>
<?php echo $this->getCmsBlockHtml() ?>
<?php elseif($this->isMixedMode()): ?>
<?php echo $this->getCmsBlockHtml() ?>
<?php echo $this->getProductListHtml() ?>
<?php else: ?>
<?php echo $this->getProductListHtml() ?>
<?php endif; ?>
I have configured 3 categories, lets say
category_a (only products)
category_b (both products and cms)
category_c (only cms block).
Things I tried which don't work:
Disabled custom theme and used default theme of magento.
Replaced view.phtml from another magento installation (where its working fine).
Replace the if-else block with
<?php if($this->isContentMode()): ?>
MODE: CMS
<?php echo $this->getCmsBlockHtml() ?>
<?php elseif($this->isMixedMode()): ?>
MODE: MIXED
<?php echo $this->getCmsBlockHtml() ?>
<?php echo $this->getProductListHtml() ?>
<?php else: ?>
MODE: PRODUCTS
<?php echo $this->getProductListHtml() ?>
<?php endif; ?>
This displayed MODE: PRODUCTS for all 3 cases (product / cms / mixed)
Removing the if-else block and using only lines to force magento to display both cms and product blocks. Assuming the control was not flowing to correct block.
<?php echo $this->getCmsBlockHtml() ?>
<?php echo $this->getProductListHtml() ?>
Only product block got displayed for all 3 categories mentioned above (product / cms / mixed)
I can see that there are 2 problems here, or atleast I think so.
category display mode is always coming as "products only"
getCmsBlockHtml() does not return anything
so I tried following code snippet based on Mage_Catalog_Block_Category_View
Product:
<?php echo $this->getCurrentCategory()->getDisplayMode()==Mage_Catalog_Model_Category::DM_PRODUCT; ?>
MIXED:
<?php echo $this->getCurrentCategory()->getDisplayMode()==Mage_Catalog_Model_Category::DM_MIXED; ?>
CMS:
<?php echo $this->getCurrentCategory()->getDisplayMode()==Mage_Catalog_Model_Category::DM_PAGE; ?>
and got response as >>Product: MIXED:1 CMS: for all three category display modes (product / cms / mixed).
Can someone help me out please.
I have disabled caches and have tried reindexing also.
I found a fix for my issue here
Magento 1.9.2.0
App/Code/Core/Mage/Catalog/Block/Category/View.php
inside function getCmsBlockHtml
On Line 109 it read
return
but should read
return $this->getData('cms_block_html');
Regards paul

Magento - show custom attribute in cart

I was looking for a way to display the "estimated delivery" of a product by using the attribute 'delivery' I've made.
So far I've managed to put together this:
<?php $delivery= Mage::getSingleton('catalog/product')->load($this->getProduct()->getId())->getAttributeText('Delivery'); ?>
<?php if(isset($delivery)){
echo $delivery;
}
?>
I have added this piece to:
template/checkout/cart/item/default.phtml - between line 38/39 (Magento Version 1.6.2)
Here is the default.phtml from line 35-49 with the code added to the h2 tag:
<h2 class="product-name">
<?php $_item = $this->getItem()?>
<?// Delivery - Script ?>
<?php $_product= Mage::getSingleton('catalog/product')->load($_item->getProductId()) ?>
<?php $delivery= Mage::getSingleton('catalog/product')->load($this->getProduct()->getId())->getAttributeText('Delivery'); ?>
<?php if(isset($delivery)){
echo $delivery;
}
?>
<?php if ($this->hasProductUrl()):?>
<?php echo $this->htmlEscape($this->getProductName()) ?>
<?php else: ?>
<?php echo $this->htmlEscape($this->getProductName()) ?>
<?php endif; ?>
</h2>
The problem is, the first product added to cart is being skipped, it's always showing the attribute as not set, but the second, third and rest of the products added to cart, works great, showing their est. delivery date just fine.
From here, I'm unsure how to proceed?
Use of singleton will result in same object being called repeatedly so previous object data will get overwritten.
Once you changed from singleton to getModel you have an instance for each product so no over writing on same instance.
After some Google'ing I found this, which seems to work - But I still cannot see why, and what the big difference is, if anyone would like to clarify it, I would be very happy.
This is the working solution, it's so simple, yet I don't understand:
<?php
$custom = Mage::getModel('catalog/product')->load($_item->getProductId());
echo $custom->getAttributeText('delivery');
?>
This does not skip the first product?
app/design/frontend/{YOURTEMPLATE}/default/template/checkout/cart/item/default.phtml
Past this code:
<?php $_item = $this->getItem(); ?>
<?php $_product = $_item->getProduct()->load(); ?>
<?php $_product->get{YOUR ATTRIBUT HERE}(); ?>

If - else php query

I need a code that pulls out the listings count for a product in order to have them in the navigation. I am trying to get the product and listing number in the navigation (if there is any), but I don't want to display something if the listings count is equals to 0.
Here follows my code:
<?php if($listingsCount = getListingsCount(0,0,2,2,1,86) > 0): ?>
Bakery
<?php echo $listingsCount['totalRecords'] ?>.
<?php else: ?>
It should be
if ( ($listingsCount = getListingsCount(0,0,2,2,1,86)) > 0)
In this case you the assignment is done first and is then compared to 0. So $listingCount contains the real count.
The way you did it, the comparison is done first and the return value (true/false) is assigned to $listingCount.
Nevertheless. If you never reach the else-part, maybe something in your method getListingsCount() is broken.
Try this:
<?php if (($listingsCount = getListingsCount(0,0,2,2,1,86)) > 0) { ?>
Bakery
<?php echo $listingsCount['totalRecords']; ?>
<?php } else { ?>
Other text
<?php }?>
Thanks guys for your answers. managed to get it working with this...
<?php
$listingsCount = getListingsCount(0,0,2,2,1,86);
if($listingsCount['totalRecords'] > 0):
?>
<dd>
Bakery
[<?php echo $listingsCount['totalRecords'] ?>]
</dd>
<?php endif; ?>

How do you stop echo of multiple values with 'foreach' in PHP?

How do you when using custom fields in Wordpress echo just the first value using foreach?
Currently the code is:
<?php for(get_field('venue_event') as $post_object): ?>
<?php echo get_the_title($post_object) ?>
<?php endforeach; ?>
This takes the field from the wordpress page (the field is a link to another page), creates a link to that page using get_permalink but when I want to echo the page title it does it, but then it also echos all other values that are not needed.
If you just want the execute the first iteration of the loop, try this:
<?php foreach(get_field('venue_event') as $post_object): ?>
<?php echo get_the_title($post_object) ?>
<?php break; ?>
<?php endforeach; ?>
Wouldn't it then be easier just to use the first element of the returned array? Maybe Wordpress offers other filters that return the the page's title only.
you can just add
$counter = 0;
<?php for(get_field('venue_event') as $post_object): ?>
$counter++;
if($counter == 1)
{
<?php echo get_the_title($post_object) ?>
}
<?php endforeach; ?>

Categories