[英]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.