Recently I had to implement something really interesting in Magento. My task was to check if coupon could be applied to product before the product is added in cart and coupon is submitted. I started implementing this validation on my own but after some digging in Magento core I found the best solution.
I constructed the snippet below after digging in Magento SalesRule module:
// Sales Rule Id of the coupon we want to validate
$ruleId = 359;
// Id of Product we want to validate
$productId = 884;
/** @var Mage_SalesRule_Model_Rule $salesRule */
$salesRule = Mage::getModel('salesrule/rule')->load($ruleId);
/** @var Mage_Catalog_Model_Product $product */
$product = Mage::getModel('catalog/product')->load($productId);
$item = new Varien_Object();
$isValid = (bool) $salesRule->getActions()->validate($item);
var_dump($isValid); // true OR false
Hope this helps!
Let’s assume that as developer you are in following situation: You know a product id and you have to fetch single attribute value of a product which id you already know. For example let’s try to get color attribute of Magento product.
Question: How would you do it? Are you going to load the product or you would look for more efficient solutions in order to fetch only the attribute you are looking for?
Magento allows us to fetch product attribute in many ways and depending on the context we could choose one. I am definitely against loading entire product in case there are much more efficient solutions and the goal is to fetch single product attribute (not all product attributes).
I think that people choose the easy way because:
- People read the first answer (which sometimes is not the most efficient) in StackOverflow but don’t check other answers.
- There is a lack of Magento documentation.
- Missing out of the box way how to accomplish the task.
- There are many examples in Internet but they are not descriptive enough to explain what happens on background and how efficient they are.
Continue reading “Magento: Load single product attribute vs. load entire product”
Today I will introduce Magento extension that allows developers to create different choosers in Magento admin forms.
List of available choosers:
- Image Chooser
- Product Chooser
- Category Chooser
- CMS Page Chooser
- Static Block Chooser
You can clone or download the extension from here: https://github.com/extendix/Extendix_AdminFormChooserButton
For this extension I used a code base of Magento extension that I created while I was working for Session Digital GmbH. I decided to take control and continue maintenance of the extension from my account and my namespace.
Continue reading “Magento: Extension for creating chooser button in admin form”
This morning I was looking for some new and interesting articles and I found one titled Are You Giving Your Users Positive Feedback? In this article I’ve found one very simple and elegant solution of something that always bothered me while shopping online. As I’m impatient person I always have problems with clicking more than once on the add to cart button and few times I’ve purchased items in bigger quantity that I’ve intended. I’ve never seen a web shop, that offers this simple solution presented in the article I’ve read – adding message for items that are already in the shopping cart.
Continue reading “Magento: Check if product is already in cart”