1

I have a Mage_Sales_Model_Order_Shipment_Item object and I'm able to load a product model using the shipment item's sku (if I can get this attribute by just using the shipment item, all the better).

The loaded product model doesn't seem to contain the size attribute. When I var_dump($prod->getData()) or var_dump($prod) there is nothing about its size. The attribute's code is size. It is set on the product I am loading, it is global, Dropdown type, not unique, required. How can I get a specific product attribute, in this case "size", from a model loaded by sku?

$prod = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);
echo $prod->getSize();
echo $prod->getData('size');

Also, I would expect this to return a value of 57. It is a dropdown type attribute. How can I get the value for this option_id? For example, I'd like to get the value "small", not 57. Btw, I see these values set for this particular sku in the database.

I've tried this solution also

$pc = Mage::getResourceModel('catalog/product_collection')
    ->addAttributeToFilter('size', array('notnull' => 'true'))
    ->addAttributeToFilter('size', array('neq' => ''))
    //->addAttributeToFilter('sku', array('eq' => '100-0055-BLK-S'))
    ->addAttributeToSelect('size');
//$p = $pc->getFirstItem();
$usedAtts = array_unique($pc->getColumnValues('size'));
$attModel = Mage::getSingleton('eav/config')->getAttribute('catalog_product', 'size');
$usedAtts = $attModel->getSource()->getOptionText(implode(',', $usedAtts));
var_dump($usedAtts);

But $usedAtts is false.

Update: I tried the method in my first example, loading the product with sku, on a fresh install and it worked. I setup the size attribute just the same, except the new install has it in the 'default' attribute set, where the prior is a custom. I will try to look into those differences.. but in the meantime, anyone have an idea of why I can't get it in my prior install? I setup the products the same. In the prior, I can get the color attribute.. not sure why I can't get size.

4 Answers 4

3

To answer the question:

Thanks to some handy built-in methods you don't have to do any loading on your own:

  • Mage_Sales_Model_Order_Shipment_Item provides getOrderItem
  • Mage_Sales_Model_Order_Item provides getProduct

So, all you should have to do is this:

$product = $shipmentItem->getOrderItem()->getProduct();

If size is persisted to the order item, you can do:

$item = $shipmentItem->getOrderItem();
echo $item->getAttributeText('size');

Or, if you need to load the ID value from the product itself, you can get all of the product's selected ID values:

$size = $shipmentItem->getOrderItem()->getProduct()->getSize();
4
  • $product = $shipmentItem->getOrderItem()->getProduct(); gives me the configurable product for the simple product that was ordered. And size is only set at the simple level. Check this out: $prod = $si->getOrderItem()->getProduct(); echo $si->getSku() . ' | ' . $prod->getSku(); That gives me 100-0055-BLK-S | 100-0055, simple and configurable. Commented Jul 2, 2013 at 0:00
  • I'm guessing that the -S at the end of the simple sku is for size small?
    – philwinkle
    Commented Jul 2, 2013 at 0:03
  • Yes lol×Comments must be at least 15 characters in length.× Commented Jul 2, 2013 at 0:04
  • I think this is my best bet, if I could just get the simple. Something weird is happening. I added a new simple and config test product and linked them using default att set with this size att added, and I couldn't even load the product using $prod = Mage::getModel('catalog/product')->loadByAttribute('sku', '1-1-BLK-S'); .. it returns false. I have a feeling the problems are related. Commented Jul 2, 2013 at 0:06
2

The problem was that when I was initializing Magento it was defaulting to the "default" store view (go figure). I needed to set it to the admin store view, then I'm able to get all the product info I needed.

require 'app/Mage.php';
umask(0);
Mage::app();

//admin store
Mage::app()->setCurrentStore(0);

And this code now works just fine:

$prod = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);
echo $prod->getSize() . ' - ' . $prod->getAttributeText('size');
2
  • Glad to see that getAttributeText is back to relive its glory days!
    – philwinkle
    Commented Jul 2, 2013 at 20:19
  • Default store view IS 0. What store view was yours defaulting to?
    – Benubird
    Commented Sep 3, 2013 at 13:08
0

Its easier to just pass this attribute to your quote and order. That way you can get it allover.

Add it to quote (in your config.xml from your extension)

<sales>
<quote>
    <item>
        <product_attributes>
            <custom_attribute />
        </product_attributes>
    </item>
</quote>

convert

<fieldsets>
    <sales_convert_quote_item>
        <custom_attribute>
            <to_order_item>*</to_order_item>
        </custom_attribute>
    </sales_convert_quote_item>
    <sales_convert_order_item>
        <custom_attribute>
            <to_quote_item>*</to_quote_item>
        </custom_attribute>
    </sales_convert_order_item>
</fieldsets>

Copy the attribute to quote

    <events>
            <sales_quote_item_set_product>
                <observers>
                    <yourmodule>
                        <class>yourmodule/observer</class>
                        <method>setCustomAttribute</method>
                    </yourmodule>
                </observers>
            </sales_quote_item_set_product>
   </events>

All these are under the global tag in your config.

Next you can copy your attribute to the quote trough the observer you just assigned the event to in your config:

public function setCustomAttribute(Varien_Event_Observer $observer) {
    $_item = $observer->getQuoteItem();
    $_product = $observer->getProduct();
    $_item->setCustomAttribute($_product->getCustomAttribute());
}

This way you won't have to load your product to get the value in your shippingmodel.

1
  • Is this still a valid way in Magento 1.9?
    – Jurik
    Commented May 10, 2016 at 8:35
0

Just to add to these answers; if you need to get the attribute value, and you don't have the camel-case code available, you could use getData() on the product instead:

$product = $this->getProduct();
$attributeValue = $product->getData('attribute_code_here');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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