0

I've created a button in my Magento 1.9 admin that when pressed, will submit the order to our printing company (once complete). Here's how it looks at the moment:

<?php
include_once Mage::getModuleDir('controllers', 'Mage_Adminhtml') . DS . 'Sales' . DS . 'OrderController.php';

class MG_Dropship_Adminhtml_Sales_OrderController extends Mage_Adminhtml_Sales_OrderController {

public function dropshipAction() {

  $orderID = $this->getRequest()->getParam('order_id');
  $order = Mage::getModel("sales/order")->load($orderID);
  $customer = $order->getShippingAddress()->getData();
  $items = array();

  $orderItems = $order->getAllVisibleItems();
  foreach ($orderItems as $item) {

    var_dump($item->sku);

At this point, it does provide me with the correct SKU. The SKU returned is for a simple product (that is linked to a configurable product). Now I'm trying to load the product model of this SKU:

$product = Mage::getModel('catalog/product');
$product->load($product->getIdBySku($item->sku));

var_dump($product);

This returns the following:

object(Mage_Catalog_Model_Product)#358 (32) {
  ["_cacheTag":protected]=>
  string(15) "catalog_product"
  ["_eventPrefix":protected]=>
  string(15) "catalog_product"
  ["_eventObject":protected]=>
  string(7) "product"
  ["_canAffectOptions":protected]=>
  bool(false)
  ["_typeInstance":protected]=>

I won't bother listing it all, but everything I'm trying to gain access to seems to be protected. Do I need to somehow allow access to the product model in my module?

UPDATE: I'm now able to access data such as $product->getName() - but other data I need access to includes $product->getColor, $product->getSize() (these are custom attributes, but just return NULL).

  • 1
    To see Magento object properties don't dump it, you can have memory issues because of circular references. Use the debug() method inherited from Varien_Object that will dump all the dynamic object properties. Example: var_dump($product->debug()) – Alessandro Ronchi Mar 25 '16 at 8:09
5
+25

What you see here is the encapsulation principle of object oriented programming. Properties are not meant to be accessed directly but with methods that should ensure that the object stays in a valid state and you cannot set any value to any property.

You did not say what exactly the properties are that you need but search for them in the model in app/code/core/Mage/Catalog/Model/Product.php to see if and how you can access them.

All product attributes (in the EAV sense) are in the array _data, and can be accessed with $product->getData($attributeCode) and $product->setData($attributeCode, $newValue)

There are some shortcuts for getData() and setData() that are implemented with the ArrayAccess interface and magic methods:

  • $product->sku
  • $product['sku']
  • $product->getSku()
  • $product->setSku($value)

This is why $product->sku worked. It translates to $product->getData('sku') and returns the value from the protected property _data['sku']

UPDATE: I'm now able to access data such as $product->getName() - but other data I need access to includes $product->getColor, $product->getSize() (these are custom attributes, but just return NULL).

I think I can see the problem now. getSku is returning the SKU of the configurable product, but it's the simple product of the configurable that I need (so that I can get color, size etc).

Then you are looking at the wrong items. For each configurable product, two quote items are created, a parent and a child item. The parent is the configurable product itself and the child is the selected simple product. getAllVisibleItems() returns only the parent items. It looks like what you want is only the child items. This can be done by fetching all items and exclude those that have children:

$orderItems = $order->getAllItems();
foreach ($orderItems as $item) {
    if ($item->getHasChildren()) {
        continue;
    }
    ...
}
  • I think I can see the problem now. getSku is returning the SKU of the configurable product, but it's the simple product of the configurable that I need (so that I can get color, size etc). – Liam McArthur Mar 21 '16 at 21:48
  • @LiamMcArthur I updated the answer – Fabian Schmengler Mar 22 '16 at 9:33
0

As a complement to @fschmengler answer, I reckon you could also do this:

$orderItems = $order->getAllItems();
foreach ($orderItems as $itemId => $item)
{
    if ($item->getParentItem()) {
        // This is a simple product assigned to a configurable
        // You can get every single details by loading it
        $product = Mage::getModel('catalog/product');
        $product->load($item->getSku(),'sku');
        // This variable will store all the data
        $data = $product->getData();
    } else if ($item->getHasChildren()) {
        // This is a configurable product
    }
}

Notice that I've used the following code:

$product->load($item->getSku(),'sku');

As the load method allows use to pass a second parameter which represents the attribute you want to use to load and as skus are unique you don't need to get the product ID from the sku and load from the product id.

  • I've tried var_dump($item->getParentItem()); and var_dump($item->getHasChildren()); within the foreach you've coded but they both return NULL. – Liam McArthur Mar 24 '16 at 17:51

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.