Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Page 1 of 5
Display inventory graphically like on BustedTees [SOLVED]
 
sherrie
Moderator
 
Avatar
Total Posts:  1655
Joined:  2007-12-14
Waterloo, ON
 

Does anyone know how to pull inventory on configurable items to display inventory the way BustedTees.com does?

http://www.bustedtees.com/cougarhunter

 
Magento Community Magento Community
Magento Community
Magento Community
 
sherrie
Moderator
 
Avatar
Total Posts:  1655
Joined:  2007-12-14
Waterloo, ON
 

Awesome, thanks Dan!

I actually can’t take credit for the art itself - I’ve been building it from assets provided by a client - but the layout is pretty fun to develop as it really stretches the default magento templates.

Let me know if you find a way to get it to work!

 
Magento Community Magento Community
Magento Community
Magento Community
 
dan_w
Sr. Member
 
Total Posts:  290
Joined:  2008-01-04
 

Hi Sherrie,
That’s an interesting way to display inventory. I have to say I kind of like it. I think I may have a solution on how to do that. Let me play around with it for a couple of minutes and see if I can get it to work. By the way, I like the look of “glad rags”.

Dan

 
Magento Community Magento Community
Magento Community
Magento Community
 
dan_w
Sr. Member
 
Total Posts:  290
Joined:  2008-01-04
 

HI Sherrie,
I was playing around with the inventory qty’s last night and managed to get the inventory bar graph display to work for simple and grouped products, but not for configurable products. I’m looking into the Configurable.php model right now and will hopefully be able to pull the qty’s for the product collection called by the getUsedProducts function. Once this is done, displaying the graph is a piece of cake. I’ll let you know if I make any progress.

Dan

 
Magento Community Magento Community
Magento Community
Magento Community
 
sherrie
Moderator
 
Avatar
Total Posts:  1655
Joined:  2007-12-14
Waterloo, ON
 

Awesome!  You are seriously my hero of the day - maybe even month - if you figure this out!

 
Magento Community Magento Community
Magento Community
Magento Community
 
dan_w
Sr. Member
 
Total Posts:  290
Joined:  2008-01-04
 

Sherrie,
I got distracted for a bit at work, but I’ve got your problem definitely figured out. I just have a couple of questions to make sure that it works right for your situation. We don’t use any configurable products, so I just want to cover all of the bases with your clients operation. Are they using multiple attributes such as color, size, style or just size?  I’m not in the t-shirt biz myself, so I’m not sure what their plans are or how they layout their products. Let me know and I’ll post my findings.

Dan

 
Magento Community Magento Community
Magento Community
Magento Community
 
sherrie
Moderator
 
Avatar
Total Posts:  1655
Joined:  2007-12-14
Waterloo, ON
 

No problem. smile

At this point all that matters is size - I’m so excited that you figured it out - can’t wait to see it!

 
Magento Community Magento Community
Magento Community
Magento Community
 
dan_w
Sr. Member
 
Total Posts:  290
Joined:  2008-01-04
 

Ok,
That makes it pretty easy. Pretty much what I did was pull the inventory qty for the allowed products in the configurable product and have this set the width of the bar graph. You only need to add the following to the catalog->product->view->type->configurable.phtml file.

<?php
function clean_num($num){
  
return trim(trim($num'0'), '.');
}?>
<?php $_associatedProducts 
$this->getAllowProducts() ?>
    <?php 
if (count($_associatedProducts)): ?>
        <?php 
foreach ($_associatedProducts as $_item): ?>
        
<div class="graph_name"><?php echo $_item->getAttributeText('tshirt_size');?></div>
        <
div class="graph_bar" style="width:<?php echo clean_num(Mage::getModel('cataloginventory/stock_item')->loadByProduct($_item)->getQty()) ?>px"></div>
        <
div class="clear"></div>
          
<?php endforeach; ?>
    <?php 
endif; ?>

Since by default, Magento stores inventory in decimals in the db, the top function just cleans up the qty value so it can be used as a css width value. Moving down, $this->getAllowProducts() calls the products used to make the configurable product. Then there are two divs: one to call the attribute label and one that actually draws the bar graph. For this bit, $_item->getAttributeText(’tshirt_size’), you’ll need to replace ‘tshirt_size’ with the attribute code for your size attribute. I’m thinking that its probably just size. This will put the label in front of each bar, ie S,M, L or Small, Medium, Large however you have your attribute labels setup.

