如果我设置了重复的SKU,Magento没有显示错误

时间:2016-10-27 11:55:03

标签: php magento

我有一个要求,我必须从Magento代码(CodeIgniter项目)之外更新Magento产品。

一切都运行得很好但是,有一件事是错的,如果我保存2个具有相同SKU的产品,我的代码不会抛出异常。

如果我访问我的Magento网站的管理部分并尝试为任何产品设置重复的SKU,那么它确实显示错误,即SKU无法复制。

但为什么以下代码允许我设置重复的SKU?我这里只发布相关代码。

   try {
        $url = preg_replace('#[^0-9a-z]+#i', '-', $product_name);
        $url = strtolower($url);

        $simpleProduct
                ->setWebsiteIds(array(1)) //website ID the product is assigned to, as an array
                ->setAttributeSetId(20) //ID of a attribute set named 'default'
                ->setTypeId('simple') //product type
                ->setCreatedAt(strtotime('now')) //product creation time
                ->setSku($_POST['sku']) //SKU
                ->setName($_POST['product_name']) //product name
                ->setUrlKey($_POST['url_key'])
                ->setBrand($_POST['brand'])->setType($_POST['type'])
                ->setStatus($_POST['status'] == 1 ? 1 : 2) //product status (1 - enabled, 2 - disabled)
                ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH) //catalog and search visibility
                ->setNewsFromDate(strtotime('now')) //product set as new from
                ->setNewsToDate(strtotime('+1 week')) //product set as new to
                ->setPrice($_POST['price']) //price in form 11.22
                ->setCost($_POST['price']) //price in form 11.22
                ->setSpecialPrice($_POST['discount_price'])

                ->setMetaTitle($_POST['meta_title'])
                ->setMetaKeyword($_POST['meta_keyword'])
                ->setMetaDescription($_POST['meta_description'])
                ->setDescription($_POST['description'])
                ->setShortDescription($_POST['short_description'])
                ->setStockData(array(
                    'use_config_manage_stock' => 0, //'Use config settings' checkbox
                    'manage_stock' => 1, //manage stock
                    'min_sale_qty' => 1, //Minimum Qty Allowed in Shopping Cart
                    'max_sale_qty' => $_POST['quantity'], //Maximum Qty Allowed in Shopping Cart
                    'is_in_stock' => $_POST['quantity'] == 0 ? 0 : 1, //Stock Availability
                    'qty' => $_POST['quantity'] //qty
                        )
                )
                ->setCategoryIds(unserialize($_POST['product_groups_category_map'])); //assign product to categories

        Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

        $simpleProduct->save($simpleProduct);

        if (!empty($_POST['product_groups_images'])) {
            if ($_POST['id_in_magento'] != 0) {
                $mediaApi = Mage::getModel("catalog/product_attribute_media_api");
                $mediaApiItems = $mediaApi->items($simpleProduct->getId());

                foreach ($mediaApiItems as $item) {
                    $datatemp = $mediaApi->remove($simpleProduct->getId(), $item['file']);
                }
            }

            $simpleProduct->load($_POST['id_in_magento']);
            $simpleProduct->setMetaTitle($_POST['meta_title']);

            foreach (unserialize($_POST['product_groups_images']) as $img) {
                $simpleProduct->addImageToMediaGallery($img, array('image', 'small_image', 'thumbnail'), false, false);
            }
        }
        Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

        $simpleProduct->save($simpleProduct);

        $return['magento_id'] = $simpleProduct->getId();
        $return['url_key'] = $simpleProduct->getUrlKey();
    } catch (Exception $e) {
        Mage::log($e->getMessage());
        $return['error'] = $e->getMessage();
    }

代码有什么问题?

PS:

我再说一遍,如果我尝试从Magento网站的后端设置重复的SKU,则会显示SKU无法重复的正常错误。

1 个答案:

答案 0 :(得分:2)

您可以在添加新产品之前检查 sku ,它将解决您的问题

$sku = 'sku_here';
$p_id = Mage::getModel('catalog/product')->getIdBySku($sku);
if ($p_id) {
   //sku exists
}
else {
   //sku does not exist
}