如何将项目添加到Magento中的购物车时动态自定义选项?

时间:2012-10-18 14:16:47

标签: php sql magento

我几天来一直在努力解决这个问题。我继承了一个建立在Magento上的网站。该网站销售的产品可以全部定制,每个产品都是按订单生产的。当客户想要将特定徽标添加到产品中,然后选择他们希望将徽标拼接的线色时,就会出现问题。最多可提供15个徽标,每个徽标都有自己的可用线色。让我们说客户选择徽标#6,然后从徽标#6的可用颜色中选择一个线程并将其添加到购物车中。当引用该项时,它会在购物车中显示“线程颜色6”以获取线程选项。这很好,订单处理正确。但是,当您在管理员端查看订单时,它将显示所有线程颜色选项,包括他们选择的那个。因此,如果该项目有6个徽标选项,并且他们选择徽标5作为选项,则在管理员方面它显示如下:
螺纹颜色1:无螺纹
螺纹颜色2:无螺纹
螺纹颜色3:无螺纹
螺纹颜色4:无螺纹
螺纹颜色5:螺纹颜色名称
螺纹颜色6:无螺纹

就像我上面说的那样,订单处理正确,在查看订单时,客户服务部门会感到困惑。

所以我试图做的是找到具有值集的线程颜色,然后将其标签更改为类似“线程颜色”的通用颜色,然后动态地从引用中删除所有其他线程颜色选项。我发现了一个possible solution here,Vinai在解释代码方面做得很好。我已经尝试了这一点,甚至逐行介绍,我知道它是根据需要添加和删除选项。但是,当我在调试后删除任何die()语句时,我得到一个SQL错误。下面是我的配置,观察者和SQL错误。

config.xml中

<events>
    <checkout_cart_product_add_after>
            <observers>
                    <customoptions>
                        <type>singleton</type>
                        <class>Lmc_Threadcolor_Model_Product_Observer</class>
                        <method>checkoutCartProductAddAfter</method>
                    </customoptions>
            </observers>
    </checkout_cart_product_add_after>
    <sales_convert_quote_item_to_order_item>
            <observers>
                    <customoptions>
                        <type>singleton</type>
                        <class>Lmc_Threadcolor_Model_Product_Observer</class>
                        <method>salesConvertQuoteItemToOrderItem</method>
                    </customoptions>
            </observers>
    </sales_convert_quote_item_to_order_item>
</events>

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

public function checkoutCartProductAddAfter(Varien_Event_Observer $observer)
{
    $item = $observer->getQuoteItem();
    $infoArr = array();

    if ($info = $item->getProduct()->getCustomOption('info_buyRequest')) 
    {
        $infoArr = unserialize($info->getValue());
    }

    // Set additional options in case of a reorder
    if ($infoArr && isset($infoArr['additional_options']))
    {
        // An additional options array is set on the buy request - this is a reorder
        $item->addOption(array(
            'code' => 'additional_options',
            'value' => serialize($infoArr['additional_options'])
        ));
        return;
    }

    $options = Mage::helper('catalog/product_configuration')->getCustomOptions($item);

    foreach ($options as $option)
    {
        // The only way to identify a custom option without
        // hardcoding ID's is the label :-(
        // But manipulating options this way is hackish anyway

        if(preg_match('/select thread color/', strtolower($option['label'])) && strtolower($option['value']) != 'no thread')
        {
            $optId = $option['option_id'];

            // Add replacement custom option with modified value
            $additionalOptions = array(array(
                'code' => 'lmc_custom_thread',
                'label' => 'Thread Color',
                'value' => $option['value'],
                'print_value' => $option['print_value'],
                'option_id' => $option['option_id'],
                'option_type' => $option['option_type'],
                'custom_view' => '',
                'image_path' => $option['image_path'],
            ));
            $item->addOption(array(
                'code' => 'additional_options',
                'value' => serialize($additionalOptions),
            ));

            // Update info_buyRequest to reflect changes
            if ($infoArr &&isset($infoArr['options']) && isset($infoArr['options'][$optId]))
            {
                // Remove real custom option
                unset($infoArr['options'][$optId]);

                // Add replacement additional option for reorder (see above)
                $infoArr['additional_options'] = $additionalOptions;

                $info->setValue(serialize($infoArr));
                $item->addOption($info);
            }

            // Remove real custom option id from option_ids list
            if ($optionIdsOption = $item->getProduct()->getCustomOption('option_ids'))
            {

                $optionIds = explode(',', $optionIdsOption->getValue());
                if (false !== ($idx = array_search($optId, $optionIds)))
                {
                    unset($optionIds[$idx]);
                    $optionIdsOption->setValue(implode(',', $optionIds));
                    $item->addOption($optionIdsOption);
                }
            }

            // Remove real custom option
            $item->removeOption('option_' . $optId);
        }
    }
}

