I want to make sure a customer cannot set a Quantity other than "1" for some special products. These products all have a special attribute set.

I tried to observe sales_quote_item_qty_set_after and manually set the quantity:

public function enforceQty (Varien_Event_Observer $observer)
    $_item = $observer->getEvent()->getItem();

    // ...

    if ($_item->getQty() > 1) 

which works perfectly fine, except that when I try to get the value of said attribute, to identify my special products, I do not get the correct value:

$_product = $_item->getProduct();
$_productId = $_product->getId();                                                                                                                                                                                         
$_storeId = Mage::app()->getStore()->getId();

$val = Mage::getResourceModel('catalog/product')
        ->getAttributeRawValue($productId, 'special_attr', $storeId);

special_attr is a boolean attribute. In the HTML output of the product, it shows the correct value Yes. In the code above, $val gets set to false for some reason.

I tried several different ways to get the value of special_attr, all of them return false instead of true.

Any Idea?

Edit 1:

I just found a method returning the correct value. Sadly the value returned is language dependent.

$val = Mage::getModel('catalog/product')

Can somebody explain why this method returns the correct value, and the other does not?

Edit 2:

After crawling through the sources for getAttributeText, I found that I can get the value I'm looking for like this:

$val = Mage::getModel('catalog/product')
        ->getData('special_attr') == "1"

In addition to Question 1: Is there a way to get the actual boolean value, instead of a string value?

  • 4
    not really an answer to your question, but maybe a solution to the problem. Magento already has a field 'Max Qty allowed in cart'. Why Not use that one. Set the value to 1 for the products you need and problem solved. – Marius Aug 21 '14 at 14:00
  • That wold work, but I'd need to make sure these two attributes do not get out of sync. 'special_attribute' does already exist and is used to identify these products. – Michael Krupp Aug 21 '14 at 14:30

So I am with Marius here. Magento has this functionality out of the box. Simply update these products so that they have the Max Qty allowed in cart as 1. Then this should work straight away.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.