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

以編程方式將產品添加到購物車并更改價格

Programmatically add product to cart with price change(以編程方式將產品添加到購物車并更改價格)
本文介紹了以編程方式將產品添加到購物車并更改價格的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我想以編程方式將產品添加到購物車.另外,我想在添加到購物車時更改產品價格.

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

假設我的產品價格是 100 美元.我想在添加到購物車時將其更改為 90 美元.

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

我將產品添加到購物車.但是,我無法更改產品價格.

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

有可能嗎?

這是將產品添加到購物車的代碼:-

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 的核心代碼后,我發現你需要使用 $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.

這里有一些示例代碼,您可以在監聽 checkout_cart_product_add_aftercheckout_cart_update_items_after 事件的觀察者中使用.代碼在邏輯上是相同的,除了 checkout_cart_product_add_after 只為一件商品調用,checkout_cart_update_items_after 為購物車中的所有商品調用.此代碼僅作為示例被分離/復制為 2 個方法.

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);
         }
    }
}

這篇關于以編程方式將產品添加到購物車并更改價格的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

Magento products by categories(按類別劃分的 Magento 產品)
Resource interpreted as image but transferred with MIME type text/html - Magento(資源被解釋為圖像但使用 MIME 類型 text/html 傳輸 - Magento)
Is there an event for customer account registration in Magento?(Magento 中是否有客戶帳戶注冊事件?)
Magento addFieldToFilter: Two fields, match as OR, not AND(Magento addFieldToFilter:兩個字段,匹配為 OR,而不是 AND)
quot;Error 404 Not Foundquot; in Magento Admin Login Page(“未找到錯誤 404在 Magento 管理員登錄頁面)
Get Order Increment Id in Magento(在 Magento 中獲取訂單增量 ID)
主站蜘蛛池模板: 在线视频 欧美日韩 | 国产精品污www在线观看 | 啪啪网页| 麻豆精品国产免费 | 欧美a级网站 | 日韩精品三区 | 日韩欧美字幕 | 国产一区二区三区色淫影院 | 五月婷婷婷 | 亚洲精品电影 | 精品一区二区三区在线观看国产 | 国产高清免费视频 | 欧美一区二区三区国产 | 激情五月婷婷综合 | 在线观看www高清视频 | 亚洲最大av网站 | 日韩欧美在线视频观看 | 亚洲激情一区二区三区 | 欧美综合久久久 | av在线免费观看网址 | 国产一区二区三区四区五区加勒比 | 国产一级片网站 | a毛片 | 欧美片网站免费 | 91av小视频| www.成人久久 | 日韩国产欧美在线观看 | 欧美日韩一区二区三区四区五区 | 精品国产乱码久久久久久丨区2区 | 成人在线看片 | 欧美精品久久久 | 男人的天堂一级片 | 狠狠狠色丁香婷婷综合久久五月 | 久久777 | 亚洲国产成人在线 | 亚洲瑟瑟 | 天天干b | 日韩日韩日韩日韩日韩日韩日韩 | 久久精品国产一区 | 久久亚洲综合 | 国产精品久久久久久久久久久新郎 |