The next div controls the actual bar for the graph. In the boxes.css, I just created a class with bgcolor, height, float, border, etc..but no width. The width of the bar is called by the style tag set by the allowed product’s qty. Here’s where you’ll have to tweak for their application and your design. First, I didn’t know how wide of a container you were going to put the inv bar graph in and I didn’t know the stock levels that your client keeps. So if your container that’s going to hold the graph is 300px, but they have 600 t-shirts on hand, well then its going to skew your design. Or if your container is 600px and they keep 5 tshirts on hand, well, you may want to skew the proportion on the graph so it can actually be seen. To do that, you can add a multiplier or divider after the last closing parentheses in clean_num(Mage::getModel(’cataloginventory/stock_item’)->loadByProduct($_item)->getQty()) so it would look like, clean_num(Mage::getModel(’cataloginventory/stock_item’)->loadByProduct($_item)->getQty())*2 or
clean_num(Mage::getModel(’cataloginventory/stock_item’)->loadByProduct($_item)->getQty())/10.

You can certainly play around with that though for presentation purposes. Also if you need a unique setup per configurable product that can be done as well. I was just thinking that if they have one t-shirt that sells really well, in lrg, med and small sizes, they may have 1500 total - 500 of each size. But then they may have another that doesn’t sell as well, maybe 15 - 5 in each size, then your graph may look distorted across different products. If this is the case, we can build in some conditionals that will keep the ratio between products looking consistent.

