我想从用户那里获得3个字段,并用自定义大小填充,然后根据该大小需要为其生成价格。我可以在页面上显示该价格,但当添加到购物车中时,该价格是该产品的原始价格 我将此代码与Observer方法一起使用 This is Observer
class CustomPrice implements ObserverInterface
{
public function execute(\Magento\Framework\Event\Observer $observer) {
$item=$observer->getEvent()->getData('quote_item');
$product=$observer->getEvent()->getData('product');
// here i am using item's product final price
$price = 1000; // 10 is custom price. It will increase in product price.
// Set the custom price
$item->setCustomPrice($price);
$item->setOriginalCustomPrice($price);
// Enable super mode on the product.
$item->getProduct()->setIsSuperMode(true);
return $this;
}
}
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="checkout_cart_update_items_after">
<observer name="customprice" instance="MGS\CartPrice\Observer\CustomPrice" />
</event>
我采用了https://webkul.com/blog/magento2-set-custom-price-of-product/中的代码