Magento Add to Wishlist code not working

Magento Add to Wishlist code not working

July 14, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento Add to Wishlist code not working

Debugging Magento Wishlist Add-to-Wishlist Functionality

Troubleshooting Your Existing Code

You’ve attempted to implement wishlist functionality on your product view page using the provided PHP code snippet. Unfortunately, it seems that neither the “Add to Wishlist” nor the “Add to Compare” buttons are currently working as expected.

Let’s take a closer look at the code and potential issues:

<a onclick="setLocation('<?php echo $this->getAddToWishlistUrl($_product) ?>')"
   class="buttons-wish"
   title="<?php echo $this->__('Add to Wishlist') ?>"></a>
<a onclick="setLocation('<?php echo $this->getAddToCompareUrl($_product) ?>')"
   class="buttons-compare"
   title="<?php echo $this->__('Add to Compare') ?>"></a>

Identifying Potential Issues

There are a few possible reasons for the failure of this code:

  1. Check Your Magento Version: Ensure that your Magento installation is up-to-date and supports the getAddToWishlistUrl and getAddToCompareUrl methods. These methods were introduced in later versions (Magento 2.0+).

  2. Verify the _product Object: Double-check if $_product contains a valid product object from your catalog. You might need to adjust the context or use appropriate product retrieval methods based on your page layout.

  3. Inspect JavaScript Functionality: The setLocation function used in the onclick attribute is not a built-in Magento function. Ensure that this function exists and is correctly implemented elsewhere in your theme’s JavaScript files.

  4. Check for PHP Errors or Logs: Enable error reporting in your environment to check if there are any PHP notices, warnings, or errors preventing the URLs from being generated properly. You can refer to our guide on enabling Magento error logs for more information.

While troubleshooting your existing code, it’s also worth considering a cleaner implementation using Magento’s built-in wishlist and compare blocks. This approach ensures proper integration with the core functionality:

  1. Include the Wishlist Block: In your product view template (e.g., catalog/product/view.phtml), include the wishlist/links block to render the “Add to Wishlist” button:
<?php echo $this->getChildHtml('wishlist_links') ?>
  1. Include the Compare Block: Similarly, add the catalog.product.compare block for the “Add to Compare” functionality:
<?php echo $this->getBlockHtml('catalog.product.compare') ?>

Remember to place these blocks within your product view template where you want the buttons to appear.

Conclusion: Resolving the Issue

To fix the current problem, I recommend the following steps:

  • Update your Magento installation to a supported version (2.0+).
  • Verify that $_product contains a valid catalog object.
  • Check for and resolve any JavaScript or PHP errors related to the setLocation function or URL generation.
  • If needed, refactor your template using the built-in wishlist and compare blocks for a cleaner implementation.

If you’re still encountering difficulties, please don’t hesitate to share your Magento version number, product view template structure, and any relevant error logs. I’ll be glad to provide further assistance in resolving this issue.

For more tips on optimizing Magento performance and troubleshooting common issues, feel free to explore our Magento development resources at Laravel Company.

Magento 2 Catalog Themes Errors

Our network