Magento购物车在Model / Quote / Item.php中获取自定义选项(文本字段)值

时间:2016-02-19 21:46:22

标签: magento magento-1.9

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

    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]);时,除了之外,我得到了所有内容,它甚至都不是数组。为了获得价值,我错过了什么?

1 个答案:

答案 0 :(得分: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
        }
    }

如果您有任何问题,我希望这有帮助并随时告诉我们!