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 2
Calculate Extra Parameters for a given product (which affect the product price)
 
winans_fred
Sr. Member
 
Total Posts:  83
Joined:  2007-09-06
 

Hi Moshe/all,

I am trying to add a product to the cart which includes a variable quantity of options called “additional business categories”.  These options increase the price of the final product by $100 per category, so I am wondering how I could make this work.

For example, let’s say my base product add URL is

http://www.mystore.com/store/checkout/cart/add/product/138/

and my customer selects “x” amount of additional business categories (remember, they are options for that product only) that should be added to this product, and then my product price will be calculated accordingly.  Is this possible to do?

Thanks!!

Fred

 
Magento Community Magento Community
Magento Community
Magento Community
 
Moshe
Magento Team
 
Avatar
Total Posts:  1770
Joined:  2007-08-07
Los Angeles
 

In my example I will assume that you will receive the business categories number in URL when adding an item to the cart, and name of the attribute is ‘biz_cat_num’.

1. create attribute for business category quantity in quote item

select @entity_type_id:=entity_type_id from eav_entity_type 
where entity_type_code
='quote_item';

insert into eav_attribute 
    
(entity_type_idattribute_codebackend_typefrontend_input)
values @entity_type_id'biz_cat_num''int''text';

2. overload app/code/core/Mage/Checkout/controllers/CartController.php::addAction:

// add this
$product->setBizCatNum($this->getRequest()->getParam('biz_cat_num'));
// right before
$cart->addProduct($product$qty)
//...

3. overload app/code/core/Mage/Sales/Model/Quote/Item.php so that calculated row total will include biz_cat_num:

public function calcRowTotal()
{
    $bizCatPrice 
100// or configurable, for example: 
    // $bizCatPrice = Mage::getStoreConfig('fred/bizcat/price')

    
$rowTotal $this->getPrice()*$this->getQty();

    
$rowTotal += $bizCatPrice*$this->getBizCatNum();

    
$this->setRowTotal($rowTotal);

    return 
$this;
}
 
Magento Community Magento Community
Magento Community
Magento Community
 
winans_fred
Sr. Member
 
Total Posts:  83
Joined:  2007-09-06
 

Hi Moshe,

Just one question - is there a function defined as setBizCatNum?

Thanks!

Fred

 
Magento Community Magento Community
Magento Community
Magento Community
 
Moshe
Magento Team
 
Avatar
Total Posts:  1770
Joined:  2007-08-07
Los Angeles
 

All the models extend Varien_Object which provide functionality of dynamic getters and setters by automagic __call method of PHP5.

So, when you call

