繁体   English   中英

在购物车Magento中加载自定义属性

[英]Load a custom attribute in the cart Magento

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

我使用getmodel函数加载项目。 但是我不知道如何加载属性。 也许我没有正确配置属性。 我已为产品列表启用启用。 属性代码为“ 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
                }
            }

您需要在第一个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');

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

请参阅此图像以了解本节的外观。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM