Working with Magento’s Wishlist and Multiple Stores/Websites

Published by John on August 29, 2017 Under Magento

Magento LogoRecently, while working on a Magento store front, I ran into an issue with how the wishlist works when you have multiple stores and upon digging into the wishlist code, found a neat feature that I could not find much documentation on.

In this case, several shops were setup on different subdomains, with some sharing products with the main website. When someone added a product to their wishlist, it was added to the wishlist of whatever store they were browsing. So, for example, if the product was on both store1.shops.com and store2.shops.com and you added it while viewing the product on store2, you would be redirected to store2’s wishlist and it would be visible there, but not on store1’s wishlist. For this particular setup, we actually wanted the product to be on store1’s wishlist, even when viewing store2.

I began to do a little digging and found the following code in app/code/core/Mage/Wishlist/Model/Wishlist.php:

public function addNewItem($product, $buyRequest = null, $forciblySetQty = false)
    {
       [...]

        if ($product instanceof Mage_Catalog_Model_Product) {
            $productId = $product->getId();
            // Maybe force some store by wishlist internal properties
            $storeId = $product->hasWishlistStoreId() ? $product->getWishlistStoreId() : $product->getStoreId();
        } else {
           
            [...]

When adding a product to the wishlist and setting the $storeId, the addNewItem function checks to see if the product has a wishlist store ID set and if it does, adds the product to that store’s wishlist. If it does not, it defaults to the current storeId.

A quick grep of the core code files found that the only file that has the phrase ‘WishlistStoreId’ in it was Wishlist.php. It was used in the above function and also the _addCatalogProduct function. I also searched for ‘wishlist_store_id’ , as well as checked a few places in the backend and didn’t see any references to it.

So, this appears to be a feature that was added with some decent foresight by a Magento Dev, but not something setup/configured by default.

How do I use the WishlistStoreId?

If you want to specify a specific store ID to be used when adding products to the wishlist, this can be done relatively easily, as described below:

  1. Goto Catalog -> Attributes -> Manage Attributes -> Add New Attribute
  2. Create a new ‘text field’ attribute, with a code of ‘wishlist_store_id’, probably Global Scope since you are dealing with multiple stores
  3. Goto Catalog -> Attributes -> Manage Attribute Sets and select the main product attribute set.
  4. Drag the new ‘wishlist_store_id’ attribute over to somewhere within the attribute set and save the set, updating indexes if necessary.
  5. Now, edit an existing product and set the ‘wishlist_store_id’ to the store ID of one of your stores.
  6. On the front end, when this product is added to a wishlist, it should now get added to the wishlist of the store you specified above, no matter what store you are actually viewing it on.

Possible Issues with This Setup

From looking at the above code, you can see that the hasWishlistStoreId check occurs after checking to see if the $product variable passed to the addNewItem function is an Mage_Catalog_Model_Product object. If it is not, it defaults to using the storeId from the $buyRequest variable. So, there may be some cases where this does not work, however when adding a product to the wishlist using the links found on the product or category page, I think it should always work, as the wishlist controller throws an exception if it is unable to load the product as an object.


No Comments |

Add a Comment