Magento-基于用户输入的报价/订购产品项属性 [英] Magento - Quote/order product item attribute based on user input

查看:90
本文介绍了Magento-基于用户输入的报价/订购产品项属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

摘要

我想创建一个产品属性,该属性不保存到产品中,也不像普通产品属性一样显示在产品编辑页面上.相反,我希望将其保存到订单/报价项目中并显示在订单,发票等上.在将产品添加到购物车之前,客户还应该可以在前端对它进行配置.

I want to create a product attribute that is not saved to products, or displayed on the product edit page like ordinary product attributes. Instead I want it to be saved to order/quote items and displayed on orders, invoices and such. It should also be configurable by the customer in the frontend before adding a product to the cart.

详细信息

  • 就像自定义选项一样,应该将表单元素添加到前端产品页面.
    • 自定义选项不同,这不是实际产品属性.它不应显示在管理产品页面或属性集上.
    • 要求客户提供有效值.我需要能够进行服务器端验证.
    • 我想要一个.phtml模板来生成其html.目前,我能够以令人满意的(设计)结果覆盖 app/design/frontend/base/default/catalog/product/view/type/default.phtml .但是我不知道如何捕获,验证并最终保存其价值.
    • Just like with Custom Options, a form element should be added to the frontend product page.
      • Unlike Custom Options, this is not an actual product attribute. It should not be displayed on the admin product pages or attribute sets.
      • The customer is required to provide a valid value. I need to be able to do server-side validation.
      • I want to have a .phtml template generating its html. Currently I'm able to override app/design/frontend/base/default/catalog/product/view/type/default.phtml with satisfactory (design) results. However I don't know how to capture, validate and eventually save its value.
      • 此值应显示在所有发票,订单,销售电子邮件中.
      • 我想使用模板来控制输出,或者至少能够返回用于显示值的字符串

      我的问题

      1. 当产品被添加到购物车时,如何验证前端的商品页面上的<input>中的值,并最终将其保存到报价项目中?后来,在结帐过程中,如何将该值保存到订单项目中?
      2. 如何在订单,发票,销售电子邮件和此类页面上显示此值?
      3. 如何过滤订单集合以获取包含我的值设置为特定值的商品的订单?
      1. How do I validate and eventually save the value from a <input> on the frontend product page to the quote item when the product is added to the cart, and later in the checkout process to the order item?
      2. How do I display this value on the order, invoice, sales emails and such pages?
      3. How do I filter an order collection to fetch orders that has items with my value set to a specific value?

      更新1

      我发现我可以在sales_quote_item_qty_set_after

      $infoBuyRequest = $product->getCustomOption('info_buyRequest');
      $buyRequest = new Varien_Object(unserialize($infoBuyRequest->getValue()));
      $myData = $buyRequest->getMyData();
      

      通过这种方式,我能够从产品页面上的<input>中检索客户提供的自定义数据.

      In this way I was able to retrieve my custom, customer supplied, data from my <input> on the product page.

      我怀疑此info_buyRequest与报价和订单项一起保存.如果是这样,此部分解决了我的问题1和2.但是,我仍然不知道在何处运行此代码,也不知道如何在后端订单/报价/报告页面上显示它. .我也相信,因为这是作为序列化值存储在数据库中的,所以基于我的自定义数据来获取报价/订单项集合将是最困难的.

      I suspect this info_buyRequest is saved with the quote and order items. If so, this partially solved my problems 1 and 2. However, I still dont know where it's suitable to run this code, and I dont know how to display it on the backend order/quote/report pages. Also I belive since this is stored as a serialized value in the database, it will be most difficult to get quote/order item collections based on my custom data.

      推荐答案

      Magento提供了添加非产品属性或产品自定义选项的功能.它们设置在产品上,并使用选项代码additional_options报价.

      Magento provides a capability for adding options that aren't product attributes or product custom options. They are set on the product and quote items with the option code additional_options.

      您需要执行两个步骤,每个步骤都可以通过事件观察器进行处理.如果您希望其他选项能够进行重新排序,则还需要观察第三件事.

      There are two steps you need to take, each can be handled via an event observer. If you want the additional options to carry through reordering, you will need also observe a third event.

      第一步是添加事件观察器,以便在将已加载产品添加到购物车之前在其上设置其他选项.一种选择是使用catalog_product_load_after事件.

      The first step is to add the event observer to set the additional options on the loaded product before it is added to the cart. One option is to use the catalog_product_load_after event.

      <catalog_product_load_after>
          <observers>
              <extra_options>
                  <type>model</type>
                  <class>extra_options/observer</class>
                  <method>catalogProductLoadAfter</method>
              </extra_options>
          </observers>
      </catalog_product_load_after>
      

      在事件观察器中,您可以添加其他检查,所请求的页面确实是添加到购物车"操作.此观察者方法的要点是将特殊选项的选择添加到产品模型的additional_options选项中.

      In the event observer you can add additional checks the requested page is indeed an add to cart action. The main point of this observer method is to add the selection of your special options to the additional_options option on the product model.

      public function catalogProductLoadAfter(Varien_Event_Observer $observer)
      {
          // set the additional options on the product
          $action = Mage::app()->getFrontController()->getAction();
          if ($action->getFullActionName() == 'checkout_cart_add')
          {
              // assuming you are posting your custom form values in an array called extra_options...
              if ($options = $action->getRequest()->getParam('extra_options'))
              {
                  $product = $observer->getProduct();
      
                  // add to the additional options array
                  $additionalOptions = array();
                  if ($additionalOption = $product->getCustomOption('additional_options'))
                  {
                      $additionalOptions = (array) unserialize($additionalOption->getValue());
                  }
                  foreach ($options as $key => $value)
                  {
                      $additionalOptions[] = array(
                          'label' => $key,
                          'value' => $value,
                      );
                  }
                  // add the additional options array with the option code additional_options
                  $observer->getProduct()
                      ->addCustomOption('additional_options', serialize($additionalOptions));
              }
          }
      }
      

      其他选项将自动从产品移至报价项目.放置此观察者后,您的选择将显示在购物车和结帐评论中.

      The additional options will be moved from the product to the quote item automatically. With this observer in place, your options will appear in the cart and the checkout review.

      为了使它们持久存在,需要一个额外的观察者(仅从Magento 1.5开始).

      In order to have them persist, one additional observer is needed (only since Magento 1.5).

      <sales_convert_quote_item_to_order_item>
          <observers>
              <extra_options>
                  <type>model</type>
                  <class>extra_options/observer</class>
                  <method>salesConvertQuoteItemToOrderItem</method>
              </extra_options>
          </observers>
      </sales_convert_quote_item_to_order_item>
      

      在这里,我们将选项从报价项目移动到订单项目.

      Here we move the option from the quote item to the order item.

      public function salesConvertQuoteItemToOrderItem(Varien_Event_Observer $observer)
      {
          $quoteItem = $observer->getItem();
          if ($additionalOptions = $quoteItem->getOptionByCode('additional_options')) {
              $orderItem = $observer->getOrderItem();
              $options = $orderItem->getProductOptions();
              $options['additional_options'] = unserialize($additionalOptions->getValue());
              $orderItem->setProductOptions($options);
          }
      }
      

      从这一点开始,其他选项将在前端和订单电子邮件的客户订单历史记录以及管理界面的订单视图,发票,货件,贷项凭证和PDF中可见.

      From this point on the additional options will be visible in the customer order history in the frontend and the order emails, as well as in the admin interface order view, invoices, shipments, creditmemos and PDFs.

      为了在重新订购期间将Oprions转移到新订单,您需要注意将其复制.这是使用checkout_cart_product_add_after事件的一种可能性.

      In order to carry the oprions over to the new order during a reorder, you need to take care to copy them over. Here is one possibility using the checkout_cart_product_add_after event.

      <checkout_cart_product_add_after>
          <observers>
              <extra_options>
                  <type>singleton</type>
                  <class>extra_options/observer</class>
                  <method>checkoutCartProductAddAfter</method>
              </extra_options>
          </observers>
      </checkout_cart_product_add_after>
      

      应该将附加选项的解析和构建附加选项数组移到一个单独的函数中,以避免代码重复,但是对于本示例,为了清楚起见,我将每种方法所需的逻辑保留在原处.

      The parsing of the extra options and building the additional options array should be moved into a separate function to avoid code duplication, but for this example I'll leave the required logic for each method in place for clarity.

      public function checkoutCartProductAddAfter(Varien_Event_Observer $observer)
      {
          $action = Mage::app()->getFrontController()->getAction();
          if ($action->getFullActionName() == 'sales_order_reorder')
          {
              $item = $observer->getQuoteItem();
              $buyInfo = $item->getBuyRequest();
              if ($options = $buyInfo->getExtraOptions())
              {
                  $additionalOptions = array();
                  if ($additionalOption = $item->getOptionByCode('additional_options'))
                  {
                      $additionalOptions = (array) unserialize($additionalOption->getValue());
                  }
                  foreach ($options as $key => $value)
                  {
                      $additionalOptions[] = array(
                          'label' => $key,
                          'value' => $value,
                      );
                  }
                  $item->addOption(array(
                      'code' => 'additional_options',
                      'value' => serialize($additionalOptions)
                  ));
              }
          }
      }
      

      翻译:

      没有适当的机制来转换这些选项标签或值.这里有一些想法可能在这方面有用.

      Translation:

      There is no mechanism in place to translate these option labels or values. Here are a few ideas that might be useful in that regard.

      在quote_item_load_after事件观察器中,获取附加选项数组并设置$option['print_value'] = $helper->__($option['value']);.如果设置了print_value,Magento将使用它来渲染显示.
      订单项也可以这样做.

      In a quote_item_load_after event observer, get the additional options array and set $option['print_value'] = $helper->__($option['value']);. If print_value is set, Magento will use that for rendering the display.
      The same can be done with order items.

      没有print_label之类的东西,但是您可以设置自定义索引(也许是label_source),并使用该索引作为源动态设置标签,例如$option['label'] = $helper->__($option['label_source']);.

      There is no such thing as a print_label, but you could set a custom index (label_source maybe) and set the label on the fly using that as the source, e.g. $option['label'] = $helper->__($option['label_source']);.

      除此之外,您可能还不得不诉诸于修改模板(getItemOptions()的grep)或覆盖块类(grep additional_options).

      Beyond that you would probably have to resort to modifying the templates (grep for getItemOptions()), or overriding the block classes (grep additional_options).

      这篇关于Magento-基于用户输入的报价/订购产品项属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