IF (ThisProduct IS IN ANY ConfigurableProduct) redirect(ThatConfigurableProduct) - magento

That's pretty much what I'm trying to do. All of my simple products are part of, at most, 1 configurable product, so there's no possibility for issues there.
This is necessary because I want my simple products (pillow in design X, color Y) to show in search, catalog but I need the user to know that the design exists in different colors once they click (presumably because they like design X but aren't necessarily sold on color Y). Further, my implementation of Color Swatches (extension) is causing my simple products (that are part of configurables) to behave funnily when accessed directly.
Thanks for any help.
Edit:
Here's the code I ended up using. I'm not a very good coder so make sure to improve it before deploying... (~In app/design/frontend/blah/blah/template/catalog/product/view.media.phtml)
<?php
/* THIS BLOCK ADDED BY __ ON 5/5/2011 */
$thisProductId = $_product['entity_id'];
$thisProductParentId = Mage::getResourceSingleton('catalog/product_type_configurable')->getParentIdsByChild($thisProductId);
if (!$thisProductParentId)
{
?>
<div class="more-views">
<h2><?php echo $this->__('More Views') ?></h2>
<ul>
<?php foreach ($this->getGalleryImages() as $_image): ?>
<li>
<img src="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile())->resize(56); ?>" width="56" height="56" alt="<?php echo $this->htmlEscape($_image->getLabel()) ?>" />
</li>
<?php endforeach; ?>
</ul>
</div>
<?php
}
else if ($thisProductParentId)
{
$_product_temp = Mage::getModel('catalog/product')->load($thisProductParentId);
if($_product_temp->getStatus()==1)
{
$_categories = $_product_temp->getCategoryIds();
$_category = Mage::getModel('catalog/category')->load($_categories[0]);
$url = $this->getUrl($_category->getUrlPath()).$_product_temp->getUrlPath();
echo '<h1><a style="color:red;" href="'.$url.'">Click here to view this pillow design in different colors and styles.</a></h1>';
// redirect disabled because it won't preload the new color on the configurable image page anyway. (haven't attempted)
/* echo '<script type="text/javascript">
<!--
window.location = "'.$url.'"
//-->
</script>'; */
}
}
// -- end --
?>
The overwriting of the More Images gallery bit is a project-specific customization, so keep that in mind.

I went and wrote a bunch of code to try to do this, and forgot that this is already a simple use case, and Magento has it written for you:
Mage::getResourceSingleton('catalog/product_type_configurable')
->getParentIdsByChild($childId);
That snippet should give you all parent products for the child. If there is one, redirect to it. Otherwise, render the page as requested.

you have two options here :
add rewrite rules form catalog > url rewrite management
program an extension that makes the necessary check against product database and makes the redirect

Related

Magento shows attribute if value is left blank

I have a drop down magento attribute for warranty_labour. I'm pulling in the attribute using my themes product/view.phtml and then attaching an icon to show what warranty you get.
Ive used this code which successfully works:
<?php $warranty=$_product->getAttributeText('warranty_labour'); echo '<img style="padding-top: 10px;" src="/images/warrantylabour/'.str_replace(' ', '_',$warranty).'.png" alt="'.$warranty.'">'; ?>
The problem I find when a product doesn't have a warranty attribute set (left blank on the backend) I still get the code inserted on to the product source code on the frontend like this:
<img style="pad`ding-top: 10px;" src="/images/warrantylabour/.png" alt="">`
Is there a way I can stop this happening when a value isn't set in the attribute?
Don't forget logic! :-) Just test that $warranty has a value Using typical Magento template conventions:
<?php if($warranty=$_product->getAttributeText('warranty_labour')): ?>
<?php echo sprintf('<img style="padding-top: 10px;" src="/images/warrantylabour/%s.png" alt="%s"/>', str_replace(' ', '_',$warranty),$warranty) ?>
<?php endif; ?>
This kind of syntax might justify encapsulating this logic and string building into a helper method, I think.

Magento - All categories have same image

So there's some code floating around that gets an individual subcategory's image:
foreach ($collection as $cat){
$cur_category = Mage::getModel('catalog/category')->load($cat->getId());
$layer = Mage::getSingleton('catalog/layer');
$layer->setCurrentCategory($cur_category);
$_img = $this->getCurrentCategory()->getImageUrl();
}
Where $cat is the subcategory I'm trying to get the image of. The problem I'm running in to - the images for the subcategories are all the same! The first image from the subcategories is the image that shows up on the page. The links and names are all correct though: (this is also in the for loop)
<div>
<a href="<?php echo $helper->getCategoryUrl($cat);?>">
<img src="<?php echo $_img; ?>" title="<?php $cat->getName() ?>"/>
<cite><?php echo $cat->getName();?></cite>
</a>
</div>
I'm making this modification in catalog/category/view.phtml. There's a bit more to it than shown, but this is all the relevant information.
Edit
All categories have their own unique images, which are properly uploaded. They show in the admin correctly. Also, $cat->getId() is returning the correct id's for the individual subcategories.
#RandyHall, are your sure that $this->getCurrentCategory() will get the category from the same place as $layer->setCurrentCategory($cur_category); will set it to?
Watch the source code here and here.
As you can see category is set to the layer model and get returns category from registry(via call to block).
So I would suggest you to do something like this:
$_img = $layer->getCurrentCategory()->getImageUrl();
Perhaps you are doing wrong when getting image url.
instead of using
$_img = $this->getCurrentCategory()->getImageUrl();
try below code
$_img = $cur_category->getImageUrl();
foreach ($collection as $cat){
$cur_category = Mage::getModel('catalog/category')->load($cat->getId());
$_img = $cur_category->getImageUrl();
}
Looks to me like this is what you are going for?

Magento upsell url with full category path?

I've got a problem with my Magento installation. I'm using the upsell feature to provide some more interesting products. The output works so far but I've got a big problem with the product url.
The url links to my-shop.com/product.html and not to my-shop.com/category-1/category-2/product.html. That's bad because I'm calling a special sidebar navigation for each top category. And so nothing is shown...
I'm using the standard upsell output:
<img src="<?php echo $this->helper('catalog/image')->init($_link, 'small_image')->resize(115) ?>" width="115" height="115" alt="<?php echo $this->htmlEscape($_link->getName()) ?>" title="<?php echo $this->htmlEscape($_link->getName()) ?>" />
<h3 class="product-name"><?php echo $this->htmlEscape($_link->getName()) ?></h3>
<?php echo $this->getReviewsSummaryHtml($_link) ?>
<?php echo $this->getPriceHtml($_link, true, '-upsell') ?>
(I can't post the whole code, the editor will delete most of it)
I've tried it with $_link->getProductPath(), but the output was empty.
I also checked the settings in System->Config->Catalog->Search Engine Optimization. Use Categories Path for Product URLs set to Yes.
Does anybody has an idea how to get the full path url for the products?
Thank you for your help.
You may use the below logic to get the product url. Update the variables accordingly.
<?php
$_prodcats = $_product->getCategoryIds();
$_cat = Mage::getModel('catalog/category')->load($_prodcats[0]);
$produrl = $this->helper('catalog/output')->productAttribute($_product, $_product->getRequestPath(), 'request_path') ;
if($this->getRequest()->getModuleName() == 'catalogsearch') {
$produrl = '/'. basename($_cat->getUrl()) .'/' . basename($_product->getProductUrl()) ;
}
?>
I found a solution on the web which works for 1.9.2.1.
The upsell products become a link with category link.
$d = $_link->getData();
$id = $d['entity_id'];
$_product = Mage::getModel('catalog/product')->load($id);
$_categories = $_product->getCategoryIds();
$_category = Mage::getModel('catalog/category')->load($_categories[0]);
$cat_url = str_replace(".html","",$_category->getUrlPath());
$_url = Mage::getUrl($cat_url).basename($_link->getProductUrl());
And then use this url
Link
I still have the problem that the created sitemap.xml just added
www.website.com/product instead of
www.website.com/category/product
even if "Use Categories Path for Product URLs" is set to YES

Magento - Unable to Refresh Product Stock Status on the Product Page

One of our Vendors has a real time inventory system and we would like to implement it into our site. When a person clicks on the product, it should check the inventory and update as necessary. This works ok at best. The problem is when the product switches to in/out of stock. It updates properly in the backend, but I am unable to get the addtocart button to be added/removed. This is my code for updating the stock:
//$_stockQTY is the realtime inventory result
$stockData = Mage::getModel('cataloginventory/stock_item');
$stockData->loadByProduct($_product->getId());
$stockData->setData('qty', $_stockQTY);
$stockData->setData('is_in_stock',($_stockQTY > 0) ? 1 : 0);
if ($stockData->dataHasChangedFor('qty')) {
$stockData->save();
$_product = Mage::getModel('catalog/product')->load($_product->getId());
}
As you can see, I am force reloading the product when qty is changed. This seems to work for everything but the addtocart button. It shows the previous result (In stock or out of stock before the reload.)
I have 2 questions:
Is there a better way to reload a product other than reassigning it as I am doing above:
$_product = Mage::getModel('catalog/product')->load($_product->getId());
And why is it that everything is updating properly, but the addtocart which uses the same
$_product->isSaleable()
call that our availability, etc uses.
Compare:
<?php if($_product->isSaleable()): ?>
<p class="availability in-stock"><img src="<?php echo $this->getSkinUrl('images/stock.png') ?>"> <span><?php echo $this->__('In stock') ?></span>
...
?>
To
<?php if($_product->isSaleable()): ?>
<?php echo $this->getChildHtml('addtocart') ?>
<?php endif; ?>
<?php echo $this->getChildHtml('alert_urls') ?> //Only Shows up if addtocart does not.
Refreshing the page will update the product properly, but doing a meta refresh or anything of the sorts is out of the question. I appreciate any advice that could be given as I would like to get this resolved and on to the next task.
Unless I'm misunderstanding your question, it appears the thorn in your side is the stock status index.
Try this:
Mage::getSingleton('cataloginventory/stock_status')->updateStatus($_product->getId());
(I haven't tested this, but it looks like it ought to work)

how to use JHTML::_('behavior.modal') in Joomla?

I am creating a component,
controllers
theatercontroller
facilitycontroller
Models
theater
facility
view
theater
facility
What I want is I want to add a new facility from the theater view by clicking a button and opening a modal window. I have tried but didn't work. I studied some components but it is difficult for someone like me to understand it. Please I need a simple example and a explanation to understand it.
It doesn't have to be anywhere near that complicated. Many templates turn the modal behavior on already, but if they don't all you need to do is add this to the head -
<?php JHTML::_('behavior.modal'); ?>
Then add this to any links you want to open in a modal window -
class="modal"
Everything else is optional.
You can use this function to get a modal button
static public function getModalButtonObject($name,$text,$link,$width=750,$height=480)
{
JHTML::_('behavior.modal', "a.{$name}"); // load the modal behavior for the name u given
$buttonMap = new JObject(); // create an Jobject which will contain some data, it is similar like stdClass object
$buttonMap->set('modal', true);
$buttonMap->set('text', $text );
$buttonMap->set('name', 'image');
$buttonMap->set('modalname', $name);
$buttonMap->set('options', "{handler: 'iframe', size: {x: ".$width.", y: ".$height."}}");
$buttonMap->set('link', $link);
return $buttonMap;
}
And HTML can be written as
<a id="<?php echo $buttonMap->modalname; ?>" class="<?php echo $buttonMap->modalname; ?>" title="<?php echo $buttonMap->text; ?>" href="<?php echo $buttonMap->link; ?>" rel="<?php echo $buttonMap->options; ?>"><?php echo $buttonMap->text; ?></a>

Resources