如何添加产品图片,以便以编程方式创建捆绑产品
-
12-12-2019 - |
题
我需要创建正在创建捆绑产品的函数,我已经创建了我的功能,它的工作原理很好,我的意思是它为我创建捆绑产品,我的问题是我如何添加产品的图像,(图像,小图像和缩略图),我做了一些搜索找不到任何好方法实现它,如果有人可以帮助,谢谢:)
以下是创建捆绑产品的代码:
$productsId = array(6,8);
$categoryId = array();
$sum = 0;
$skuSum = 0;
$ProductsName = array();
for($i=0,$j=count($productsId);$i<$j;$i++)
{
$productCheck = Mage::getModel('catalog/product');
$categoryId[] = $productCheck->load($productsId[$i])->getCategoryIds();
$productPrice = $productCheck->load($productsId[$i])->getPrice();
$productSku = $productCheck->load($productsId[$i])->getSku();
$ProductsName[] = $productCheck->load($productsId[$i])->getName();
$sum += $productPrice;
$skuSum += $productSku;
}
$bundleProductName = implode('-', $ProductsName);
$categoryIDs = call_user_func_array('array_merge', $categoryId);
$uniqueCategoryIDs = array_unique($categoryIDs);
require_once('includes/config.php');
require_once('app/Mage.php');
$storeID = 1;
$websiteIDs = array(1);
//$cats = array(8,11,9);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$p = array(
'sku_type' => 1,
'sku' => $skuSum,
'name' => $bundleProductName,
'description' => 'Pendant chian created by user',
'short_description' => 'Pendant chian created by user',
'type_id' => 'bundle',
'attribute_set_id' => 4,
'weight_type' => 0,
'visibility' => 4,
'price_type' => 1,
'price_view' => 0,
'status' => 1,
'created_at' => strtotime('now'),
'category_ids' => $uniqueCategoryIDs,
'store_id' => $storeID,
'website_ids' => $websiteIDs,
'price' => $sum
);
$productCheck->setData($p);
Mage::register('product', $productCheck);
$optionRawData = array();
$optionRawData[0] = array(
'required' => 1,
'option_id' => '',
'position' => 0,
'type' => 'multi',
'title' => 'pendant chain',
'default_title' => 'pendantChain',
'delete' => '',
);
$selectionRawData = array();
for($i=0,$j=count($productsId);$i<$j;$i++)
{
$selectionRawData[0][] = array(
'product_id' => $productsId[$i],
'selection_qty' => 1,
'selection_can_change_qty' => 1,
'position' => 0,
'is_default' => 1,
'selection_id' => '',
'selection_price_type' => 0,
'selection_price_value' => 0.0,
'option_id' => '',
'delete' => ''
);
}
/*$selectionRawData[0] = array();
$selectionRawData[0][] = array(
'product_id' => 7,
'selection_qty' => 1,
'selection_can_change_qty' => 1,
'position' => 0,
'is_default' => 1,
'selection_id' => '',
'selection_price_type' => 0,
'selection_price_value' => 0.0,
'option_id' => '',
'delete' => ''
);*/
Mage::register('productCheck', $productCheck);
Mage::register('current_product', $productCheck);
$productCheck->setCanSaveConfigurableAttributes(false);
$productCheck->setCanSaveCustomOptions(true);
// Set the Bundle Options & Selection Data
$productCheck->setBundleOptionsData($optionRawData);
$productCheck->setBundleSelectionsData($selectionRawData);
$productCheck->setCanSaveBundleSelections(true);
$productCheck->setAffectBundleProductSelections(true);
$productCheck->save();
if($productCheck->save()){
return true;
}
else
return false;
. 解决方案
使用此代码保存捆绑产品的图片
$filepath
是您的图片
$bundleProduct->addImageToMediaGallery($filePath, array('image', 'small_image', 'thumbnail'), false, false);
.
希望这有帮助,