这几天我一直在为这个问题苦苦挣扎。我继承了一个建立在 Magento 上的网站。这个网站销售的产品都可以定制,每个产品都是按订单生产的。当客户想要将特定徽标添加到产品中,然后选择他们想要缝合徽标的线色时,就会出现我的问题。最多有 15 个徽标可用,每个徽标都有自己的一组可用线色。假设客户选择了 logo #6,然后从 logo #6 的可用颜色中选择一个线程并将其添加到购物车。当项目被引用时,它会在购物车中显示“线程颜色 6”以获取线程选项。这很好,订单处理正确。但是,当您在管理员端查看订单时,它将显示所有线色选项,包括他们选择的线色选项。
线色 1:无线
线色 2:无线
线色 3:无线
线色 4:无线
线色 5:线色名称
线色 6:无线
如上所述,订单处理正确在查看订单时让客户服务部门感到困惑。
所以我试图做的是找到设置了值的线色,然后将它的标签更改为像“线色”这样的通用标签,然后从报价中动态删除所有其他线色选项。我在这里找到了一个可能的解决方案,并且 Vinai 在解释代码方面做得很好。我已经尝试过了,甚至逐行逐行执行,我知道它正在根据需要添加和删除选项。但是,当我在调试后删除任何 die() 语句时,会出现 SQL 错误。下面是我的配置、观察者和 SQL 错误。
配置文件
<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>
观察者.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}
任何帮助将不胜感激。谢谢你。