SQL错误

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':1:{i:0;a:8:{s:4:"code";s:17:"lmc_custom_thread";s:5:"label";s:12:"Thread Color"' at line 1

Trace:
#0 /var/www/html/lib/Zend/Db/Statement.php(300): Zend_Db_Statement_Pdo->_execute(Array)
#1 /var/www/html/lib/Zend/Db/Adapter/Abstract.php(479): Zend_Db_Statement->execute(Array)
#2 /var/www/html/lib/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_Adapter_Abstract->query('SELECT * FROM `...', Array)
#3 /var/www/html/lib/Varien/Db/Adapter/Pdo/Mysql.php(333): Zend_Db_Adapter_Pdo_Abstract->query('SELECT * FROM `...', Array)
#4 /var/www/html/lib/Zend/Db/Adapter/Abstract.php(753): Varien_Db_Adapter_Pdo_Mysql->query('SELECT * FROM `...', Array)
#5 /var/www/html/app/code/local/Mage/Sales/Model/Quote/Item.php(281): Zend_Db_Adapter_Abstract->fetchRow('SELECT * FROM `...')
#6 /var/www/html/app/code/local/Deg/Sales/Model/Quote/Item.php(17): Mage_Sales_Model_Quote_Item->setProduct(Object(Mage_Catalog_Model_Product))
#7 /var/www/html/app/code/core/Mage/Sales/Model/Mysql4/Quote/Item/Collection.php(224): Deg_Sales_Model_Quote_Item->setProduct(Object(Mage_Catalog_Model_Product))
#8 /var/www/html/app/code/core/Mage/Sales/Model/Mysql4/Quote/Item/Collection.php(136): Mage_Sales_Model_Mysql4_Quote_Item_Collection->_assignProducts()
#9 /var/www/html/lib/Varien/Data/Collection/Db.php(632): Mage_Sales_Model_Mysql4_Quote_Item_Collection->_afterLoad()
#10 /var/www/html/lib/Varien/Data/Collection.php(729): Varien_Data_Collection_Db->load()
#11 /var/www/html/app/code/core/Mage/Sales/Model/Quote.php(589): Varien_Data_Collection->getIterator()
#12 /var/www/html/app/code/core/Mage/Sales/Model/Quote.php(620): Mage_Sales_Model_Quote->getAllItems()
#13 /var/www/html/app/code/core/Mage/Checkout/Model/Cart.php(123): Mage_Sales_Model_Quote->hasItems()
#14 /var/www/html/app/code/core/Mage/Checkout/controllers/CartController.php(123): Mage_Checkout_Model_Cart->init()
#15 /var/www/html/app/code/core/Mage/Core/Controller/Varien/Action.php(418): Mage_Checkout_CartController->indexAction()
#16 /var/www/html/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php(253): Mage_Core_Controller_Varien_Action->dispatch('index')
#17 /var/www/html/app/code/core/Mage/Core/Controller/Varien/Front.php(176): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#18 /var/www/html/app/code/core/Mage/Core/Model/App.php(340): Mage_Core_Controller_Varien_Front->dispatch()
#19 /var/www/html/app/Mage.php(627): Mage_Core_Model_App->run(Array)
#20 /var/www/html/index.php(80): Mage::run('', 'store')
#21 {main}

非常感谢任何帮助。谢谢。

0 个答案:

没有答案