0

I made a module to calculate the price, based on given dimensions. This is working very nice, but when I login as user, the cart price resets to zero.

How do I solve this?

/// edit #1

After some digging, I found out that the vars: $quantity and $dimensionsPrice not working on quote merging. Do you know why? $dimensionPrice is from the custom attribute dimension_price.

/// edit #2

Turns out that when I want to add a product to the cart I have to use $product->getQty(). And when I login (so it merges), I have to use $quote_item->getQty(). Bit strange, but I fixed it with:

  $quantity = ($product->getQty()) ? $product->getQty() : $quote_item->getQty();

Now, I still not getting my attribute 'dimension price'...?

mymodule/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
      <Roywulms_Dimensioncalculator>
          <version>0.1.0</version>
      </Roywulms_Dimensioncalculator>
    </modules>
    <global>
    <models>
        <Roywulms_Dimensioncalculator>
            <class>Roywulms_Dimensioncalculator_Model</class>
        </Roywulms_Dimensioncalculator>
    </models>
     <events>
        <sales_quote_add_item>
            <observers>
               <Roywulms_Dimensioncalculator_Model_Observer>
                  <type>singleton</type>
                  <class>Roywulms_Dimensioncalculator_Model_Observer</class>
                  <method>changingPrice</method>
               </Roywulms_Dimensioncalculator_Model_Observer>
           </observers>
        </sales_quote_add_item>
    </events>
    </global>
</config>

mymodule/Model/Observer.php

<?php
class Roywulms_Dimensioncalculator_Model_Observer {
    public function __construct()
   {

   }
    public function changingPrice(Varien_Event_Observer $observer){

        $event = $observer->getEvent();
        $quote_item = $event->getQuoteItem();
        $product = $quote_item->getProduct();
        $quantity = $product->getQty();
        $options = $this->getProductOptions($quote_item);

        if(sizeof($options) > 0)
        {
            foreach($options as $option)
            {
                if($option['option_type'] == 'height')
                    $heightVal =  $option['value'];
                if($option['option_type'] == 'width')
                    $widthVal =  $option['value'];
            }
            $basePrice = $product->getPrice();
            $dimensionsPrice =  $product->getDimensionPrice();

            if(isset($heightVal) && isset($widthVal)){
              $meter = $basePrice + ($widthVal * $heightVal)*$dimensionsPrice;
              $new_price = $meter*$quantity;
              $quote_item->setOriginalCustomPrice($new_price);
            }
        }
     }

      function getProductOptions(&$item)
        {
          $options = array();

          if ($optionIds = $item->getOptionByCode('option_ids')) {
              $options = array();
              foreach (explode(',', $optionIds->getValue()) as $optionId) {
                  if ($option = $item->getProduct()->getOptionById($optionId)) {

                      $quoteItemOption = $item->getOptionByCode('option_' . $option->getId());

                      $group = $option->groupFactory($option->getType())
                          ->setOption($option)
                          ->setQuoteItemOption($quoteItemOption);

                      $options[] = array(
                          'label' => $option->getTitle(),
                          'value' => $group->getFormattedOptionValue($quoteItemOption->getValue()),
                          'print_value' => $group->getPrintableOptionValue($quoteItemOption->getValue()),
                          'price'=> $group->getOptionPrice($quoteItemOption->getValue(),0),
                          'option_id' => $option->getId(),
                          'option_type' => $option->getType(),
                          'custom_view' => $group->isCustomizedView()
                      );
                  }
              }
          }
          if ($addOptions = $item->getOptionByCode('additional_options')) {
              $options = array_merge($options, unserialize($addOptions->getValue()));
          }
          return $options;
        }
}
?>
1

Found the solution. Not the best I think, so if you know a better approach, please tell me.

Turns out I had two problems:

1: Quantity was not loaded on quote merge (happens when a user is logging in)

Replaced

 $quantity = $product->getQty();

with

$quantity = ($product->getQty()) ? $product->getQty() : $quote_item->getQty();

2: Attribute was not loaded on quote merge.

Replaced

$dimensionsPrice =  $product->getDimensionPrice();

with

$dimensionsPrice =  Mage::getModel('catalog/product')->load($product->getId())->getData('dimension_price');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.