当前位置: 首页 > 面试题库 >

通过价格更改以编程方式将产品添加到购物车

包阳成
2023-03-14
问题内容

我想以编程方式将产品添加到购物车。另外,添加到购物车后,我想更改产品价格。

假设我的产品价格为100美元。加入购物车后,我想将其更改为$ 90。

我将产品添加到购物车。但是,我无法更改产品价格。

可能吗?

以下是将产品添加到购物车的代码:-

$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()工作。

以下是一些示例代码,您可以在用于监听checkout_cart_product_add_afteror
checkout_cart_update_items_after事件的Observer中使用。该代码在逻辑上是相同的,除了checkout_cart_product_add_after只对一项checkout_cart_update_items_after调用,对购物车中的所有项调用。仅作为示例,此代码被分离/复制为2种方法。

事件:checkout_cart_product_add_after

/**
 * @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
 */
html" target="_blank">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);
         }
    }
}


 类似资料:
  • 我使用WooCommerce Wordpress中的以下代码,通过一个模板,使用add_to_cart($product_id)功能添加了许多产品。 现在,我希望通过此模板为每个产品添加自定义价格。现在购物车中的价格和数据库中的价格一样,但我想通过这一点添加我的自定义价格。有人能帮我做同样的事吗。谢谢

  • 我使用“从Woocommerce中的functions.php文件中添加到购物车按钮上显示价格”的答案代码,在简单的产品中添加价格。 但我想改进这个功能,动态显示选择的变异价格内按钮。在变体产品页面上使用此代码,它只显示最低的价格。有什么解决办法吗?

  • 问题内容: 上面的代码将产品添加到购物车时间。但是我遇到的问题是我希望能够完全超越产品价格……据我所知,我唯一能做到的就是将优惠券应用到购物车。 有没有办法将价格完全覆盖到完全定制的价格上? 问题答案: 这是覆盖购物车中产品价格的代码 希望它会有用…

  • 我是新来的。我想将产品添加到购物车并列出购物车的所有产品。我已经将Co-Cart插件添加到我的服务器,并且我正在使用Co-Cart API实现购物车相关功能。 我面临的问题是,我不能在我的反应本地应用程序中查看购物车产品。以下是我现在使用的API: 1.将产品添加到购物车: 方法:邮寄 URL:https://www.myhost.com/wp-json/wc/v2/cart/add?token=

  • 我已经添加了一个自定义字段,其中有图片帧的额外价格,现在如果图片价格是10$和用户选择一个帧,它将加起来5$让说,总数将是15$。 现在,如果我添加另一个产品,它的选择定制框架价格应该得到添加。e、 g产品1的价格是:10美元,上面选择的框架是:frame1谁的价格是5美元,那么该产品的总价格将是15美元,如果产品2加上价格10美元,选择frame2的价格是6美元,那么该产品的总价格将是16美元,