动态添加产品自定义选项magento [英] dynamically add product custom options magento

查看:73
本文介绍了动态添加产品自定义选项magento的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是magento的新手. 我为从后端添加的产品添加了一些自定义选项,例如名称,地址,电子邮件.

所以现在看起来像这样:

Item 1

    Name : [textbox]
    Email : [textbox]
    Address : [textarea]

现在我要做的是动态添加自定义选项. 为此,将有2个按钮,添加项目"和删除项目".

因此,当我单击添加项目"时,结果应如下所示:

Item 1

    Name : [textbox]
    Email : [textbox]
    Address : [textarea]

Item 2

    Name : [textbox]
    Email : [textbox]
    Address : [textarea]

当我单击删除项目"时,它应该删除一个项目.

我该如何实施?谢谢

解决方案

如果它是完全动态的,则可以在产品详细信息页面上创建一个表单,以便添加和删除项目,您需要使用jQuery创建该表单.

对于顺序管理,如果您在引号中添加此数据,则将按整体顺序对其进行管理.

因此,要添加动态创建的选项,您需要创建一个模块.

我已经创建了具有名称空间 Tech 的模块个性化,如果您在产品详细信息页面表单上可以使用此输入框,则会添加intials自定义选项.

因此,在创建模块之前,在名为intials的产品详细信息"页面中添加新的表单元素.

Tech/Personlization/etc/config.xml文件中,您需要编写如下代码:

<?xml version="1.0"?>
<config>
<modules>
<Tech_Personlization>
  <version>1.0.0</version>
</Tech_Personlization>
</modules>
<global>
  <models>
  <personlization>
    <class>Tech_Personlization_Model</class>
    <resourceModel>personlization_mysql4</resourceModel>
  </personlization>
</models>
<events>        
    <catalog_product_load_after>
        <observers>
            <personlization>
                <type>model</type>
                <class>personlization/observer</class>
                <method>catalogProductLoadAfter</method>
            </personlization>
        </observers>
    </catalog_product_load_after>
    <sales_convert_quote_item_to_order_item>
        <observers>
            <personlization>
                <type>model</type>
                <class>personlization/observer</class>
                <method>salesConvertQuoteItemToOrderItem</method>
            </personlization>
        </observers>
    </sales_convert_quote_item_to_order_item>
</events>
</global>
</config> 

此外,您还需要使用以下代码创建观察Tech/Personlization/Model/Observer.php:

<?php
class Tech_Personlization_Model_Observer {
public function catalogProductLoadAfter(Varien_Event_Observer $observer)
{
    $action = Mage::app()->getFrontController()->getAction();

    if ($action->getFullActionName() == 'checkout_cart_add' || $action->getFullActionName() == 'checkout_cart_updateItemOptions'){          
        $product = $observer->getProduct();
        $post = Mage::app()->getRequest()->getPost();                   

                $array = array();
                $array['intials']  = $post['intials'];

                if ($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,
                            );
                        }

                    $observer->getProduct()->addCustomOption('additional_options', serialize($additionalOptions));
                }
    }
}

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

}

希望此帮助!

i am a newbie to magento. I have added a couple of custom options for a product that i added from the backend like name , address, email.

So it looks like this now :

Item 1

    Name : [textbox]
    Email : [textbox]
    Address : [textarea]

Now what i want to do is to dynamically add custom options. For that there will be 2 buttons, "Add an item" and "Remove an item".

So when i click "Add an item" the result should look like this:

Item 1

    Name : [textbox]
    Email : [textbox]
    Address : [textarea]

Item 2

    Name : [textbox]
    Email : [textbox]
    Address : [textarea]

When i click "Remove an item" it should remove an item.

How can i implement this? Thanks

解决方案

If It is completely Dynamic you can create a form on product detail page For Adding and Removing Item you need to create using jQuery.

For Managing in order if you add this data in quote then it will be managed in whole order.

So for adding Dynamically created option you need to create a module.

I have created a module Personlization with namespace Tech that will add intials Custom option if you have this input box avialable on product details page form.

So before creating a module add new form element in Product detail page Named intials.

In Tech/Personlization/etc/config.xml File you need to code as follows:

<?xml version="1.0"?>
<config>
<modules>
<Tech_Personlization>
  <version>1.0.0</version>
</Tech_Personlization>
</modules>
<global>
  <models>
  <personlization>
    <class>Tech_Personlization_Model</class>
    <resourceModel>personlization_mysql4</resourceModel>
  </personlization>
</models>
<events>        
    <catalog_product_load_after>
        <observers>
            <personlization>
                <type>model</type>
                <class>personlization/observer</class>
                <method>catalogProductLoadAfter</method>
            </personlization>
        </observers>
    </catalog_product_load_after>
    <sales_convert_quote_item_to_order_item>
        <observers>
            <personlization>
                <type>model</type>
                <class>personlization/observer</class>
                <method>salesConvertQuoteItemToOrderItem</method>
            </personlization>
        </observers>
    </sales_convert_quote_item_to_order_item>
</events>
</global>
</config> 

Also you need to create a observe Tech/Personlization/Model/Observer.php with the following code:

<?php
class Tech_Personlization_Model_Observer {
public function catalogProductLoadAfter(Varien_Event_Observer $observer)
{
    $action = Mage::app()->getFrontController()->getAction();

    if ($action->getFullActionName() == 'checkout_cart_add' || $action->getFullActionName() == 'checkout_cart_updateItemOptions'){          
        $product = $observer->getProduct();
        $post = Mage::app()->getRequest()->getPost();                   

                $array = array();
                $array['intials']  = $post['intials'];

                if ($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,
                            );
                        }

                    $observer->getProduct()->addCustomOption('additional_options', serialize($additionalOptions));
                }
    }
}

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

}

Hope this Help !!

这篇关于动态添加产品自定义选项magento的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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