Magento wrong language in wishlist - magento

I've a problem with the magento wishlist. My store has 4 diffrent languages (DE/EN/FR/IT).
All works great but in the wishlist the product name and description are displayed in the wrong language (in Italien) no matter if the store language is set to English, French etc.
I know there's a simliar thread here
Magento Not Translating Wishlist Product Name and Description
But the suggestion to remove $product = $this->_getData('product'); doesn't solve the problem. It only changes product name and description to the default language, which is German.
//SOLUTION//
I finally got it. I know it's not the most perfect solution but it works.
In /app/code/core/Mage/Wishlist/Model/Item.php at public function getProduct()
i removed this line $product = $this->_getData('product'); and added the following:
$store_id = Mage::app()->getStore()->getStoreId();
Then I changed: $product = Mage::getModel('catalog/product')
->setStoreId($this->getStoreId())
->load($this->getProductId());
to:
$product = Mage::getModel('catalog/product')
->setStoreId($store_id)
->load($this->getProductId());

Thanks for your solution. To avoid multiple product reload, I've changed that code to:
public function getProduct() {
$product = null;
$current_store_id = Mage::app()->getStore()->getStoreId();
if (!$this->getProductId()) {
Mage::throwException(Mage::helper('wishlist')->__('Cannot specify product.'));
}
if($this->_getData('last_store_id') != $current_store_id){
$product = Mage::getModel('catalog/product')
->setStoreId($current_store_id)
->load($this->getProductId());
} else {
$product = $this->_getData('product');
}
$this->setData('product', $product);
$this->setData('last_store_id', $current_store_id);
/**
* Reset product final price because it related to custom options
*/
$product->setFinalPrice(null);
$product->setCustomOptions($this->_optionsByCode);
return $product;}

Related

how to assign random product positions in Magento category?

I have the following issue in Magento 1.9: there are categories with a lot of products in them and the default sort order is by position and all product position in Magento backend is 1. So what happens is that when you open a category on the frontend a lot of similar products show first followed by another set of similar type of products. I want to randomize their positions somehow (by a script or anything else) so that different kind of products show mixed together.
For example I have categories wine,
champagne and whisky and products
in them and I also have category birthday products that includes products from these categories. And when the category is opened on the frontend first a lot of whisky products
are shown then a lot of wine ... etc I want them mixed. Thanks in advance for
any help!
You can do as below :
First get the category :
$category = Mage::getModel('catalog/category')
->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID)
->load($categoryId);
Then its product positions :
$products = $category->getProductsPosition();
This will be an array organized like this :
product_id_1 => position_1
product_id_1 => position_2
So foreach one of those products just set a random position (here between 0 and 9999) :
foreach($products as $productId => $position ){
$products[$productId] = '' . rand(0,9999);
}
And finally save :
$category->setPostedProducts($products);
$category->save();
Here is below a script that you could put in /shell magento directory :
<?php
require_once './abstract.php';
class RandomCategoryOrder extends Mage_Shell_Abstract {
private $_categoryId = 188;
public function run(){
$category = Mage::getModel('catalog/category')
->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID)
->load($this->_categoryId);
$products = $category->getProductsPosition();
foreach($products as $productId => $position ){
$products[$productId] = '' . rand(0,9999);
}
$category->setPostedProducts($products);
try{
$category->save();
}catch(Exception $e){
echo $e->getMessage();
}
}
}
$randowCategoryOrder = new RandomCategoryOrder();
$randowCategoryOrder->run();

Magento Multiple Wishlist creation programmatically

