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).
Best Answer
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()
andsetData()
that are implemented with the ArrayAccess interface and magic methods:This is why
$product->sku
worked. It translates to$product->getData('sku')
and returns the value from the protected property_data['sku']
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: