2014-03-06 151 views
1

我想要弄清楚如何讓Bundle頁面中的「Price as Configured」部分以定價更新。Magento:捆綁產品中「配置價格」的bundle.js中的Tier Pricing'Bug'

現在,如果我在捆綁產品中有複選框選擇,並且單擊複選框將其添加到捆綁包中,則「配置價格」會更新爲產品的完整價格,而不是層級定價該選擇的默認數量在層級定價領域內)。

現在,我經歷的/skin/frontend/base/default/js/bundle.js文件,並根據selectionPrice方法我看到以下內容:

selectionPrice: function(optionId, selectionId) { 

................. 

    if (this.config.priceType == '0') { 
     price = this.config.options[optionId].selections[selectionId].price; 
     tierPrice = this.config.options[optionId].selections[selectionId].tierPrice; 

     for (var i=0; i < tierPrice.length; i++) { 
      if (Number(tierPrice[i].price_qty) <= qty && Number(tierPrice[i].price) <= price) { 
       price = tierPrice[i].price; 
      } 
     } 
    } else { 
     selection = this.config.options[optionId].selections[selectionId]; 
     if (selection.priceType == '0') { 
      price = selection.priceValue; 
     } else { 
      price = (this.config.basePrice*selection.priceValue)/100; 
     } 
    } 

到目前爲止,我可以告訴大家,它到達設置:

price = this.config.options[optionId].selections[selectionId].price 

現在通常如果tierPrice返回一個對象,它應該能夠來遍歷它,找到一線的價格(至少是這樣的代碼似乎有什麼要該做的)。但是,它永遠不會進入實際設置分級價格的for循環。在控制檯中,我可以做直接訪問層價格:

bundle.config.options['301'].selections['1066'].tierPrice['32000-5'] 

然而,Magento的代碼依賴於能夠隨叫隨到..... tierPrice [0],而不是.... tierPrice [」 32000-5']來獲得每個單獨的定價對象。調用.... tierPrice [0]返回undefined,tierPrice.length返回undefined。

爲什麼我不能使用for循環訪問嵌套數組?我在這裏有什麼選擇?

謝謝!

回答

1

作爲一種替代的基於JavaScript的解決方案,遺憾的是需要進行更換的核心文件bundle.js我有一個基於PHP的解決方案

擺在首位的問題是,這個對象不應該是一個對象都沒有。我們可以介入JSON生成並確保它實際上是一個數組。

爲此,類Mage_Catalog_Model_Product_Type_Price必須用下面的方法重寫改寫:

public function getTierPrice($qty = null, $product) 
{ 
    $tierPrice = parent::getTierPrice($qty, $product); 
    if (is_array($tierPrice)) { 
     return array_values($tierPrice); 
    } 
    return $tierPrice; 
} 

getTierPrice()方法沒有任何地方使用,其中字符串鍵會有關,據我所看到的,所以這比將生成的JSON分開並重新創建它更優雅。

我寫了一個小的擴展,與其他束層的定價錯誤一起修復了這一點,你可以從它https://github.com/sgh-it/bundletierprices

延伸閱讀:http://www.schmengler-se.de/en/2014/10/magento-buendelprodukte-staffelpreise-der-einfachen-produkte-nutzen/

+0

在一次奇怪的事件轉變中,我實際上剛剛遇到了模塊解決的第二個問題(使用捆綁定價),現在在Google搜索將我帶回此線程後,現在正在使用它來解決此問題。幹得不錯! –

0

我主要使用Magento的1.7:

[跳到下面我的回答肉的更新。總結:我們學到了什麼?我認爲你使用的是Magento 1.5。但即使是Magento 1.7也不夠好。在Magento 1.8之前,您嘗試使用的功能尚未完全修復錯誤。]

我着手幫助你,因爲我知道使用bundle可以有多複雜。我沒有使用層級定價,因此我在開發服務器上設置了它,並開始逐步完成上面列出的bundle.js函數。

我發現,讓我困惑了兩兩件事:

一)我tierPrice[]使用索引數組0,1,2(我設置爲通過Magento管理三個層次): tierPrice object is an array object

這裏來幫助你是從我的包JSON對象定義一個片段,即從app/design/frontend/themename/default/template/bundle/catalog/product/view/type/bundle/bundle.phtml

<script type="text/javascript"> 
    //<![CDATA[ 
     var bundle = new Product.Bundle(<?php echo $this->getJsonConfig() ?>); 
    //]]> 
</script> 

段:

var bundle = new Product.Bundle({"options":{"837":{"selections":{"4205":{"qty":1,"customQty":"1","price":52,"priceInclTax":52,"priceExclTax":52,"priceValue":0,"priceType":"0","tierPrice":[{"price_id":"4","website_id":"0","all_groups":"1","cust_group":32000,"price":29.99,"price_qty":"2.0000","website_price":"29.9900"},{"price_id":"5","website_id":"0","all_groups":"1","cust_group":32000,"price":18.88,"price_qty":"3.0000","website_price":"18.8800"},{"price_id":"6","website_id":"0","all_groups":"1","cust_group":32000,"price":7.77,"price_qty":"4.0000","website_price":"7.7700"}], ... 

你的看起來像這樣嗎?

二)所以我的開發服務器上面的for循環後,bundle.js已經正確地確定了一層價格稍後在代碼中的變價系統會根據顯示含稅價或不含稅,即此代碼復位:

//file: skin/frontend/theme/default/js/bundle.js 
//function: selectionPrice() 
//... 
selection = this.config.options[optionId].selections[selectionId]; 
if (selection.priceInclTax !== undefined) { 
    priceInclTax = selection.priceInclTax; 
    price = selection.priceExclTax !== undefined ? selection.priceExclTax : selection.price; 
} else { 
    priceInclTax = price; 
} 
//... 

因此,到最後,我的「價格作爲配置的」最終不正確了。

您認爲如何?你能弄清楚爲什麼你的捆綁對象是tierPrice['32000-5']而不是tierPrice[0]?當bundle.js嘗試應用含稅或不含稅的顯示價格時,您的等級價格是否被覆蓋?

(並且似乎沒有默認的方式知道tierPrice是否包含稅或不含稅,我在這裏嗅到一個錯誤)。

其實這個錯誤報告可能是你感興趣的。 http://www.magentocommerce.com/bug-tracking/issue?issue=11477(需要登錄)],並有some other bugs tracked on tier pricing,這將有助於解釋代碼更新在Magento 1.8

更新:我已經提到bundle.js從Magento的1.7

我可以在Magento看到1.8捆綁.js已被改進以考慮tierPrice和tierPrice包括和不含稅 - 但也必須伴隨不同的捆綁JSON對象(以保存字段和值爲tierPrice[i].priceInclTax;tierPrice[i].priceExclTax;

因此,我們可能會現在回答:

一)升級到Magento的1.8

B)手動更新只是你的主題bundle.jsbundle.js Magento的1.8 代碼和爲JSON對象,文件中的「應用程序/核心/法師/Bundle/Block/Catalog/Product/View/Type/Bundle.php」,由1.7和1.8之間檢查,

Magento的1.7

//file app/core/Mage/Bundle/Block/Catalog/Product/View/Type/Bundle.php 
//class Mage_Bundle_Block_Catalog_Product_View_Type_Bundle 
//function getJsonConfig() 
//... 
       $tierPrices = $_selection->getTierPrice(); 
       foreach ($tierPrices as &$tierPriceInfo) { 
        $tierPriceInfo['price'] = $coreHelper->currency($tierPriceInfo['price'], false, false); 
       } 
       unset($tierPriceInfo); // break the reference with the last element 
//... 

Magento的1.8

//file app/core/Mage/Bundle/Block/Catalog/Product/View/Type/Bundle.php 
//class Mage_Bundle_Block_Catalog_Product_View_Type_Bundle 
//function getJsonConfig() 
//... 
       $tierPrices = $_selection->getTierPrice(); 
       foreach ($tierPrices as &$tierPriceInfo) { 
        $tierPriceInfo['price'] = $coreHelper->currency($tierPriceInfo['price'], false, false); 
        $tierPriceInfo['priceInclTax'] = $taxHelper->getPrice($_selection, $tierPriceInfo['price'], true); 
        $tierPriceInfo['priceExclTax'] = $taxHelper->getPrice($_selection, $tierPriceInfo['price']); 
       } 
       unset($tierPriceInfo); // break the reference with the last element 
//... 

所以,如果你是手動更新的Magento的舊版本,您將需要擴展的類Mage_Bundle_Block_Catalog_Product_View_Type_Bundle並創建您自己的新bundle.php與此更新getJsonConfig()

我還沒有運行之間的差異這些文件從1.7和1.8這是值得做的,看看是否有其他改變。

所有這一切都假設您可以深入瞭解爲什麼您有tierPrice ['32000-5'],如上所述,比較我的包JSON對象聲明與您的或粘貼在這裏供其他人檢查。

我們學到了什麼?我認爲你使用的是Magento 1.5。但即使是Magento 1.7也不夠好。在Magento 1.8之前,您嘗試使用的功能尚未完全修復錯誤。

Malachy。

+0

馬拉奇您好,感謝後 - 事實上,我想通出一個修正,只是涉及到更改bundle.js文件 - 它看起來像比您的建議更簡單 - 我現在將它發佈。 –

0

這是我回答我自己的問題!

所以我想出了爲什麼它不工作。 Magento代碼嘗試使用以下對象迭代:

var(i=0;i<object.length;i++) 

這不適用於對象。我改變了代碼通過對象與迭代在selectionPrice(在對象鍵):功能(optionId,selectionId)方法:

if (this.config.priceType == '0') { 
     price = this.config.options[optionId].selections[selectionId].price; 
     tierPrice = this.config.options[optionId].selections[selectionId].tierPrice; 

     // Ensures that tierPrice is set 
     // If a selection has no tier pricing it would return an empty array 

     if (tierPrice.length != 0){ 

     // Iterate through tier Pricing until you reach correct quantity break 
     // then set price.  

     for (key in tierPrice){ 
      if (tierPrice.hasOwnProperty(key)){ 
      if(tierPrice[key].price_qty <= qty && tierPrice[key].price <= price) { 
       price = tierPrice[key].price; 

      } 
     } 
     } 
     } 
在方法的底部

然後,你需要把條件tierPrice.length == 0否則你的層次的價格只是被再次改寫:

// Check that priceInclTax AND tierPrice are not set 
    if (selection.priceInclTax !== undefined && tierPrice.length == 0) { 

     priceInclTax = selection.priceInclTax; 
     price = selection.priceExclTax !== undefined ? selection.priceExclTax : selection.price; 

    } 

    else { 
     priceInclTax = price; 
    } 

這似乎這樣的伎倆,我 - 現在成功更新「價格作爲配置」佔到層次的價格,並且仍然成功更新所有其他定價太。如果你有一個Magento商店有Weee模塊/稅收規則,那麼它可能會大驚小怪,但對於我的商店配置,它的效果很好。

希望有幫助!

+0

爲了幫助其他訪問thia Q&A的人,此修補程序適用於哪個版本的Magento?你在1.5嗎? 1.6? – Malachy

+0

您可能想要編輯for..in循環以使用此語法,因爲[Douglas Crockford](http://javascript.crockford.com/code.html) 'for(object variable){ if( object.hasOwnProperty(變量)){ 聲明 } }' 爲了記錄在案,包括和不含稅價格不僅關係到WEEE它包括營業稅了,所以這是一個三層的價格是不是一個簡單的補丁複雜的稅收。 我認爲最好的解決方案是升級到Magento 1.8 – Malachy

+0

感謝您對hasOwnProperty的單挑!該補丁適用於我,因爲我們在結賬之前不會顯示稅金,因此,當您在捆綁頁面上時,您將始終看到沒有任何稅費的價格。升級到1.8也是一個不錯的解決方案,但是我仍然有點擔心從1.7.0.2(我現在的版本)開始移動,以防萬一破壞哈哈! –

相關問題