Magento - 根据用户输入报价/订购产品项目属性

时间:2022-12-02
本文介绍了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. 如何在产品添加到购物车时验证并最终将前端产品页面上的 中的值保存到报价项中,然后在结帐过程中以订单项目?
      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 等事件期间在 catalog/product 模型(可能还有 sales/quote_item)上运行此代码

      I've discovered that I can run this code on a catalog/product model (and probably sales/quote_item as well) during events such as 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']);.

      除此之外,您可能不得不求助于修改模板(grep for getItemOptions()),或覆盖块类(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 - 根据用户输入报价/订购产品项目属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:如何在 Magento 中创建一个简单的“Hello World" 下一篇:Magento - 检索具有特定属性值的产品

相关文章

最新文章