我有一個腳本,可以創建一個可配置的產品和與之關聯的簡單產品。創建后,在后端,所有這些看起來都很好(股票、網站、狀態、可見性和簡單產品和可配置之間的關聯都可以)。問題是當我嘗試搜索可配置產品或將其添加到類別時,它不會顯示。
所有產品(可配置和簡單)首先使用此方法創建:
private function createBaseProduct($sku)
{
$_product = Mage::getModel('catalog/product');
$_product->setSku($sku);
$_product->setAttributeSetId(4);
$_product->setTypeId('simple');
$_product->setWebsiteIDs(array(1));
$_product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);
$_product->setStatus(1);
$_product->setTaxClassId(0);
$_product->setStockData(array(
'use_config_manage_stock' => 0,
'manage_stock' => 1,
'is_in_stock' => 1,
'qty' => 100
));
return $_product;
}
如果產品是可配置的,那么它會轉到此方法:
private function setData($configurable)
{
$configurable->setTypeId('configurable');
$configurable->setStockData(array(
'use_config_manage_stock' => 0,
'manage_stock' => 0,
'is_in_stock' => 1,
'qty' => 0,
));
--> $configurable = $this->setAssociativeAttributes($configurable);
$configurableAttributesData = $configurable->getTypeInstance()->getConfigurableAttributesAsArray();
$configurable->setCanSaveConfigurableAttributes(true);
$configurable->setConfigurableAttributesData($configurableAttributesData);
return $configurable;
}
該方法setAssociateAttributes()設定正在創建的可配置產品的屬性 ID:
private function setAssociativeAttributes()
{
$configurable->getTypeInstance()->setUsedProductAttributeIds($this->configurableAttrsIds);
return $configurable;
}
After that, the configurable product returned in setData() is saved using $product->save(). Then, the simple product's are created (using the createBaseProduct() method), saved, and assigned to the configurable product using this method:
public function associateChildProduct($configurableId, $childProduct)
{
$configurable = Mage::getModel('catalog/product')->load($configurableId);
$childProducts = $configurable->getTypeInstance()->getUsedProducts();
array_push($childProducts, $childProduct);
$childProductsIds = array();
foreach($childProducts as $product) {
array_push($childProductsIds, $product->getId());
}
Mage::getResourceSingleton('catalog/product_type_configurable')
->saveProducts($configurable, $childProductsIds);
}
And all seems good, products are created and correctly assigned to configurable. But in the frontend the configurable product isn't displayed (only if I access it via URL it opens correctly, with the variations and all). Obs.: simple products are displayed in search correctly, only the configurable is missing.
I believe there's something wrong in the configurable's data, but I can't figure it out :(
----- EDIT -----
So I debugged it a little more and seems that the problem is actually in the attribute used to create the configurable product (I'm also creating this attribute programmatically). If I save the attribute created programatically again, in my admin panel, the link between simple products and their configurable disapear (they don't show linked in the admin panel anymore).The attribute is created before the configurable product using class "CustomAttribute":
public function __construct($attrCode)
{
$attrData = array(
'group' => '',
'type' => 'varchar',
'backend' => '',
'frontend' => '',
'label' => ucfirst($attrCode),
'input' => 'select',
'class' => '',
'source' => '',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => true,
'default' => '0',
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'is_configurable' => true,
'unique' => false,
);
$installer = new Mage_Eav_Model_Entity_Setup('core_setup');
$installer->startSetup();
$installer->addAttribute(Mage_Catalog_Model_Product::ENTITY, $attrCode, $attributeData);
$this->addAttributeToDefaultSet();
$installer->endSetup();
}
And this attribute is set to child products (during their creation) using the following method:
private function setCustomAttribute($chidlProduct, $attrCode, $optionLabel)
{
$customAttribute = new CustomAttribute($attrCode);
$customAttribute->addOptionIfNotExists($optionLabel, $attrCode);
$optionId = $customAttribute->getOptionId($optionLabel);
$product->setData($attrCode, $optionId);
}
Where the method $customAttribute->addOptionIfNotExists() creates the attribute's option if not already created:
public function addOptionIfNotExists($optionLabel, $attrCode)
{
$value['option'] = array($optionLabel);
$order['option'] = 0;
$optionData = array(
'value' => $value,
'order' => $order
);
$attribute = Mage::getSingleton('eav/config')
->getAttribute(Mage_Catalog_Model_Product::ENTITY, $attrCode);
$attribute->setData('option', $optionData);
$attribute->save();
}
And after added the method associateChildProduct() is called to associate simple products to their configurable.
Obs.: configurable product is displayed correctly via URL, even the attribute variations are shown. Obs2.: if I save the attribute via admin panel, delete simple products and create new ones using "Quick create" (with the same values) the configurable product is displayed in search and categories.
uj5u.com熱心網友回復:
只是以答案的形式發表我的評論,并組織它......
最近創建的產品或以編程方式創建的產品沒有顯示在某些集合中的原因有很多。
指數
第一個也是最常見的一個與索引有關。
這可能與 cronjob 中的問題(未配置或無法正常作業)有關。
您可以通過執行以下操作手動觸發重新索引程序:
Magento 1 : php shell/indexer.php reindexall
Magento 2:bin/magento indexer:reindex
產品未添加到網站或不可見
檢查產品是否已啟用并在目錄中可見。如果它們是可配置產品中的簡單產品,請確保它們都已啟用。還要檢查物品是否有足夠的庫存,以及它們是否沒有標記為“缺貨”(無論庫存數量)。
在多網站商店中,檢查產品編輯頁面中的“網站”組,看看它是否被選中顯示在該網站中。還要檢查產品范圍。有時您會在內部級別(即商店視圖或網站)禁用產品。
某些條件不滿足
如果您認為您已經檢查了所有內容,那么現在該除錯集合了。
Magento 1 和 Magento 2getSelect()在集合物件中有可用的方法。
找到你的產品被回圈的phtml或者block ,找到collection變數(一般用在foreach)。
然后,添加類似echo (string)$collection->getSelect().
這將顯示用于搜索產品的查詢。查看您丟失的產品不滿足哪個join或條件。這是 Magento 1
中類別集合查詢的示例:where
SELECT `e`.*,
`cat_index`.`position` AS `cat_index_position`,
`price_index`.`price`,
`price_index`.`tax_class_id`,
`price_index`.`final_price`,
IF(price_index.tier_price IS NOT NULL, LEAST(price_index.min_price, price_index.tier_price),
price_index.min_price) AS `minimal_price`,
`price_index`.`min_price`,
`price_index`.`max_price`,
`price_index`.`tier_price`
FROM `catalog_product_entity` AS `e`
INNER JOIN `catalog_category_product_index` AS `cat_index`
ON cat_index.product_id = e.entity_id AND cat_index.store_id = 4 AND
cat_index.visibility IN (2, 4) AND cat_index.category_id = '10'
INNER JOIN `catalog_product_index_price` AS `price_index`
ON price_index.entity_id = e.entity_id AND price_index.website_id = '4' AND
price_index.customer_group_id = 0
ORDER BY `cat_index`.`position` ASC
LIMIT 12
來自 Magento 1 樣本資料的1 個“新來者”類別。
uj5u.com熱心網友回復:
在Ricardo Martins的幫助下,我能夠除錯問題并發現可配置產品沒有被顯示,因為它entity_id沒有被添加到表中catalog_product_index_price。這個問題似乎是因為在可配置產品的創建中使用的屬性backend_type設定為varchar, 而不是int( Reference )。
因此,我將用于創建屬性的代碼更改為:
public function __construct($attrCode)
{
$attrData = array(
'group' => '',
--> 'type' => 'int',
'backend' => '',
'frontend' => '',
'label' => ucfirst($attrCode),
'input' => 'select',
'class' => '',
'source' => '',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => true,
'default' => '0',
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'is_configurable' => true,
'unique' => false,
);
$installer = new Mage_Eav_Model_Entity_Setup('core_setup');
$installer->startSetup();
$installer->addAttribute(Mage_Catalog_Model_Product::ENTITY, $attrCode, $attributeData);
$this->addAttributeToDefaultSet();
$installer->endSetup();
}
在指示的行中,屬性backend_type設定為int. 在此之后,可配置產品現在可以正確顯示在類別和搜索中:)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/443631.html
