【发布时间】:2020-05-04 22:40:50
【问题描述】:
下面是我的代码,我调用 sales_quote_add_item 以根据自定义选项更改产品价格。我希望它在管理员上添加到购物车。
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="sales_quote_add_item">
<observer name="customprice" instance="Smartupworld\Core\Observer\Admin\Price" />
</event>
</config>
我这里是php观察者
<?php
namespace Smartupworld\Core\Observer\Admin;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\RequestInterface;
class Price implements ObserverInterface
{
public function execute(\Magento\Framework\Event\Observer $observer) {
//$product=$observer->getEvent()->getData('product');
$item = $observer->getEvent()->getData('quote_item');
$item = ( $item->getParentItem() ? $item->getParentItem() : $item );
$customOptions = $objectManager->get('Magento\Catalog\Model\Product\Option')->getProductOptionCollection($item->getProduct());
$options = $item->getProductOptions();
if (isset($options['options']) && !empty($options['options'])) {
foreach ($options['options'] as $option) {
echo 'Title: ' . $option['label'] . '<br />';
echo 'ID: ' . $option['option_id'] . '<br />';
echo 'Type: ' . $option['option_type'] . '<br />';
echo 'Value: ' . $option['option_value'] . '<br />' . '<br />';
//$title[] = $option['label'];
}
}
$price = 100;
$item->setCustomPrice($price);
$item->setOriginalCustomPrice($price);
$item->getProduct()->setIsSuperMode(true);
}
我无法获得 price.php 文件的选项值。我想根据选项值更改价格。但我无法得到它。
【问题讨论】:
标签: magento magento2 magento-2.3