简体   繁体   English

在Magento中向购物车项目添加属性,在购物车中单独显示

[英]Add attribute to cart items in Magento, show separate in cart

I found a lot of answers to the problem I have, but some are contradicting and many are not fully clear to me, so I hope a Magento expert can point me into the right direction. 我找到了解决这个问题的很多答案,但是有些矛盾并且对我而言还不是很清楚,所以我希望Magento专家可以为我指明正确的方向。

For some Products in Magento, there is a complex customization process. 对于Magento中的某些产品,有一个复杂的自定义过程。 There is a custom editor and every "design" is identified by a unique value. 有一个自定义编辑器,每个“设计”均由唯一值标识。 What I need is to store this ID with the product in the cart and the order. 我需要将此ID与产品一起存储在购物车和订单中。 It shouldn't be shown directly, but accessible in the code (to show a different thumbnail in the cart or some information on the invoice, for example). 它不应该直接显示,而是可以在代码中访问(例如,在购物车中显示其他缩略图或发票上的某些信息)。

Additionally, because the product is customized, the same product should have a separate line in the cart and invoice if this id differs! 此外,由于产品是定制的,因此如果此ID不同,则同一产品在购物车和发票中应有单独的一行!

So for example, a customer opens the product page, does some customization and this customization gets an ID "1". 因此,例如,某个客户打开产品页面,进行了一些自定义,此自定义获得的ID为“ 1”。 The customer puts that into the cart. 客户将其放入购物车。 He opens the same product again, customizes it differently and it gets the id "2". 他再次打开相同的产品,对其进行不同的自定义,并获得ID“ 2”。 The customer also puts this into the cart! 客户还将其放入购物车!

The cart should now be: 现在,购物车应为:

  • ProductX [custom text generated by code using id 1] 1 piece x€ ProductX [使用ID 1的代码生成的自定义文本] 1件x€
  • ProductX [custom text generated by code using id 2] 1 piece x€ ProductX [使用ID 2由代码生成的自定义文本] 1件x€

and NOT: 并不是:

  • ProductX 2 piece x€ 产品X 2件x€

But still the customer should be able to order more than one of the same product with same ID! 但是客户仍然应该能够订购多个具有相同ID的相同产品! so eg 所以例如

  • ProductX [custom text generated by code using id 1] 4 piece x€ ProductX [使用ID 1的代码生成的自定义文本] 4件x€
  • ProductX [custom text generated by code using id 2] 2 piece x€ ProductX [使用ID 2由代码生成的自定义文本] 2件x€

The customization happens outside of magento. 定制发生在magento之外。 Magento just has a link with a popup and then gets some ID that can then be attached using PHP. Magento仅有一个带有弹出窗口的链接,然后获取一些ID,然后可以使用PHP附加该ID。 It just stores the ID and it has to be possible to get the ID for every product in cart/invoice/... 它仅存储ID,并且必须能够在购物车/发票/ ...中获取每种产品的ID。

Important is that the ID is stored and that products with same sku/customizable options are not merged in the cart if the ID is different! 重要的是要存储ID,并且如果ID不同,则不会将具有相同sku / customizable选项的产品合并到购物车中! Also the ID has to be a text string and not selected from a list like with magento customizable options! 另外,ID必须是文本字符串,并且不能从列表中选择,例如magento可自定义选项!

Methods I found that might do what I want: 我发现的方法可以满足我的要求:

  1. http://www.atwix.com/magento/custom-product-attribute-quote-order-item/ This looks the best for me, there is a finished Module I could modify. http://www.atwix.com/magento/custom-product-attribute-quote-order-item/这对我来说似乎是最好的,有一个成品模块可以修改。 But I am not sure if it is the correct approach. 但是我不确定这是否正确。 Will the products be separated in cart? 产品会在购物车中分开吗? Also the value of the ID is set as Product option here and visible in the backend?! 另外,ID的值在此处设置为“产品”选项,并在后端可见吗? There is no value before a customer customizes the product in my case! 在我的情况下,客户定制产品之前没有任何价值!

    1. https://magento.stackexchange.com/a/1382 I am not quite sure... How can I get the ID, that is available on the product detail page, inside the observer... https://magento.stackexchange.com/a/1382我不太确定...如何获取观察者内部产品详细信息页面上可用的ID ...

    2. How can I add every product as a new item into cart Maybe I need to modify this in addition to 1. or 2.? 如何将每个产品作为新商品添加到购物车中?也许我需要在1.或2之外进行修改?

I would greatly appreciate it if a Magento Expert could shine some light on this for me! 如果Magento Expert可以为我提供一些帮助,我将不胜感激!

You said you can take care of adding the ID to the product page. 您说过可以将ID添加到产品页面中。 Which is great. 太好了 When required, you need to write the value to a hidden input field within the add to cart form on the product page. 必要时,您需要将该值写入产品页面上“添加到购物车”表单中的隐藏输入字段中。 Let's say the input looks like this; 假设输入看起来像这样;

<input type="hidden" name="myid" id="myid" value="123" />

Within your custom module, add an event observer. 在您的自定义模块中,添加一个事件观察器。 I am assuming here that you remain on the product page when you add to cart, rather than go direct to the cart. 我在这里假设您将其添加到购物车后仍留在产品页面上,而不是直接转到购物车。

<catalog_product_load_after>
<observers>
    <Company_Module_Model_Observer>
        <type>model</type>
        <class>Company_Module_Model_Observer</class>
        <method>addPostData</method>
    </Company_Module_Model_Observer>
</observers>
</catalog_product_load_after>   

Within your module folder, create or open Company/Module/Model/Observer.php and create this function; 在您的模块文件夹中,创建或打开Company / Module / Model / Observer.php并创建此功能;

class Company_Module_Model_Observer {


public function addPostData(Varien_Event_Observer $observer) {

      $action = Mage::app()->getFrontController()->getAction();
      if ($action->getFullActionName() == 'checkout_cart_add') {
         if($action->getRequest()->getParam('myid')) {
             // ID IS PRESENT, SO LETS ADD IT
             $item = $observer->getProduct();
             $additionalOptions = array();
             $additionalOptions[] = array(
                'label' => 'The unique ID',
                'value' => $action->getRequest()->getParam('myid')
            );
             $item->addCustomOption('additional_options', serialize($additionalOptions));
         }
      }

   }


}

With this in place, you will see the ID against the product in the cart and on the quote, just like if the value was from a super attribute. 设置好此位置后,您将在购物车和报价中看到该产品的ID,就像该值来自超级属性一样。 Any unique ID will be seen as a new line item. 任何唯一的ID将被视为新的订单项。

To save this value against the sales order so it shows on sales emails and in the backend, add this observer; 要针对销售订单保存此值,使其在销售电子邮件中和后端显示,请添加此观察者;

<sales_convert_quote_item_to_order_item>
<observers>
    <Company_Module_Model_Observer>
        <type>model</type>
        <class>Company_Module_Model_Observer</class>
        <method>salesConvertQuoteItemToOrderItem</method>
    </Company_Module_Model_Observer>
</observers>
</sales_convert_quote_item_to_order_item>

And add this function to Company/Module/Model/Observer.php 并将此功能添加到Company / Module / Model / Observer.php

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);
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM