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

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)

Related

Show Product on Search Page - Magento 1.7

Currently I have a snippet of code that will forward a user to the product page if they search for a term and only 1 product is associated with that keyword.
<?php if($this->getResultCount() == 1): ?>
<?php $prodId = $this->_productCollection->getAllIds() ?>
<?php $singleProduct = Mage::getModel('catalog/product')->load($prodId) ?>
<?php header('Location: ' . $singleProduct->getProductUrl()) ?>
<?php exit; ?>
<?php elseif($this->getResultCount()): ?>
However, what I want to do now is actually serve up the product and all its details on the results page itself if its the only one with that tag/search term INSTEAD of redirecting to the product page. Im pretty new to php so please bear with me.
Block template is bad place for this. Good place - controller.
Maybe you need rewrite controller for this functionality.
For example/app/code/core/Mage/CatalogSearch/controllers/ResultController.php
In controller your code looks like:
$this->getResponse()->setRedirect($_product->getProductUrl());

Magento Special price validation

Sorry that I'm a total newbie in magento.
I have a multi-vendor magento site where vendors can create product. But when setting product price some users often do some mistakes. Some times special prices are higher than original price. I like to check this mistake. I want a validation script so that when vendors (who have limited admin access) create a new product then they should keep a minimum difference between special price and original price where special price is always lower than original price.
Can any body give some hints?
Thanks
Hope following code will help you
<?php
$product= Mage::getModel('catalog/product')->load(product_id);
$price = $product->getPrice();
$webprice = $product->getwebprice();
$specialprice = $product->getFinalPrice();
if($specialprice==$price)
{?>
<span>$<?php echo number_format($price,2);?></span>
<?php } else if($specialprice<$price) { ?>
<div>
<span>Regular Price:</span>
<span>$ <?php echo number_format($price,2); ?></span>
</div>
<div>
<span>Web Special:</span>
<span>$ <?php echo number_format($specialprice,2); ?> </span>
</div>
<?php } ?>
Even if the user set special prices more than original price,Magento takes cares of it by not displaying that special price .However if you want to do some customization ,the path for price display is :app/design/frontend/default/default/template/catalog/product/price.phtmlIt would be wise if you copy the structure, paste it on your custom theme and continue with your modification.can add your javascript in list.phtml (same product folder).Hope it gives some hint.

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 share cart between websites

I have a Magento store which needs different prices for each site, which restricts me to using different websites for each, as stores or views won't let me set different prices for the same items.
However, I need to be able to allow the customer to switch store, and for their current basket to stay with them. This would include updating the prices to those in the new website.
I've set Share Customer Accounts to Global and Catalog Price Scope to Website.
I also have an initial changer:
<?php $websites=Mage::app()->getWebsites();?>
<?php if(count($websites)>1): ?>
<fieldset class="store-switcher">
<label for="select-store"><?php echo $this->__('Select Store') ?>:</label>
<select id="select-store" onchange="location.href=this.value">
<?php foreach ($websites as $website): ?>
<?php $_selected = ($website->getCode() == Mage::app()->getWebsite()->getCode()) ? ' selected="selected"' : '' ?>
<option value="<?php echo $website->getDefaultStore()->getBaseUrl()?>"<?php echo $_selected ?>><?php echo $this->htmlEscape($website->getName()) ?></option>
<?php endforeach; ?>
</select>
</fieldset>
<?php endif; ?>
Is this achievable? Or is it back to the drawing board?
Info: Magento ver. 1.6.2.0
Also: The websites I wish to share the cart between are on the same domain, and have the same frontend cookie value. (which I assume is the SID).
This is an old fix to share cart contents (1.3 or 1.4) I used and may no longer be valid for 1.6, but give it a shot.
Edit the following template for your theme: template/page/switch/stores.phtml
Add to stores.phtml
$sessionID = Mage::getModel('core/session')->getSessionId();
Paste the new option value I included below over the the existing option value
<option value="<?php if(strpos($_group->getHomeUrl(),"?")===false){ echo $_group->getHomeUrl()."?SID=".$sessionID; }else{ if(strpos($_group->getHomeUrl(),"&SID=")===false){ echo $_group->getHomeUrl()."&SID=".$sessionID; }else{ echo $_group->getHomeUrl();}} ?>" <?php echo $_selected ?>><?php echo $this->htmlEscape($_group->getName()) ?></option>
Then create or modify a template to include static links to the individual stores to switch back and forth (eg: in the header). This fix did not work for the store switcher itself, but worked fine with these links.
You are in store A. Goto Store B.
As best I can tell it is not possible to share carts between websites by design. Though by store within a website works fine.

Magento Backorder Availability Message

In my Magento store I have it set up to allow backorders on some products. When these items are out of stock they still show as 'In Stock' on the product page but the user gets notified when they visit the cart that the item is on backorder.
I would like to change the product page so it also shows there that the item is on backorder in place of the 'In Stock' text.
In the file template/catalog/product/view/type/simple.phtml (and the same for bundled, configurable, grouped and virtual - you must override them all) there is some code that looks like this:
<?php if($_product->isSaleable()): ?>
<p class="availability in-stock"><?php echo $this->__('Availability:') ?>
<span><?php echo $this->__('In stock') ?></span></p>
My guess is you need to change it a bit like this:
<?php if($_product->isSaleable()): ?>
<p class="availability in-stock"><?php echo $this->__('Availability:') ?>
<span><?php echo $this->__($_product->isInStock() ? 'In stock' : 'On Backorder') ?></span></p>
Do a search of all template files for "availability" to see the various places that might need fixing.
I have found the following solution which worked for me at link below:
Show backorder status on magento frontend
To do this, make sure you have enabled backorders from inventory tab.
If you are on product page then first of all retrieve product qty.
<?php $inventory = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product);
if( (int)$inventory->getQty() == 0 && $inventory->getBackorders() )
{
// No Backorders => getBackorders() = 0
// Allow Qty Below 0 => getBackorders() = 1
// Allow Qty Below 0 and Notify Customer => getBackorders() = 2
echo "display your backordedr message";
}
'?>
You can also put this code in
app\design\frontend\base\default\template\catalog\product\view\type\default.phtml
file where availability message of product come from.

Resources