久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

以編程方式將產(chǎn)品添加到購(gòu)物車(chē)并更改價(jià)格

Programmatically add product to cart with price change(以編程方式將產(chǎn)品添加到購(gòu)物車(chē)并更改價(jià)格)
本文介紹了以編程方式將產(chǎn)品添加到購(gòu)物車(chē)并更改價(jià)格的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我想以編程方式將產(chǎn)品添加到購(gòu)物車(chē).另外,我想在添加到購(gòu)物車(chē)時(shí)更改產(chǎn)品價(jià)格.

I want to add a product to cart programmatically. Also, I want to change the product price when added to cart.

假設(shè)我的產(chǎn)品價(jià)格是 100 美元.我想在添加到購(gòu)物車(chē)時(shí)將其更改為 90 美元.

Suppose, my product's price is $100. I wanted to change it to $90 when added to cart.

我將產(chǎn)品添加到購(gòu)物車(chē).但是,我無(wú)法更改產(chǎn)品價(jià)格.

I added product to cart. However, I am unable to change the product price.

有可能嗎?

這是將產(chǎn)品添加到購(gòu)物車(chē)的代碼:-

Here is the code to add product to cart:-

$cart = Mage::getSingleton('checkout/cart');

try {   
    $cart->addProduct($product, array('qty' => 1));
    $cart->save();
}
catch (Exception $ex) {
    echo $ex->getMessage();
}

推薦答案

深入 Magento 的核心代碼后,我發(fā)現(xiàn)你需要使用 $item->getProduct()->setIsSuperMode(true) 以使 $item->setCustomPrice()$item->setOriginalPrice() 工作.

After digging a bit into Magento's core code, I found that you need to use $item->getProduct()->setIsSuperMode(true) in order to make $item->setCustomPrice() and $item->setOriginalPrice() work.

這里有一些示例代碼,您可以在監(jiān)聽(tīng) checkout_cart_product_add_aftercheckout_cart_update_items_after 事件的觀察者中使用.代碼在邏輯上是相同的,除了 checkout_cart_product_add_after 只為一件商品調(diào)用,checkout_cart_update_items_after 為購(gòu)物車(chē)中的所有商品調(diào)用.此代碼僅作為示例被分離/復(fù)制為 2 個(gè)方法.

Here is some sample code you can use within an Observer that listens for the checkout_cart_product_add_after or checkout_cart_update_items_after events. The code is logically the same except checkout_cart_product_add_after is called for only one item and checkout_cart_update_items_after is called for all items in the cart. This code is separated/duplicated into 2 methods only as an example.

/**
 * @param Varien_Event_Observer $observer
 */
public function applyDiscount(Varien_Event_Observer $observer)
{
    /* @var $item Mage_Sales_Model_Quote_Item */
    $item = $observer->getQuoteItem();
    if ($item->getParentItem()) {
        $item = $item->getParentItem();
    }

    // Discounted 25% off
    $percentDiscount = 0.25; 

    // This makes sure the discount isn't applied over and over when refreshing
    $specialPrice = $item->getOriginalPrice() - ($item->getOriginalPrice() * $percentDiscount);

    // Make sure we don't have a negative
    if ($specialPrice > 0) {
        $item->setCustomPrice($specialPrice);
        $item->setOriginalCustomPrice($specialPrice);
        $item->getProduct()->setIsSuperMode(true);
    }
}

事件:checkout_cart_update_items_after

/**
 * @param Varien_Event_Observer $observer
 */
public function applyDiscounts(Varien_Event_Observer $observer)
{
    foreach ($observer->getCart()->getQuote()->getAllVisibleItems() as $item /* @var $item Mage_Sales_Model_Quote_Item */) {
         if ($item->getParentItem()) {
             $item = $item->getParentItem();
         }

         // Discounted 25% off
         $percentDiscount = 0.25; 

         // This makes sure the discount isn't applied over and over when refreshing
         $specialPrice = $item->getOriginalPrice() - ($item->getOriginalPrice() * $percentDiscount);

         // Make sure we don't have a negative
         if ($specialPrice > 0) {
             $item->setCustomPrice($specialPrice);
             $item->setOriginalCustomPrice($specialPrice);
             $item->getProduct()->setIsSuperMode(true);
         }
    }
}

這篇關(guān)于以編程方式將產(chǎn)品添加到購(gòu)物車(chē)并更改價(jià)格的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Magento products by categories(按類(lèi)別劃分的 Magento 產(chǎn)品)
Resource interpreted as image but transferred with MIME type text/html - Magento(資源被解釋為圖像但使用 MIME 類(lèi)型 text/html 傳輸 - Magento)
Is there an event for customer account registration in Magento?(Magento 中是否有客戶(hù)帳戶(hù)注冊(cè)事件?)
Magento addFieldToFilter: Two fields, match as OR, not AND(Magento addFieldToFilter:兩個(gè)字段,匹配為 OR,而不是 AND)
quot;Error 404 Not Foundquot; in Magento Admin Login Page(“未找到錯(cuò)誤 404在 Magento 管理員登錄頁(yè)面)
Get Order Increment Id in Magento(在 Magento 中獲取訂單增量 ID)
主站蜘蛛池模板: 夜夜骑首页 | 精品真实国产乱文在线 | 免费一级黄色录像 | caoporn国产精品免费公开 | 中文在线一区二区 | 麻豆精品久久 | 91一区二区三区在线观看 | 中文字幕高清av | 成人av免费 | 久久成人精品视频 | 日韩欧美一区在线 | 激情欧美一区二区三区中文字幕 | 99视频免费 | 国产精品免费一区二区三区 | 韩日三级| 日韩av免费在线电影 | 国产无人区一区二区三区 | 亚洲精品二区 | 自拍 亚洲 欧美 老师 丝袜 | 国产成人91 | 精品欧美乱码久久久久久1区2区 | 一区二区激情 | 欧美精品一二三区 | 亚洲小说图片 | 九九精品在线 | 亚洲国产精品视频一区 | 欧美二区三区 | 欧美狠狠操 | 91人人看 | 久久国产综合 | 另类专区亚洲 | 久久99精品久久久久子伦 | 日本一区二区三区四区 | 国产精品www | 精品福利一区二区三区 | 欧美www在线观看 | 日韩一区二区av | 黄网址在线观看 | 欧美成视频在线观看 | 日韩中文字幕一区二区三区 | 黄色一级视频 |