Only two products are displaying as recommended product in magento product admin grid whereas we have assign 10 product with a product as recommended product.
It is the default issue of magento, can be resolved by changing the following file with mentioned changes.
Magento\Catalog\Model\ProductLink\CollectionProvider on method getCollection
foreach ($output as $item) {
$itemPosition = (int)$item['position'];
while(true) {
if (!isset($sorterItems[$itemPosition])) {
break;
}
$itemPosition += 1;
}
$sorterItems[$itemPosition] = $item;
}
Related
I have a custom observer in Magento 1.8.1.0 that is called on Product view page when current product having any upsell products. I have verified (using Mage::log()) that the observer is working, however when I try the following:
public function updateUpsells(Varien_Event_Observer $oObserver)
{
$iCurrentCategory = Mage::registry('current_category')->getId();
$oUpsellCollection = $oObserver->getCollection();
foreach ($oUpsellCollection->getItems() as $key => $oUpsellProduct) {
$aCategoriesIds = $oUpsellProduct->getCategoryIds();
if (!in_array($iCurrentCategory, $aCategoriesIds)) {
$oUpsellCollection->removeItemByKey($key);
}
}
}
On echo $oUpsellCollection; i got nothing returned ?
Is anyone know how to get upsell products collection ? Is this a proper way to do it ?
$upsellCollection = $_product->getUpSellProductCollection();
I'm trying to make something different with my Magento instance;
I want to increase product's stock (in a certain condition) when I'm selling a product. I've added a custom checkbox in the Magento Admin panel and when it's checked I expect to increase product's quantity.
I'm currently investigating the following method:
Mage_Sales_Model_Service_Quote:submitOrder()
Is it a good place to start looking at? Any tip on this problem?
I think I've achieved what I wanted.
I've added the following lines at Mage_Sales_Model_Service_Quote:submitOrder()
...
// $mySpecialCondition = form.checkbox
foreach ($quote->getAllItems() as $item) {
if($mySpecialCondition){
$item->setQty(0 - $item->getQty(), true);
}
...
and also changed the method Mage_Sales_Model_Quote_Item:_prepareQty to accept negative values:
protected function _prepareQty($qty, $ignoreValue = false)
{
$qty = Mage::app()->getLocale()->getNumber($qty);
if(!$ignoreValue){
$qty = ($qty > 0) ? $qty : 1;
}
return $qty;
}
Thanks anyway :)
I want to show popup when a product is first added to cart in Magento and don't want to show a popup if the product was added again or updated.In short, I want to know product which is going to be added in the cart is First occurence or not?
The answer largely depends on how you want to deal with parent/child type products (if you need to).
If you are only dealing only with simple products or you have parent/child type products and you need to test for child id's then:
$productId = 1;
$quote = Mage::getSingleton('checkout/session')->getQuote();
if (! $quote->hasProductId($productId)) {
// Product is not in the shopping cart so
// go head and show the popup.
}
Alternatively, if you are dealing with parent/child type products and you only want to test for the parent id then:
$productId = 1;
$quote = Mage::getSingleton('checkout/session')->getQuote();
$foundInCart = false;
foreach($quote->getAllVisibleItems() as $item) {
if ($item->getData('product_id') == $productId) {
$foundInCart = true;
break;
}
}
EDIT
The question was asked in a comment as to why setting a registry value in controller_action_predispatch_checkout_cart_add is not available to retrieve in cart.phtml.
Essentially registry value are only available through the life of a single request - you are posting to checkout/cart/add and then being redirected to checkout/cart/index - so your registry values are lost.
If you would like to persist a value across these then you can use the session instead:
In your observer:
Mage::getSingleton('core/session')->setData('your_var', 'your_value');
To retrieve the value
$yourVar = Mage::getSingleton('core/session')->getData('your_var', true);
The true flag being passed to getData will remove the value from the session for you.
In order check if the product is already in cart or not, you can simply use the following code:
$productId = $_product->getId(); //or however you want to get product id
$quote = Mage::getSingleton('checkout/session')->getQuote();
$items = $quote->getAllVisibleItems();
$isProductInCart = false;
foreach($items as $_item) {
if($_item->getProductId() == $productId){
$isProductInCart = true;
break;
}
}
var_dump($isProductInCart);
Hope this helps!
I am trying to write a custom module which is capable of duplicating a product into multiple products with only varying SKU. I have tried using function duplicate() under /app/code/core/Mage/Catalog/Model/Product.php in my custom module. But its not working.
I am using the below code in my custom Obesrever.php file to duplicate, but duplication is not occuring
$product = $observer->getEvent()->getProduct();
$newProduct = $product->duplicate();
can anyone suggest me any links to do this or any code format would be helpful.
Thanks
It would be great if you can post the complete function that you are trying to debug or create duplicate products and config.xml (where you are trying to call the event).
Below code works for me in CE 1.9.2.2 without any problems. This function does the following tasks:
Creates duplicate of the original product
Sets the stock to "In Stock" and Qty to "100" (hard coded for now)
Automates reindexing
public function indexAction() //change the function name
{
$productId = $observer->getEvent()->getProduct()->getId();
$productObject = Mage::getModel('catalog/product');
$_product = $productObject->load($productId);
$newProduct = $_product->duplicate();
//new product status is disabled - to view in the frontend you will need to set the status as enabled
$newProduct->setStatus(1);
$newProduct->setName('Duplicate-' . $_product->getName());
$newProduct->setSku('value-' . $productId);
$newProduct->setWebsiteIds($_product->getWebsiteIds());
//set the product stock and qty to display product in the frontend
//while creating duplicate product, it will update the new product to have qty 0 and out of stock
$stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($newProduct->getId());
if ($stockItem->getId() > 0 && $stockItem->getManageStock())
{
$qty = 100;
$stockItem->setQty($qty);
$stockItem->setIsInStock((int)($qty > 0));
$stockItem->save();
}
$newProduct->getResource()->save($newProduct);
//automate reindexing - to display the product in the frontend
$indexers = Mage::getSingleton('index/indexer')->getProcessesCollection();
foreach ($indexers as $indexer)
{
$indexer->reindexEverything();
}
}
Hope this helps.
Happy Coding...
I am having trouble with a module that is supposed to update prices on a per-website basis.
At the top of my module I have :
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
This should mean that when I call the module (the module has a frontend) I am able to update prices etc as 'admin'. However, this just isn't happening. Currently I have gone on a few desperate efforts to get it working - seeing if number formats trip it up or indexes not reindexed, cache problems and anything else.
The following snippet actually produces the right numbers but it just is not saving to the database. Any ideas?
$product = Mage::getModel('catalog/product')->
setStoreId($storeId)->setWebsiteId($websites[$store])->
loadByAttribute('sku',$price['SKU']);
$oldprice=(float)str_replace(",","",$product->getPrice());
if($newprice!=$oldprice) {
Mage::log($product->getPrice());
$product->setPrice(number_format($newprice,4,".",""));
Mage::log($product->getPrice());
$product->getResource()->saveAttribute($product, 'price');
$product->save();
Mage::log($product->getPrice());
unset($product);
}
Let's assume that you are updating the prices of products using a method in your module's class, with the following sample code:-
public function updateProductPrices ($sku, $newPrice) {
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$websites = Mage::app()->getWebsites();
$product = Mage::getModel('catalog/product')
$productId = $product->getIdBySku($sku);
foreach ($websites as $_eachWebsite) {
$_websiteId = $_eachWebsite->getWebsiteId();
$websiteObj = new Mage_Core_Model_Website();
$websiteObj->load($_websiteId);
$storeIds = $websiteObj->getStoreIds();
if (count($storeIds)) {
foreach ($storeIds as $_eachStoreId) {
$product->setStoreId($_eachStoreId)
->load($productId);
$oldPrice = $product->getPrice();
if ($oldPrice != $newPrice) {
$product->setPrice($newPrice);
$product->save();
}
}
}
unset($storeIds, $websiteObj, $_websiteId);
}
unset($product);
}
Hope it helps.
Try:
$product->setPrice($newprice)->save();
I am having similar issue and I think there is a bug in the answer from Knowledge Craving. The code will actually not update on a per website basis but rather update one unique price to each and every website
The bug I am talking about actually happened to me with similar code and is that other product attributes will be unlinked from default (at least, it is what happened to me).
Below is a suggestion based on Knowledge Craving's solution, I just amended a bit
public function updateProductPrices ($sku, $newPrice, $websiteId) {
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$product = Mage::getModel('catalog/product')
$productId = $product->getIdBySku($sku);
$websiteObj = new Mage_Core_Model_Website();
$websiteObj->load($websiteId);
$storeIds = $websiteObj->getStoreIds();
if (count($storeIds)) {
foreach ($storeIds as $_eachStoreId) { //Does it need to do for each storeview, price being anyway website scope?
$product->load($productId);
$oldPrice = $product->getPrice();
if ($oldPrice != $newPrice) { //actually, this if would not be necessary as you anyway want to update, right?
$product->setStoreId($_eachStoreId)
->setPrice($newPrice)
->save();
}
}
}
unset($productId, $storeIds, $websiteObj, $product);
}