【问题标题】:Load a custom attribute in the cart Magento在购物车 Magento 中加载自定义属性
【发布时间】:2016-12-23 13:54:52
【问题描述】:

您好,对于 Magento 网站,我需要在购物车中加载自定义属性。

我使用 getmodel 函数来加载项目。但我不知道如何加载属性。也许我没有正确配置属性。我已经为产品列表启用了是。 de 属性代码是“staffel_percentage”。它只是一个普通的字符串

另外,当我更改每个产品的价格时,它不会更改小计,这可能是因为我们已经更改了网站其余部分的产品价格?

也许它在事件中?我使用这个事件:controller_action_layout_render_before。

这是我在观察者中使用的代码

$cart = Mage::getModel('checkout/cart')->getQuote();                            // Gets the collection of checkout 
        foreach ($cart->getAllItems() as $item) {                                               // adds all items to $item and does the following code for each item

            if ($item->getParentItem()) {                                                       // If items is part of a parent
            $item = $item->getParentItem();                                                     // Get parentItem;
            }
            //echo 'ID: '.$item->getProductId().'<br />';

            $percentage = 80;// init first percentage

            $quantity = $item->getQty(); //quantity

            //$staffelstring = loadattribute//loads attribute

            //gives the right percentage per quantity 
            //$staffelarray = explode(';', ^);
            //foreach($staffelarray as $staffels){
                //$stafel = explode(':', $staffels);
                //if($quantity >= $stafel[0]){
                    //$percentage = $Stafel[1];
                //}
            //}


            $currency = Mage::app()->getStore()->getCurrentCurrencyRate();                      // Currencyrate that is used currently
            $specialPrice = (($this->CalculatePrice($item) * $currency)* $percentage) / 100;                            //New prices we want to give the products

            if ($specialPrice > 0) {                                                            // Check if price isnt lower then 0
                $item->setCustomPrice($specialPrice);                                           //Custom Price will have our new price
                $item->setOriginalCustomPrice($specialPrice);                                   //Original Custom price will have our new price, if there was a custom price before
                $item->getProduct()->setIsSuperMode(true);                                      //set custom prices against the quote item
                }
            }

【问题讨论】:

    标签: php magento


    【解决方案1】:

    您需要在第一个 if 语句之后立即加载产品

    $product = Mage::getModel('catalog/product')->load($item->getId()); // may be $item->getProductId() instead here
    

    然后在下一行,您可以添加一些将出现在 var/log/system.log 中的日志记录语句

    Mage::log($product->getData()); // this will output all of the product data and attributes. You will see your custom attribute value here if it is set on this product.
    

    如果您的产品为您的自定义属性设置了值,那么您可以像这样获取它

    $value = $product->getData('staffel_percentage');
    

    至于价格变化,不确定您是如何设置价格的。您需要设置一个正数或负数以在目录 > 产品 > 您的产品 > 关联产品的父产品配置页面中的字段中添加或减去价格。

    请参阅this image 了解该部分的外观。

    【讨论】:

      猜你喜欢
      • 2013-05-10
      • 2011-07-09
      • 2012-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-22
      相关资源
      最近更新 更多