我在购物车上运行了一些计算来改变价格 - 这非常有效。
但是如果我在购物车中添加两次相同的产品,但使用不同的自定义选项值 - 第一个产品选项值将被第二个产品的选项值覆盖....
嗯......不太好!
我不知道怎么弄明白...... 所以我做了一些肮脏的东西并克隆了每个正在转移到购物车的产品 - 所以我有一个新的Product_ID,我的计算应该以正确的方式运行,因为我认为问题是第一个产品值被第二个产品值覆盖,因为它的相同的产品ID .......
所以现在我想要的是:
_1选项:当相同的Product_ID在购物车中时,我需要在不更改第一个产品的价格的情况下运行我的计算
或
_2选项:我需要在购物车中加载克隆产品而不是原始产品并显示出来。
任何想法?
Cartcontroller.php
/**
* if customer enteres shopping cart we should mark quote
* as modified bc he can has checkout page in another window.
*/
$this->_getSession()->setCartWasUpdated(true);
Varien_Profiler::start(__METHOD__ . 'cart_display');
$this
->loadLayout()
->_initLayoutMessages('checkout/session')
->_initLayoutMessages('catalog/session')
->getLayout()->getBlock('head')->setTitle($this->__('Shopping Cart'));
$this->renderLayout();
Varien_Profiler::stop(__METHOD__ . 'cart_display');
}
public function addAction()
{
$connection = Mage::getSingleton("core/resource")->getConnection("core_read");
$cart = $this->_getCart();
$params = $this->getRequest()->getParams();
try {
if (isset($params['qty'])) {
$filter = new Zend_Filter_LocalizedToNormalized(
array('locale' => Mage::app()->getLocale()->getLocaleCode())
);
$params['qty'] = $filter->filter($params['qty']);
}
$product = $this->_initProduct();
$related = $this->getRequest()->getParam('related_product');
/**
* CLONE PRODUCT
*/
$clone=$product->duplicate();
$clone->setSku($clonedSku);
$clone->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE);
$clone->setStatus(1); // AKTIV=1
$clone->setVisibility(4);
$clone->setTaxClassId(2);
$clone->setCategoryIds("93");
/**
* Check product availability
*/
if (!$clone) {
$this->_goBack();
return;
}
$cart->addProduct($product, $params);
if (!empty($related)) {
$cart->addProductsByIds(explode(',', $related));
}
$cart->save();
################## table from which to get percent sku catalog_product_option_type_value & catalog_product_option
foreach($this->_getQuote()->getAllItems() as $item)
{
if($item->getProductId() == $product->getId())
{
$percentage_total = 0;
$optionsArray = array();
foreach($params['options'] as $key => $value){
$qry = "SELECT `catalog_product_option_type_value`.`option_type_id`, `catalog_product_option_type_value`.`option_id` ,`catalog_product_option_type_price`.`price` FROM `catalog_product_option_type_price` INNER JOIN `catalog_product_option_type_value` ON `catalog_product_option_type_price`.`option_type_id` = `catalog_product_option_type_value`.`option_type_id` AND `catalog_product_option_type_value`.`sku` = 'percent' AND `catalog_product_option_type_value`.`option_type_id` = '".$value."' AND `catalog_product_option_type_value`.`option_id` = '".$key."'";
$percentvalue = $connection->fetchAll($qry);
if(intval($percentvalue[0]['price'])>0) $optionsArray[] = $percentvalue[0];
}
if(count($optionsArray)>0)
{
$rowTotal = $item->getRowTotalInclTax();
$newTotal = $rowTotal;// - $percentage_total;
$newTotalGrand = 0;
foreach($optionsArray as $optionsArr)
{
$newTotalGrand += $optionsArr['price'];
}
if($newTotalGrand>0)
{
$newTotalGrand = ($newTotal/100)*$newTotalGrand;
$newTotal = $newTotal + $newTotalGrand;
$query = "UPDATE `sales_flat_quote_item` SET `custom_price` = '".$newTotal."',`original_custom_price` = '".$newTotal."' WHERE `sales_flat_quote_item`.`item_id` ='".$item->getId()."'";
$connection->query($query);
}
}
}
}
$clone->setPrice($newTotal);
$clone->getResource()->save($clone);
$this->_getSession()->setCartWasUpdated(true);
答案 0 :(得分:0)
当Magento将产品添加到购物车时,它首先执行查找以查看产品是否已添加,只是增加了数量。如您所述,添加具有不同自定义选项的相同产品时,这无济于事。
我找到了一种非常手动的方法。请务必使用自定义选项替换$YOUR_OPTIONS_HERE
并抓取正确的产品
// be sure to load the correct product
$product = Mage::getModel("catalog/product")->load($product_id);
$cart = Mage::getSingleton("checkout/cart");
$cart->init();
$options = new Varien_Object(array("options" => $YOUR_OPTIONS_HERE,
"qty" => 1));
$addall = $product->getTypeInstance(true)
->prepareForCartAdvanced($YOUR_OPTIONS_HERE, $product, Mage_Catalog_Model_Product_Type_Abstract::PROCESS_MODE_FULL);
$addme = array_shift($addall);
手动为购物车创建新的销售报价项目
$item = Mage::getModel('sales/quote_item');
$item->setStoreId(Mage::app()->getStore()->getId());
$item->setProduct($addme)
->setOptions($addme->getCustomOptions());
手动将其添加到报价单(购物车)
$item->setQty(1);
$cart->getQuote()->addItem($item);
添加您要添加的所有内容后
$cart->save();