Also, the generic css that I used to test it is:
.graph_bar{height:10px; border:1px solid #999999; margin-top:3px;background-color:#006666; float:left;}
.graph_name{font-size:11px;float:left; width:50px;}

Hope this helps and let me know if it works.
Dan

 
Magento Community Magento Community
Magento Community
Magento Community
 
sherrie
Moderator
 
Avatar
Total Posts:  1655
Joined:  2007-12-14
Waterloo, ON
 

FanTAStic!  Thank you SO much!  I’ve been playing around with it already and customizing it quite a bit.

I do have a couple more questions though - is it possible to write a logic statement into that says if the length is less than a certain # to change the color?  Also, this is what mine looks like now:

<div class="availability">
    <
div class="graph_container">
      
<?php
function clean_num($num){
  
return trim(trim($num'0'), '.');
}?>
      <?php $_associatedProducts 
$this->getAllowProducts() ?>
      <?php 
if (count($_associatedProducts)): ?>
      <?php 
foreach ($_associatedProducts as $_item): ?>
      
<div class="graph_name"><?php echo $_item->getAttributeText('size');?></div>
      <
div class="graph_bar_bg">
        
<?php if($_product->isSaleable()): ?>
        
<div class="graph_bar" style="width:<?php echo clean_num(Mage::getModel('cataloginventory/stock_item')->loadByProduct($_item)->getQty()) ?>px"></div>
        
<?php else: ?>
        
<p><?php echo $this->__('Out of stock. Request a Reprint'?></p>
        
<?php endif; ?>
      
</div>
    
<?php endforeach; ?>
    <?php 
endif; ?>
  
</div>
</
div>

However, the “Out of Stock. Request a Reprint” doesn’t display.  Any idea why?

 
Magento Community Magento Community
Magento Community
Magento Community
 
dan_w
Sr. Member
 
Total Posts:  290
Joined:  2008-01-04
 
sherrie - 06 June 2008 07:02 AM

is it possible to write a logic statement into that says if the length is less than a certain # to change the color?  Also, this is what mine looks like now:

<div class="availability">
    <
div class="graph_container">
      
<?php
function clean_num($num){
  
return trim(trim($num'0'), '.');
}?>
      <?php $_associatedProducts 
$this->getAllowProducts() ?>
      <?php 
if (count($_associatedProducts)): ?>
      <?php 
foreach ($_associatedProducts as $_item): ?>
      
<div class="graph_name"><?php echo $_item->getAttributeText('size');?></div>
      <
div class="graph_bar_bg">
        
<?php if($_product->isSaleable()): ?>
        
<div class="graph_bar" style="width:<?php echo clean_num(Mage::getModel('cataloginventory/stock_item')->loadByProduct($_item)->getQty()) ?>px"></div>
        
<?php else: ?>
        
<p><?php echo $this->__('Out of stock. Request a Reprint'?></p>
        
<?php endif; ?>
      
</div>
    
<?php endforeach; ?>
    <?php 
endif; ?>
  
</div>
</
div>

Yep..when defining the style for the bar, after the width, you could add something like:

background-color:
        
<?php if(clean_num(Mage::getModel('cataloginventory/stock_item')->loadByProduct($_item)->getQty())<[USE YOUR MINIMUM VALUE]) echo ('#[YOUR COLOR CODE]');?>;"
This should override the background color in the css for any qty below what you set and change the color.

However, the “Out of Stock. Request a Reprint” doesn’t display.  Any idea why?

I’m not sure off the top of my head...I’ll take a look into it.

 
Magento Community Magento Community
Magento Community
Magento Community
 
dan_w
Sr. Member
 
Total Posts:  290
Joined:  2008-01-04
 

I just noticed why your out of stock condition doesn’t work. $_product refers to getProduct which is the configurable product itself. Also, as I remember correctly, getAllowProducts is set to only pull products that are Saleable, which is why if a size goes out of stock it doesn’t show up on the drop down either. I just took a look at the Configurable.php Block and it does have an

if ($product->isSaleable())
for the getAllowProducts function. If I removed it, then you can use your Saleable logic by using <?php if($_item->isSaleable()): ?>. $_item refers to our associated products rather than the main configurable products. The down side to this is:
1. You should probably overwrite the class from a local code pool to make sure its stable across upgrades.
2. The out of stock item still shows up in the attribute select drop down. They can select the item, but it won’t let them add it to the cart. Lets say medium is out of stock and they try to select a medium and hit add to cart, they will just get a warning that re-iterates, “yes, the tshirt is really out of stock. look at the graph.”

Otherwise, as always, especially with Magento, everything is doable.

 
Magento Community Magento Community
Magento Community
Magento Community
 
sherrie
Moderator
 
Avatar
Total Posts:  1655
Joined:  2007-12-14
Waterloo, ON
 

That makes sense, it will still be upgradeable as I can put that as a local copy - but when I remove that line from Configurable.php I get this:

Parse errorsyntax errorunexpected T_RETURNexpecting T_FUNCTION in 
app
/code/local/Mage/Catalog/Block/Product/View/Type/Configurable.php on line 62
 
Magento Community Magento Community
Magento Community
Magento Community
 
dan_w
Sr. Member
 
Total Posts:  290
Joined:  2008-01-04
 

Easy fix...make sure you remove the closing bracket for the isSaleable clause. This is how my function looks:

public function getAllowProducts()
    
{
        
if (!$this->hasAllowProducts()) {
            $products 
= array();
            
$allProducts $this->getProduct()->getTypeInstance()->getUsedProducts();
            foreach (
$allProducts as $product{
            
//    if ($product->isSaleable()) {
                    
$products[] $product;
                
//}
            
}
            $this
->setAllowProducts($products);
        
}
        
return $this->getData('allow_products');
    
}
 
Magento Community Magento Community
Magento Community
Magento Community
 
sherrie
Moderator
 
Avatar
Total Posts:  1655
Joined:  2007-12-14
Waterloo, ON
 

I feel dumb - I only removed the opening bracket.  Thanks Dan - you’ve been a HUGE help!

 
Magento Community Magento Community
Magento Community
Magento Community
 
dan_w
Sr. Member
 
Total Posts:  290
Joined:  2008-01-04
 

No problem...the only reason I recognized the error right away is because I made the same mistake when I tried to remove the clause.

 
Magento Community Magento Community
Magento Community
Magento Community
 
harry12bar
Sr. Member
 
Avatar
Total Posts:  168
Joined:  2008-01-07
 

Hi, can this be modified to show for simple products? a basic status bar next to STOCK LEVEL: ? Luv the way you patiently explained what you script does, oh by the way hi to everyone else.
Cheers.
Alex

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top
Page 1 of 5