cancel
Showing results for 
Search instead for 
Did you mean: 

Add Product Quantity in Magento Order Item in Admin Panel.

SOLVED

Add Product Quantity in Magento Order Item in Admin Panel.

 Hello there,

I want to show product quantity in magento order items in admin panel. I'm not talking about quantity ordered, but product quantity itself.

Is there a way to check product qty in sales order item in Magento admin panel? 

Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Add Product Quantity in Magento Order Item in Admin Panel.

Surprisingly, I've found the solution!

Here is the code to get product quantity in admin panel sales order item in name.phtml file:

<?php
$_sku = $_item->getSku();
$_product = Mage::getModel('catalog/product')->loadByAttribute('sku', $_sku);
$stock_count = (int) Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty();
echo $stock_count;
?>

Regards

 

 

View solution in original post

6 REPLIES 6

Re: Add Product Quantity in Magento Order Item in Admin Panel.

Hi @Armita,

 

Can you share a screenshot of the result you want and the difference between the original feature?

Re: Add Product Quantity in Magento Order Item in Admin Panel.

Thanks Damian for reply. 

 

product quantity.png

I want to get product quantity marked in the above picture, and show it in the below one:

 

order_items.png

Precisely, In the second picture (sales order items) as it's marked with rectangle, I want to check the product quantity (mentioned in the first picture) and if it's less than or equal to zero, show estimated dispatch value here.

 

Regards

 

Re: Add Product Quantity in Magento Order Item in Admin Panel.

Hi  @Armita,

 

I think there are several details here.

First of all, you will allow backorders?

Then, if you will allow it (I guess is the path to get the feature you want) you will need to add some logic where the order is placed to validate the remaining stock and estimate when the prdocut will be delivered.

So here you'll have, at least, 2 validations: current stock and delivery time (I guess you'll have your own specific rule or way to estimate the date).

Only after all those steps we can move forward on how to show that info in that line.

 

Am I in the right direction here?

Re: Add Product Quantity in Magento Order Item in Admin Panel.

Thanks Damian for information you provide. Let make this situation simple! The only thing I need is to get product stock quantity in admin panel sales order item. I mean under  product name and sku I can get product total quantity too.

Actually, I want to get product quantity in the file name.phtml located on app/design/adminhtml/default/default/template/sales/items/column.

Below is the code for this file: 

 

<?php if ($_item = $this->getItem()): ?>
    <h5 class="title"><span id="order_item_<?php echo $_item->getId() ?>_title"><?php echo $this->escapeHtml($_item->getName()) ?></span></h5>
    <div><strong><?php echo $this->helper('sales')->__('SKU') ?>:</strong> <?php echo implode('<br />', Mage::helper('catalog')->splitSku($this->escapeHtml($this->getSku()))); ?></div>
    <?php if ($this->getOrderOptions()): ?>
        <dl class="item-options">
        <?php foreach ($this->getOrderOptions() as $_option): ?>
            <dt><?php echo $_option['label'] ?></dt>
            <dd>
            <?php if (isset($_option['custom_view']) && $_option['custom_view']): ?>
                <?php echo $this->getCustomizedOptionValue($_option); ?>
            <?php else: ?>
                <?php $_option = $this->getFormattedOption($_option['value']); ?>
                <?php echo $_option['value']; ?><?php if (isset($_option['remainder']) && $_option['remainder']): ?><span id="<?php echo $_dots = 'dots' . uniqid()?>"> ...</span><span id="<?php echo $_id = 'id' . uniqid()?>"><?php echo $_option['remainder'] ?></span>
                    <script type="text/javascript">
                    $('<?php echo $_id ?>').hide();
                    $('<?php echo $_id ?>').up().observe('mouseover', function(){$('<?php echo $_id ?>').show();});
                    $('<?php echo $_id ?>').up().observe('mouseover', function(){$('<?php echo $_dots?>').hide();});
                    $('<?php echo $_id ?>').up().observe('mouseout',  function(){$('<?php echo $_id ?>').hide();});
                    $('<?php echo $_id ?>').up().observe('mouseout',  function(){$('<?php echo $_dots ?>').show();});
                    </script>
                <?php endif; ?>
            <?php endif; ?>
            </dd>
        <?php endforeach; ?>
        </dl>
    <?php endif; ?>
    <?php echo $this->escapeHtml($_item->getDescription()) ?>
<?php endif; ?>

 As you can see, product name, sku, product options and so on are fetched there. I want to get product quantity in this file (something like $this -> getQty() for example). That's it. 

 

Thank you and Regards

Re: Add Product Quantity in Magento Order Item in Admin Panel.

Surprisingly, I've found the solution!

Here is the code to get product quantity in admin panel sales order item in name.phtml file:

<?php
$_sku = $_item->getSku();
$_product = Mage::getModel('catalog/product')->loadByAttribute('sku', $_sku);
$stock_count = (int) Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty();
echo $stock_count;
?>

Regards

 

 

Re: Add Product Quantity in Magento Order Item in Admin Panel.

This solution could be cleaner. Firstly, try to utilise the methods available to you already.

Mage_Sales_Model_Item has the method getProduct() which does pretty much what you've done for the $_product expression. However getProduct() will define the _product class param when it is first called. Any subsequent requests to `getProduct will return the already loaded product.

 

It's bad practice to load a model in a template file. Loading a model here will fire all the events around that model even when they're not required in this instance. It's best practice to create an observer to join the required additional information.

 

In an appropriate module I'd add the following to the config.xml

 

<adminhtml>
    <events>
        <sales_order_item_collection_load_after>
            <observers>
                <add_stock_to_order_items>
                    <class>module/observer</class>
                    <method>addStockItemToOrderProducts</method>
                </add_stock_to_order_items>
            </observers>
        </sales_order_item_collection_load_after>
    </events>
</adminhtml>


Then in Observer.php

 

public function addStockItemToOrderProducts(Varien_Event_Observer $observer)
{
/** @var $orderItems \Mage_Sales_Model_Resource_Order_Item_Collection */
$orderItems = $observer->getOrderItemCollection();


foreach ($orderItems as $orderItem) {
$orderItem->getProduct()->getStockItem();
}

return $this;
}

 

Then in your template you can simply call:

 

    <?php if ($stock = $_item->getProduct()->getStockItem()): ?>
        <?php echo $stock->getQty() ?>
    <?php endif; ?>