【问题标题】:Get product Custom option value in event sales_quote_add_item在事件 sales_quote_add_item 中获取产品自定义选项值
【发布时间】: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


    【解决方案1】:

    使用这个我得到选项数据:

    $options = $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct());
    $customOptions = $options['options'];
    $title = array();
    if (!empty($customOptions)) 
    {
        foreach ($customOptions as $key=>$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'];
            //$title[] = $option['option_value'];
        }
    
        //$cart->getquote()->getItemById($item->getItemId())->setOptions($options);
        //$cart->save();
    }
    

    【讨论】:

      猜你喜欢
      • 2016-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-02
      • 2023-03-21
      • 2013-10-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多