I know how to add products to a customer's wishlist programmatically however it only adds to one wishlist. I have the multiple wishlist option set to enabled however I do not know how to create a new wishlist instead of merging products into the existing wishlist.
public function submitQuote(Mage_Adminhtml_Model_Session_Quote $quote)
{
$currentQuote = $quote->getQuote();
$customer = $currentQuote->getCustomer();
$items = $currentQuote->getAllVisibleItems();
//$wishlist = Mage::helper('wishlist')->getWishlist();
//Mage::register('wishlist', $wishlist);
$wishlist = Mage::getModel('wishlist/wishlist');
$curretDate = date('m/d/Y', time());
$wishlist->setCustomerId($customer->getId());
$wishlist->setName('Quote ' . $curretDate)
->setVisibility(false)
->generateSharingCode()
->save();
foreach ($items as $item)
{
$productId = $item->getProductId();
$product = Mage::getModel('catalog/product')->load($productId);
$buyRequest = $item->getBuyRequest();
$result = $wishlist->addNewItem($product, $buyRequest);
if(is_string($result))
{
Mage::throwException($result);
}
$wishlist->save();
}
//Mage::unregister('wishlist');
}
I've had this problem before. To add a product to a customer's wishlist you need to start a wishlist model and call the addNewItem method passing the product object. Here is how I do it.
$customer = Mage::getModel('customer/customer');
$wishlist = Mage::getModel('wishlist/wishlist');
$product = Mage::getModel('catalog/product');
$customer_id = 1;
$product_id = 1;
$customer->load($customer_id);
$wishlist->loadByCustomer($customer_id);
$wishlist->addNewItem($product->load($product_id));
Hope this helps!
EDITED:
$customerid= "YOUR CUSTOMER ID "; // Modify This
$customer=Mage::getModel("customer/customer")->load($customerid);
$wishlist=Mage::getModel("wishlist/wishlist")->loadByCustomer($customer,true);
Check for reference this: app/code/core/Mage/Wishlist/Model/Wishlist.php
LATEST EDIT:
I see that your problem is here:
$result = $wishlist->addNewItem($product, $buyRequest);
FINAL EDIT:
Then create another function and add the items and wishlist you want to add it to (as parameters). This should add items to the wishlist that you have passed as an argument when calling the function. Its the only solution I can come up with. Hope this helps!
I just stumbled across this question because I'm trying to do something similar - your code part helped me to create a new wishlist, and I've managed to do the rest so thought I'd share.
This code will;
Create a new wishlist with the name 'Quote dd/mm/YY h:m:s'
Load the wishlist collection for the current user, ordered by highest ID first
Load the newest wishlist
Add all basket items to it
Redirect the user to the new wishlist view.
public function addToQuoteAction() {
if(Mage::getSingleton('customer/session')->isLoggedIn()) {
$customerData = Mage::getSingleton('customer/session')->getCustomer();
$customerId = $customerData->getId();
} else {
Mage::getSingleton('core/session')->addError($this->__('Please login to use this feature'));
return $this->_redirectUrl($this->_getRefererUrl());
}
// Create the wishlist
$newWishlist = Mage::getModel('wishlist/wishlist');
$curretDate = date('d/m/Y h:m:s', time());
$newWishlist->setCustomerId($customerId);
$newWishlist->setName('Quote ' . $curretDate)
->setVisibility(false)
->generateSharingCode()
->save();
// Find the newly create list and load it
$wishlistCollection = Mage::getModel('wishlist/wishlist')->getCollection()
->addFieldToFilter('customer_id',$customerId)->setOrder('wishlist_id');;
$firstWish = $wishlistCollection->getFirstItem();
$wishlist = Mage::getModel('wishlist/wishlist')->load($firstWish->getWishlistId());
$cart = Mage::getModel('checkout/cart')->getQuote();
//getAllItems
foreach ($cart->getAllVisibleItems() as $item) {
$productId = $item->getProductId();
$buyRequest = $item->getBuyRequest();
$result = $wishlist->addNewItem($productId, $buyRequest);
$wishlist->save();
}
Mage::getSingleton('core/session')->addSuccess($this->__('All items have been moved to your quote'));
return $this->_redirectUrl(Mage::getBaseUrl().'wishlist/index/index/wishlist_id/'.$firstWish->getWishlistId().'/');
}

Get configurable sku from simple SKU or ID

