dpj96988 2016-12-23 13:54
浏览 23
已采纳

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

Hi for a Magento website i need to load a custom attribute in the shopping cart.

i use getmodel function to load the items. but i have no clue how i load the attribute. maybe i haven't configure the attribute right. i have enabled yes for enable on product listing. de attribute code is 'staffel_percentage'. it is just a regular string

also when i change the price per product it doesn't change the subtotal maybe this is because we already change the price for the product on the rest of the website?

maybe it is in the event? i use this event: controller_action_layout_render_before.

this is the code in observer i use for it

$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
                }
            }
  • 写回答

1条回答 默认 最新

  • dongqiaogouk86049 2016-12-23 17:09
    关注

    You need to load the product right after your first if statement

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

    Then on the next line after that you can add some logging statements that will appear in 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.
    

    If there is a value set for your product for your custom attribute then you can get it like this

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

    As for the prices changing, not sure how you have the prices set up. You need to set a positive or negative number to add or subtract from the price in the fields found in the parent product configuration page in Catalog > Products > Your product > Associated Products.

    See this image for what the section looks like.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况