我开发了定制价格的扩展,这是我的代码
item.php:
class WebDirect_CustomPrice_Model_Sales_Quote_Item
extends Mage_Sales_Model_Quote_Item {
public function representProduct($product) {
$parentResult = parent::representProduct($product);
//if parent result is already false, we already have a different product, exit.
if ($parentResult === false) {
return $parentResult;
}
$bool = "";
$itemProduct = $this->getProduct();
Mage::app()->getRequest()->getPost();
$customPrice = Mage::app()->getRequest()->getParam('custom_price');
$cart = Mage::getModel('checkout/cart')->getQuote();
foreach ($cart->getAllItems() as $item) {
$productId = $item->getProductId();
$productPrice = $item->getPrice();
if($productId == $itemProduct->getId()){
/*
* do our check for 'same product' here.
* Returns true if attribute is the same thus it is hte same product
*/
if ($productPrice == $customPrice) {
$bool = true; //same product
break;
} else {
$bool = false; //different product
}
}
}return $bool;
}
}
观察员:
class WebDirect_CustomPrice_Model_Observer {
const MODULE_NAME = 'WebDirect_CustomPrice';
public $test = false;
public function convertPricespanToInput($observer = NULL) {
if (!$observer) {
return;
}
if ('product.info.simple' == $observer->getEvent()->getBlock()->getNameInLayout()) {
if (!Mage::getStoreConfig('advanced/modules_disable_output/'.self::MODULE_NAME)) {
$transport = $observer->getEvent()->getTransport();
$block = new WebDirect_CustomPrice_Block_priceSpanToInput();
$block->setPassingTransport($transport['html']);
$block->toHtml();
}
}
return $this;
}
/**
* @param Varien_Event_Observer $observer
*/
public function applyCustomPrice(Varien_Event_Observer $observer) {
// @var $item Mage_Sales_Model_Quote_Item
$item = $observer->getQuoteItem();
if ($item->getParentItem()) {
$item = $item->getParentItem();
}
Mage::app()->getRequest()->getPost();
$customPrice = Mage::app()->getRequest()->getParam('custom_price');
$defaultCp = $item->getProduct()->getData('min_price');
if($customPrice >= $defaultCp){
$item->setCustomPrice($customPrice);
$item->setOriginalCustomPrice($customPrice);
$item->getProduct()->setIsSuperMode(true);
}
}
问题是:
1 - 添加product2 1000 $; 2- add product2 2000 $;工作正常,它看起来像这样:
- Product2 ------ Qty: 1 ---------- total: 1000$
- Product2 ------ Qty: 1 ---------- total: 2000$
3 - 添加product2 2000 $;现在它擦除product2(1000 $)并将product2 qty = 2总计4000 $(ERROR !!!!)
现在购物车是: - Product2 ------ Qty: 2 ---------- total: 4000$
- Product2 ------ Qty: 1 ---------- total: 2000$
所以看起来应该是这样的:
- Product2 ------ Qty: 1 ---------- total: 1000$
- Product2 ------ Qty: 2 ---------- total: 4000$
这是描述问题的图像:
(它是同一产品,价格不同)
我想要的是在将产品添加到购物车时,magento以相同的价格创建相同的产品,如果它的产品具有不同的价格值,则将其添加为不同的产品,然后添加它!
如何解决这个问题?