I'm attempting to get a parent configurable product SKU from one of it's child simple products' SKU or ID. My understanding is that a simple can belong to multiple configurables, but I want the specific configurable the customer added to their cart. I read a related question but it gets all parent configurables.
I'm using Magento EE 1.12
EDIT: More detail on what I'm doing. When a customer checks out successfully I'm attempting to get SKU's of simples and the configurable the customer checked out with.
Attempting to apply code to:
app/design/frontend/enterprise/mytheme/template/checkout/success.phtml
If the configurable is in the cart already I think you can interrogate the cart to find the configurable and its simple ID.
$myTargetSimpleProductId = $someIdThatYouKnow;
$quote = Mage::getSingleton('checkout/session')->getQuote();
$cartItems = $quote->getAllVisibleItems();
foreach ($cartItems as $item){
if ($option = $item->getOptionByCode('simple_product')) {
$productIdArray[] = $option->getProduct()->getId(); //for the record
if ($option->getProduct()->getId()==$myTargetSimpleProductId ){
$myMatchingConfigurableProductId = $item->getProductId(); //capture the ID of the configurable
}
}
}
//$productIdArray holds a list of all the IDs of simple products that are in the basket due to configurables.
echo("The answer is ".$myMatchingConfigurableProductId);
Code adapted from this Q&A and that Q&A and not tested so let me know if this bombs horribly and you can't figure out all the corrections.
****EDIT to explain the code a bit more following the comment below ***
Overall, it helps to understand that when someone adds a configurable product to the cart Magento principally stores the product ID of the configurable not the product ID of the underlying simple product. But Magento being Magento the configured product in the cart is a complex object, part of which is a reference to the underlying simple product.
So:
$item->getProductId(); //Really means [pseudo code] $item->getConfiguredProductId()
$item->getOptionByCode('simple-product') //Accesses the underlying simple product object, hence
$item->getOptionByCode('simple-product')->getProduct()->getId() //gives accesse to the ID of the underlying simple product - ie the thing you want to test.
Now, if you are on the success page the challenge is how to access the order items. There are a sprinkling of answers on Stack Overflow for that and here is a sampling:
$_order = Mage::getModel('sales/order')->loadByIncrementId($this->getOrderId());
foreach ($_order->getAllItems() as $item) {
due to this Q&A. Or
$_customerId = Mage::getSingleton('customer/session')->getCustomerId();
$lastOrderId = Mage::getSingleton('checkout/session')->getLastOrderId();
$order = Mage::getSingleton('sales/order');
$order->load($lastOrderId);
foreach ($order->getItemsCollection() as $item) {
or from an observer function:
$order = $observer->getOrder();
/* #var $item Mage_Sales_Model_Order_Item */
foreach ($order->getItemsCollection() as $item) {
both due to this Q&A.
But I think you might benefit most from this tutorial from the Magento savvy Yireo:
$orderId = Mage::getSingleton('checkout/session')->getLastRealOrderId();
$order = Mage::getModel('sales/order')->loadByIncrementId($orderId);
$cartItems = $order->getAllItems();
foreach($cartItems as $item) {
due to Jisse Reitsma of Yireo.com
So you should be all set. There are a few bits and pieces to string together into your final code but I think you are either putting your code in template/checkout/success.phtml or in an observer on checkout_type_onepage_save_order_after and the snippets above will guide you.
****FURTHER EDITS***
If you have a $product that is product_type = 'configurable' then to get its SKU you should call $product->getData('sku'); if you only call $product->getSku(); that will always return the simple SKU due to
//file: app/code/core/Mage/Core/Catalog/Model/Product.php
//class: Mage_Catalog_Model_Product
public function getSku()
{
return $this->getTypeInstance(true)->getSku($this);
}
and if you follow the code, you find
//file: app/code/core/Mage/Core/Catalog/Model/Product/Type/Configurable.php
//class: Mage_Catalog_Model_Product_Type_Configurable
public function getSku($product = null)
{
$sku = $this->getProduct($product)->getData('sku');
if ($this->getProduct($product)->getCustomOption('option_ids')) {
$sku = $this->getOptionSku($product,$sku);
}
return $sku;
}
I would add that I have been running lots of code tests. When I set up an observer on the event checkout_type_onepage_save_order_after then
$cartItems = $observer->getEvent()->getOrder()->getAllVisibileItems();
foreach ($cartItems as $item){
if ($item->getProductType() == 'configurable') {
$skuConfigurable = $item->getProduct()->getData('sku');
$skuMatchingSimple = $item->getProduct()->getSku();
Mage::log("skuConfigurable ".$skuConfigurable." has skuMatchingSimple ".$skuMatchingSimple, null, 'mylogfile.log');
}else{
Mage::log("Configurable SKU"." (not configurable product)", null, 'mylogfile.log');
}
}
Works a treat, but when I access the order items from within success.phtml then both getSku() and getData('sku') both return the SKU of the configurable. Which leads me to think that I am not loading the sales/order correctly or not understanding how to ascertain the 'configurable option' from the configurable $item. But I can't figure out why there is that difference between `$item' in the observer compared to the success.phtml.
You probably know this already but I thought I would add: If you catch the event then you can access both the sale/quote and the sale/order objects but by the time you get to success.phtml the sales/quote object has been reset and you can only access sale/order (which I think is okay for you if all you need is the configurable sku).
Here is the code I am running in success.phtml, for me it only returns the config SKU
<?php //TEST CODE to retrieve SKUs from the order
$_checkoutSession = Mage::getSingleton('checkout/session');
$_customerSession = Mage::getSingleton('customer/session');
$orderId = Mage::getSingleton('checkout/session')->getLastRealOrderId();
$order = Mage::getModel('sales/order')->loadByIncrementId($orderId);
echo("<br>Order Id".$orderId);
$myTargetSimpleProductId = 42;//$someIdThatYouKnow;
//$cartItems = $order->getAllVisibleItems(); //returns the configurable items only
$cartItems = $order->getAllItems();
Mage::log(':PHTML--PHTML--PHTML--PHTML--PHTML--PHTML--PHTML--PHTML--', null, 'mylogfile.log');
Mage::log(':', null, 'mylogfile.log');
Mage::log('cartItems (from order):', null, 'mylogfile.log');
foreach ($cartItems as $item){
Mage::log("product_id".$item->getProductId(), null, 'mylogfile.log');
Mage::log("product_type".$item->getProductType(), null, 'mylogfile.log');
Mage::log("product_real_type".$item->getRealProductType(), null, 'mylogfile.log');
if ($option = $item->getOptionByCode('simple_product')) { //NEVER RETURNS TRUE, why?
Mage::log("item_opByCode_getProd_getId".$item->getOptionByCode('simple_product')->getProduct()->getId(), null, 'mylogfile.log');
}else{
Mage::log("item_opByCode_getProd_getId"." (not simple_product option)", null, 'mylogfile.log');
}
if ($item->getProductType() == 'configurable') {
$dummy = $item->getProduct()->getData('sku');
Mage::log("Configurable SKU ".$dummy, null, 'mylogfile.log'); $myAnswers[]=array('simpleSku'=>$item->getProduct()->getSku(),'configurableSku'=>$item->getProduct()->getData('sku')); //in success.phtml these two are always the same (the configurable SKU)
}else{
Mage::log("Configurable SKU"." (not configurable product)", null, 'mylogfile.log');
}
Mage::log("item options".print_r($item->getOptions(),true), null, 'mylogfile.log');
Mage::log("getProduct()->getId()".$item->getProduct()->getId(), null, 'mylogfile.log');
Mage::log("getProduct()->getSku".$item->getProduct()->getSku(), null, 'mylogfile.log');
Mage::log("getProduct()->getName()".$item->getProduct()->getName(), null, 'mylogfile.log');
Mage::log("SKUs : ".print_r($myAnswers,true), null, 'mylogfile.log');
Mage::log("<br>********************************", null, 'mylogfile.log');
if($item->getOptions()){ //NEVER RUNS - how get the configurable product options?
echo("OPTIONS".print_r($item->getOptions(),true));
}
}
echo("SKU's array".print_r($myAnswers,true));
I hope something here works for you. One time in the past I wrote a theme that only puts the simple product in the cart even if it originated as a configurable option so maybe worth checking your theme isn't doing that (by developing this code in the default theme until it works)
I just needed the SKUs for an AJAX cart function. This worked for me:
$cart = Mage::helper('checkout/cart')->getCart();
$items = $cart->getItems();
foreach ($items as $item) {
if ($item->getProductType() == 'configurable') {
$sku = $item->getProduct()->getData('sku');
} else {
$sku = $item->getSku(); // simple product
}
}
Answer pulled mostly from Malachy's dissertation...
There have any direct function to fetch parent Product sku
<?php
$parentIds = Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($childproid);
/* $parentIds is array of ids */
foreach( $parentIds as $eachProID)
$child=Mage::getModel('catalog/product')->load($eachProID);
}
?>

Magento - Get product name for all languages

When reading all magento products for a export extension I've encountered a problem:
when trying to get the name of a product by using getName() on the loaded model you only get the active language name or if that is not set the default name of the product. But I need to get all product names for default, english, german, french, etc.
Does anyone have a solution for this problem or an idea how to solve it?
$model = Mage::getModel('catalog/product');
$collection = $model->getCollection();
foreach ($collection as $product) {
$id = $product->getId();
$model->load($id);
$name = $model->getName(); // gives you only the active language name / default name
}
Since you also want the default store, I'm only aware of one working way:
$aStoreHash = Mage::getModel('core/store')
->getCollection()
->setLoadDefault(true)
->toOptionHash();
$aName = array();
foreach ($aStoreHash as $iStoreId => $sStoreName) {
Mage::app()->setCurrentStore($iStoreId);
$oCollection = Mage::getModel('catalog/product')
->getCollection()
// Uncomment next line for testing if you have thousands of products
// ->addFieldToFilter('entity_id', array('from' => 1, 'to' => 5))
->addAttributeToSelect('name');
foreach ($oCollection as $oProduct) {
$aName[$oProduct->getId()][$iStoreId] = $oProduct->getName();
}
}
var_dump($aName);
If you don't need the default store, you could drop Mage::app()->setCurrentStore($iStoreId); and use ->addStoreFilter($iStoreId) on the collection instead.

Check whether a product is in the wishlist or not

I'm working on a Magento theme, and I need to build a function that can check to see whether or not a product has been added to the user's wishlist.
Magento has a "Mage_Wishlist_Helper_Data" helper class, but I have no idea how to build a check-if-already-in-wishlist function. Basically I need to use Magento's wishlist feature to build a favorites list. I want to add a special class to the "add to wishlist" link if the particular product was already added to the user's favorites.
<?php $wishlist = Mage::getModel('wishlist/item')->load($_product->getId(),'product_id');
if($wishlist->getId())
//product is added
echo "Added! - Product is in the wishlist!";
else
//add product to wishlist
echo "<a href='".$this->helper('wishlist')->getAddUrl($_product) ."'>Add This?</a>";
;?>
Since collections are lazy loaded, I am assuming you can do something such as:
$_product = ...; // some product object you already have
$_productCollection = Mage::helper('wishlist')->getProductCollection()
->addFieldToFilter('sku', $_product->getSku());
if($_productCollection->count() > 0) {
// User already has item in wishlist.
}
You can do similar filtering on other fields, but SKU should be sufficient in this case.
I'm only getting back to this project now, but I took Daniel Sloof's suggestion, and it worked perfectly using the function below:
public function isInWishlist($item)
{
$_productCollection = Mage::helper('wishlist')->getProductCollection()
->addFieldToFilter('sku', $item->getSku());
if($_productCollection->count()) {
return true;
}
return false;
}
This checks to see whether or not a product has been added into the current user's Wishlist. I called it my template file like this:
if ($this->helper('wishlist')->isInWishlist($_product)) :
I found this solution after checked select query of Mage::helper('wishlist')->getWishlistItemCollection(). I hope this solution help to someone.
/**
* Check customers wishlist on identity product.
* #param Mage_Catalog_Model_Product $_product
* #return bool
*/
private function _isInWishlist($_product)
{
$_productCollection = Mage::helper('wishlist')->getWishlistItemCollection()
->addFieldToFilter('product_id', $_product->getId());
if ($_productCollection->count()) {
return true;
}
return false;
}
I solved this by the below function. I Hope this may help some one.
function checkInWishilist($_product){
Mage::getSingleton('customer/session')->isLoggedIn();
$session = Mage::getSingleton('customer/session');
$cidData = $session->isLoggedIn();
$customer_id = $session->getId();
if($customer_id){
$wishlist = Mage::getModel('wishlist/item')->getCollection();
$wishlist->getSelect()
->join(array('t2' => 'wishlist'),
'main_table.wishlist_id = t2.wishlist_id',
array('wishlist_id','customer_id'))
->where('main_table.product_id = '.$_product->getId().' AND t2.customer_id='.$customer_id);
$count = $wishlist->count();
$wishlist = Mage::getModel('wishlist/item')->getCollection();
}
else {
$count="0";
}
if ($count) :
return true;
else:
return false;
endif;
}
Since #alexadr.parhomenk s' solution causes undesired results, here's a smaller method that does the trick:
/**
* Check customers wishlist on identity product.
* #param int $productId
* #return bool
*/
public function isInWishlist($productId)
{
$ids = Mage::registry('wishlist_ids');
if (!$ids) {
$productCollection = Mage::helper('wishlist')->getWishlistItemCollection();
$ids = $productCollection->getColumnValues('product_id');
Mage::register('wishlist_ids', $ids);
}
return in_array($productId, $ids);
}

Categories

Resources