danneh.org

Tag: magento

Getting value of Attribute Option and adding a new Attribute Option in Magento

by Dan on Jan.29, 2011, under Magento, PHP

This, I find, is a hard one to come across. At least, trying to find a sensible solution that just spits out a numerical value which, more often than not, is all you want. Needing to get the value of an option from a multi-option/dropdown attribute. Try this:

public function getAttributeOptionValue($arg_attribute, $arg_value) {
    $attribute_model        = Mage::getModel('eav/entity_attribute');
    $attribute_options_model= Mage::getModel('eav/entity_attribute_source_table') ;
 
    $attribute_code         = $attribute_model->getIdByCode('catalog_product', $arg_attribute);
    $attribute              = $attribute_model->load($attribute_code);
 
    $attribute_table        = $attribute_options_model->setAttribute($attribute);
    $options                = $attribute_options_model->getAllOptions(false);
 
    foreach($options as $option) {
        if ($option['label'] == $arg_value) {
            return $option['value'];
        }
    }
 
    return false;
}

And use it as such:

$optionValue = $this->getAttributeOptionValue("size", "XL");

where “size” is the attribute name/ID in the database, and “XL” is the exact label belonging to the option you need to get. Likewise, if you need to add a new option to the attribute’s list, use this:

public function addAttributeOption($arg_attribute, $arg_value) {
    $attribute_model        = Mage::getModel('eav/entity_attribute');
    $attribute_options_model= Mage::getModel('eav/entity_attribute_source_table') ;
 
    $attribute_code         = $attribute_model->getIdByCode('catalog_product', $arg_attribute);
    $attribute              = $attribute_model->load($attribute_code);
 
    $attribute_table        = $attribute_options_model->setAttribute($attribute);
    $options                = $attribute_options_model->getAllOptions(false);
 
    $value['option'] = array($arg_value,$arg_value);
    $result = array('value' => $value);
    $attribute->setData('option',$result);
    $attribute->save();
 
    return $this->getAttributeOptionValue($arg_attribute, $arg_value);
}

and use as such:

$optionValue = $this->addAttributeOption("size", "XXL");

again, where “size” is the attribute name/ID, and “XXL” is the label of the new option required. It returns the value of the option it just added incase it’s needed. Bear in mind that this function doesn’t first check to see if the label already exists as an option, so you should probably use $this->getAttributeOption() first and only run $this->addAttributeOption() upon a false return.

It’s probably not the most efficient way of doing it, but if efficiency is what you really need, $options could be cached/saved so that the DB/model is only every queried once across both functions within the same model.

4 Comments :, , , , , , more...

Update: Bug in Magento 1.4.1.1 core affecting Catalog Price Rules targetting Customer Groups

by Dan on Nov.17, 2010, under Magento, PHP

I blogged here regarding a bug in Magento’s core affecting Catalog Price Rules under promotions. This would effect, for instance, wholesale pricing. Well, Magento confirmed on the bug report that this was fixed in their new release, 1.4.2.0-RC1. I’ve tested this on my staging server and confirm that this is the case.

Discount away!

Leave a Comment :, , , , more...

Bug in Magento 1.4.1.0/1.4.1.1 affecting the recalling of newsletter templates to send

by Dan on Sep.24, 2010, under Magento, PHP

So I wondered if I could go a whole week without finding another bug/flaw/issue in the latest Magento. I was wrong.

We’re trying to send out a newsletter using the built-in feature. I created a template, then went to queue to send it, and I was like; “So, what’s this ‘message’ field?”. I pondered for a bit, typed a message in, thinking it may have just been for some placeholder in the template somewhere, and hit send. When the newsletter turned up, lo-and-behold, there was just my message, without the rest of the template. WTF? Did it again, thinking I’d screwed something up – same result. Hmm..

Bit of Googling at this point revealed this thread on Magento’s forums. “A bug?”, I thought as I read on. “Couldn’t possibly be!”. </sarcasm>

As the code/class that had the flaw only had one function and extended the Adminhtml_Block_Widget_Form class, I decided to just write a quick extension to replace the class that Magento comes with, with this flaw fixed. I attach to this post my quick extension. Just drop this in the Magento webroot and extract. It’ll put the files in the relevant places, and just start working.

Attached file here: BTS NewsletterFix.tar (md5: df19810f175630fd9206d31e3147c3da)

4 Comments :, , more...

Adding multiple products to the cart simultaneously in Magento

by Dan on Sep.09, 2010, under Magento, PHP

Some people ask me if it’s possible to do what some other sites do, allowing you to check a box next to a product on a listing page, and then add all selected items to the cart in one go. This is possible in Magento, it’s just not built in. There’s a few ways you can do this. You can either make an ajax call using a button against each product, which fires off a GET request to the server to add. This method will most likely return the HTML for the cart, and since we’re not interested in the response, said HTML can be discarded.

Continue reading “Adding multiple products to the cart simultaneously in Magento” »

5 Comments :, , , , , , more...

Magento Discount Rules – Buy X Get Lowest Priced Item

by Dan on Sep.09, 2010, under Magento

I figured out today that the discount/promotion rules in Magento aren’t exactly obvious, nor straight forward to apply. If you don’t have the logical mind of a programmer, setting up rules for products that only meet certain criteria can be a tricky one. Take this example:

Buy 4 for 3 on any product in [this] category.

Sounds simple, no? Actually, no. Continue reading “Magento Discount Rules – Buy X Get Lowest Priced Item” »

7 Comments :, , , , more...

Blogroll

A few highly recommended websites...