2016-02-19 57 views
2

我的产品具有影响重量的可变尺寸,我需要将重量逻辑放入购物车以更准确地计算运费。我已将app/code/core/Mage/Sales/Model/Quote/Item.php的副本移至本地代码池。我已经能够获取相关产品的基本重量,但是,在更新购物车之前,我无法获得自定义选项(文本字段)的值来进行数学计算。我在公共职能setProduct中这样做。这是我到目前为止有:Magento购物车在型号/报价/ Item.php中获取自定义选项(文本字段)值

public function setProduct($product) 
    { 
     if ($this->getQuote()) { 
      $product->setStoreId($this->getQuote()->getStoreId()); 
      $product->setCustomerGroupId($this->getQuote()->getCustomerGroupId()); 
     } 

      //Get the Weight per UOM 
       $sku = $product->getSku(); 
       $item = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku); 
       $wpuom = $item->getResource()->getAttribute('weight_per_uom')->getFrontend()->getValue($item); 

    //Get the Length 
     $params = Mage::app()->getRequest()->getParams(); 
     /** @var Mage_Catalog_Model_Product $product */ 
     $info = new Varien_Object($params); 

     // Don't throw an exception if required options are missing 
     $processMode = Mage_Catalog_Model_Product_Type_Abstract::PROCESS_MODE_LITE; 

     $options = array(); 
     foreach ($product->getOptions() as $option) { 
      /* @var $option Mage_Catalog_Model_Product_Option */ 
      $group = $option->groupFactory($option->getType()) 
       ->setOption($option) 
       ->setProduct($product) 
       ->setRequest($info) 
       ->setProcessMode($processMode) 
       ->validateUserValue($info->getOptions()); 

      $optionValue = $info->getData('options/' . $option->getId()); 
      $options[] = array(
       'label' => $option->getTitle(), 
       'value' => $group->getFormattedOptionValue($optionValue), 
       'value2' => $option->getValues(), 
       'option_id' => $option->getId(), 
       'option_type' => $option->getType() 
      ); 

//<<<This is Where I Cannot Get the Value for the Custom Option>>> 

      if($options[0]['label'] == 'Length') { 
       //print_r($options[0]['value']); 
      } 
     } 

      //Update Weight 
       $baseWeight = $item->getWeight(); 
       $uom = $item->getResource()->getAttribute('uom')->getFrontend()->getValue($item); 

       if((($uom == 'Meters') && ($length >= 76)) || (($uom == 'Feet') && ($length >= 250))) { $spoolWeight = 3; } 
       else { $spoolWeight = 0; } 

       $finalWeight = ($baseWeight + ($length * $wpuom) + $spoolWeight); 


     $this->setData('product', $product) 
      ->setProductId($product->getId()) 
      ->setProductType($product->getTypeId()) 
      ->setSku($this->getProduct()->getSku()) 
      ->setName($product->getName()) 
      ->setWeight($this->getProduct()->getWeight()) 
      ->setTaxClassId($product->getTaxClassId()) 
      ->setBaseCost($product->getCost()) 
      ->setIsRecurring($product->getIsRecurring()); 

     if ($product->getStockItem()) { 
      $this->setIsQtyDecimal($product->getStockItem()->getIsQtyDecimal()); 
     } 

     Mage::dispatchEvent('sales_quote_item_set_product', array(
      'product' => $product, 
      'quote_item' => $this 
     )); 
     return $this; 
    } 

,当我尝试添加print_r($options[0]);,我得到的一切,除了,它甚至不是一个数组。我为了获得价值而错过了什么?

+0

一些附注:产品是可配置的,版本是Magento 1.9.1 CE – NotJay

回答

1

如果您在网站上的各个位置显示产品价格,例如类别页,愿望清单和网站上的其他区域,您可能会遇到希望清单和其他功能的问题。

这就是说,我对代码进行了一些修改,并在下面进行了更新。我采取了一种稍微不同的方法来获得最终结果。首先,获取当前会话的所有购物车物品。如果产品类型是可配置的,则执行一些逻辑,如果不是,则执行其他逻辑。在逻辑内部,我正在收集项目,关联的属性值,自定义选项和自定义选项值。我还添加了一些功能来将长度和度量单位添加到sku。然后,我继续展示了如何使用直接mySQL查询来更新数据库的示例,包括新的定价,skus,权重和其他详细信息。

public function setProduct($product) 
{ 
    if ($this->getQuote()) { 
     $product->setStoreId($this->getQuote()->getStoreId()); 
     $product->setCustomerGroupId($this->getQuote()->getCustomerGroupId()); 
    } 

    $quote = Mage::getSingleton('checkout/session')->getQuote(); 
    foreach ($quote->getAllVisibleItems() as $itm){ 
     $sku = $product->getSku(); 
     $item = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku); 
     $assocProd = Mage::getModel('catalog/product')->loadByAttribute('sku',$itm->getSku()); 

     if($product->getTypeId() == 'configurable') { //configurable product 
      foreach ($product->getOptions() as $o) { 
       if($o->getTitle() == "YOUR CUSTOM OPTION LABEL" && $o->getType() == "field"){ 
        $optionId = $o->getOptionId(); 
        $opz = $itm->getOptionByCode('info_buyRequest'); 
        $buyRequest = new Varien_Object($opz ? unserialize($opz->getValue()) : null); 
        foreach($buyRequest['options'] as $key => $value){ 
         if($key == $optionId) { 
          $length = $value; 
          //here is your length... do with it what you please 
          } 

          //example of how to update the database 
          $itemId = $itm->getId(); 

          $query = 'UPDATE `sales_flat_quote_item` SET `weight` = "'.$finalWeight.'", `display_sku` = "'.$displaySku.'" WHERE `item_id` = "'.$itemId.'"'; 
          $resource = Mage::getSingleton('core/resource'); 
          $writeConnection = $resource->getConnection('core_write'); 
          $writeConnection->query($query); 
         } 
        } 
       } 
      } 
     } 
     else { //simple product 
      if($product->getOptions()) { //has options 
       foreach ($product->getOptions() as $o) { 
        if($o->getTitle() == "YOUR OPTION LABEL" && $o->getType() == "field"){ 
         $optionId = $o->getOptionId(); 
         $opz = $itm->getOptionByCode('info_buyRequest'); 
         $buyRequest = new Varien_Object($opz ? unserialize($opz->getValue()) : null); 
         foreach($buyRequest['options'] as $key => $value){ 
          $finalWeight = ''; 
          if($key == $optionId) { 
           $length = $value; 
           //here's your length value for simple products 
           } 

           //to update the database 
           $itemId = $itm->getId(); 

           $query = 'UPDATE `sales_flat_quote_item` SET `weight` = "'.$finalWeight.'", `display_sku` = "'.$displaySku.'" WHERE `item_id` = "'.$itemId.'"'; 
           $resource = Mage::getSingleton('core/resource'); 
           $writeConnection = $resource->getConnection('core_write'); 
           $writeConnection->query($query); 
          } 
         } 
        } 
       } 
      } 
      else { //doesn't have options 
       //this would be your logic for a simple product with no options 
     } 
    } 

我希望这有帮助,并随时让我知道,如果您有任何问题!

+1

完美。感谢您提供查询以更新数据库......您为我节省了另一步! – NotJay

相关问题