$this->setBizCatNum($value);
effectively it equivalents to
$this->_data['biz_cat_num'$value;
if the method setBizCatNum() is not defined.

Same with getters:

$value $this->getBizCatNum();
// equivalents to
$value $this->_data['biz_cat_num'];
where capital letters in method name are replaced with underscores in key name.

There is also

// same as $flag = isset($this->_data['biz_cat_num']);
$flag $this->hasBizCatNum();

// same as unset($this->_data['biz_cat_num']);
$this->unsBizCatNum();

You could also use more conventional

$this->setData('biz_cat_num'$value);
But then you would loose capability of custom logic on this action.
 
Magento Community Magento Community
Magento Community
Magento Community
 
winans_fred
Sr. Member
 
Total Posts:  83
Joined:  2007-09-06
 

That is what I was hoping to hear.  Yay PHP5 Magic Methods!  Thanks, Moshe!

 
Magento Community Magento Community
Magento Community
Magento Community
 
winans_fred
Sr. Member
 
Total Posts:  83
Joined:  2007-09-06
 

Hi Moshe

I did all the changes as indicated, but the URL doesn’t seem to reflect our changes:

http://myonline.jagflyhosting.com/store/checkout/cart/add/product/141/biz_cat_num/2

should be the product price ($350) plus $200 (2 business categories).  Yes, I’ve checked that the attribute is names correctly in the DB, and the entity_type_id = 15, which is the id corresponding to the quote_item entity type.  Help!

 
Magento Community Magento Community
Magento Community
Magento Community
 
winans_fred
Sr. Member
 
Total Posts:  83
Joined:  2007-09-06
 

Would anyone be able to help with the above request?  Thanks in advance!

 
Magento Community Magento Community
Magento Community
Magento Community
 
Moshe
Magento Team
 
Avatar
Total Posts:  1770
Joined:  2007-08-07
Los Angeles
 

Do you see a value in table sales_quote_entity_int for biz_cat_num attribute_id ?

 
Magento Community Magento Community
Magento Community
Magento Community
 
winans_fred
Sr. Member
 
Total Posts:  83
Joined:  2007-09-06
 

Nothing there for attribute_id 537 (biz_cat_num).

 
Magento Community Magento Community
Magento Community
Magento Community
 
Moshe
Magento Team
 
Avatar
Total Posts:  1770
Joined:  2007-08-07
Los Angeles
 

Could you paste your customizations?

 
Magento Community Magento Community
Magento Community
Magento Community
 
winans_fred
Sr. Member
 
Total Posts:  83
Joined:  2007-09-06
 

first, in app/code/core/Mage/Sales/Model/Quote/Item.php:

public function calcRowTotal()    //Overloaded
    
{
        $bizCatPrice 
100// or configurable, for example: 
        // $bizCatPrice = Mage::getStoreConfig('fred/bizcat/price')
    
        
$rowTotal $this->getPrice()*$this->getQty();
    
        
$rowTotal += $bizCatPrice*$this->getBizCatNum();
        
        
$this->setRowTotal($rowTotal);
        
//$this->setRowTotal($this->getPrice()*$this->getQty());
        
return $this;
    
}

second, in app/code/core/Mage/Checkout/controllers/CartController.php (function addAction):

$cart $this->_getCart();
        try 
{
            $product 
Mage::getModel('catalog/product')
                ->
load($productId)
                ->
setConfiguredAttributes($this->getRequest()->getParam('super_attribute'))
                ->
setGroupedProducts($this->getRequest()->getParam('super_group', array()))
                ->
setBizCatNum($this->getRequest()->getParam('biz_cat_num'));    //Custom Tweak for ACCGS  Added by Fred 11/26/2007
            
            
$cart->addProduct($product$qty)
                ->
addProductsByIds($additionalIds)
                ->
save();
            
$this->_backToCart();
        
}
        
catch (Exception $e{...

--- table "eav_attribute" ---
attribute_id:  537 
entity_type_id
15 
attribute_code
biz_cat_num 
attribute_model
NULL 
backend_model
NULL 
backend_type
int 
backend_table
NULL 
frontend_model
NULL 
frontend_input
text 
frontend_label
Additional Business Categories 
frontend_class
validate-digits 
source_model
NULL 
is_global

is_visible

is_required

is_user_defined

default_value

is_searchable

is_filterable

is_comparable

is_visible_on_front

is_unique

apply_to

use_in_super_product


HTH
!
 
Magento Community Magento Community
Magento Community
Magento Community
 
winans_fred
Sr. Member
 
Total Posts:  83
Joined:  2007-09-06
 

Bump.  Help still required, thank you!

 
Magento Community Magento Community
Magento Community
Magento Community
 
Moshe
Magento Team
 
Avatar
Total Posts:  1770
Joined:  2007-08-07
Los Angeles
 

Ok, there’s another place - look in Mage_Sales_Model_Quote_Item_Abstract::importCatalogProduct

 
Magento Community Magento Community
Magento Community
Magento Community
 
winans_fred
Sr. Member
 
Total Posts:  83
Joined:  2007-09-06
 

there is nothing in there that factors in the biz_cat_num parameter.  Should I put something there?

Thanks!

 
Magento Community Magento Community
Magento Community
Magento Community
 
Mark_Kimsal
Sr. Member
 
Total Posts:  186
Joined:  2007-09-12
Michigan, USA
 

Fred, you have to edit one function in Sales/Model/Quote/Item/Abstract.php

I have two variables i’m keeping track of, so my importCatalogProduct() looks like this:

$this->setProductId($product->getId())
            ->
setSku($product->getSku())
            ->
setName($product->getName())
            ->
setWeight($product->getWeight())
            ->
setTaxClassId($product->getTaxClassId())
            ->
setCost($product->getCost());
        
$this->setLineitemdetails($product->getLineitemdetails());
        
$this->setConfigid($product->getConfigid());

@moshe
This doesn’t seem to save my “text” type variables, only varchar.
I ran this for each variable

insert into eav_attribute 
    
(entity_type_idattribute_codebackend_typefrontend_input)
values (15'lineitemdetails''text''text');

There is a record in “sales_quote_entity_text” with the correct attribute_id, but the value column is empty.  The set function call works, I can see the correct value inside “_data” of the quote item, I can’t find where the quote item is saved to sales_quote_entity_*.

One last question, what’s the difference between sales_quote_entities and sales_order_entities?  Are sales quotes like a shopping cart or a list of line items and a sales order is the information about shipping/payments?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Mark_Kimsal
Sr. Member
 
Total Posts:  186
Joined:  2007-09-12
Michigan, USA
 

I figured it out, sort of.  If my TEXT data has commas in it, it won’t save to the DB.

“GU, UD” does not save, but
“GUUD” does… still working on this.

EDIT
Nope, that isn’t even right.  Sometimes it works and sometimes it doesn’t.  I have all my caching off.  Now it’s mostly on the not working side.  I got it to work 1 time.

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