Engine23

Display Bundle Product Weight

We had a store where we disabled user configuration of bundle products and set them up the way we wanted as defaults.  That made this much easier, but something similar could be done to update the weight via an ajax call as the configuration is changes.

 

Override the getAdditionalData function in Mage_Catalog_Block_Product_View_Attributes

 

About line 10 you will see where we look for when a bundle product's weight is being set and override it.  Once we catch the right piece of logic, we the weight of the products up and also catch the logic to do the proper formatting logic.  

public function getAdditionalData(array $excludeAttr = array())
    {
        $data = array();
        $product = $this->getProduct();
        $attributes = $product->getAttributes();
        foreach ($attributes as $attribute) {
//            if ($attribute->getIsVisibleOnFront() && $attribute->getIsUserDefined() && !in_array($attribute->getAttributeCode(), $excludeAttr)) {
            if ($attribute->getIsVisibleOnFront() && !in_array($attribute->getAttributeCode(), $excludeAttr)) {
                //if bundled product and setting it's weight value, insert out custom logic
                if( $product->getType_id() == 'bundle' &&  $attribute->Attribute_code == 'weight' )
                {
                    //get products that make up this bundle
                    $selectionCollection = $product->getTypeInstance(true)->getSelectionsCollection(
                        $product->getTypeInstance(true)->getOptionsIds($product), $product
                    );

                    //initialize variable we will add weight up in
                    $_total_weight = 0;

                    //go through each item in bundle
                    foreach($selectionCollection as $option)
                    {
                        //get a full load of the product
                        $option_product     = Mage::getModel('catalog/product')->load( $option->getData( 'entity_id' ) );

                        //if it is included by default and has a qty
                        if( $option->getData('is_default') && ( $option->getData('selection_qty') > 0 ) )
                        {
                            //add the total weight for all of these items ( qty * product_weight )
                            $_total_weight += $option->getData( 'selection_qty' ) * $option_product->getData( 'weight' );
                        }
                    }

                    //store the value in orginal magento location
                    $value = $_total_weight;
                }
                //else do everything as normal
                else
                {
                    $value = $attribute->getFrontend()->getValue($product);
                }

                //if using special way, we need to format it differently also, else it break things
                if( $attribute->getFrontendInput() == 'weight' && $product->getType_id() == 'bundle' )
                {
                    //format the weight here, this is the same as simple products
                    $value = @number_format( $value, 4 );
                }
                else if (!$product->hasData($attribute->getAttributeCode())) {
                    $value = Mage::helper('catalog')->__('N/A');
                } elseif ((string)$value == '') {
                    $value = Mage::helper('catalog')->__('No');
                } elseif ($attribute->getFrontendInput() == 'price' && is_string($value)) {
                    $value = Mage::app()->getStore()->convertPrice($value, true);
                }

                if (is_string($value) && strlen($value)) {
                    $data[$attribute->getAttributeCode()] = array(
                        'label' => $attribute->getStoreLabel(),
                        'value' => $value,
                        'code'  => $attribute->getAttributeCode()
                    );
                }
            }
        }
        return $data;
    }

Share: