以编程方式创建的产品未上传图片
Image not uploading for programatically created product
我正在尝试通过以下代码以编程方式创建产品,一切正常,但如果访客创建产品,则自定义图片无法上传
谁能告诉我正确的方法?
protected function _thisProduct($type, $doSave=true, $originalProduct, $newImagePath="")
{
// code for Guest
$session = Mage::getSingleton('customer/session');
if ($session->isLoggedIn()) {
return;
}
$result = array(
'success' => false
);
if ($this->getRequest()->isPost()) {
$login = $this->getRequest()->getPost('login');
$productId=$this->getRequest()->getPost('product_id');
// echo $login['product_id'];die;
if (!empty($login['username']) && !empty($login['password'])) {
try {
$session->login($login['username'], $login['password']);
$result['redirect'] = $this->_getRefererUrl() ? $this->_getRefererUrl() : Mage::getUrl('customer/account', array('_secure' => true));
$result['success'] = true;
$customerId = Mage::getSingleton('customer/session')->getCustomerId();
// code for guest end
$product = Mage::getModel('catalog/product');
// product images
$images = array(
'thumbnail' => 'image.png', // displaying under cart page
//'small_image' => 'image.png',
'image' => 'image.png', // displaying under my design
);
foreach ($images as $imageType => $imageFileName) {
if($newImagePath != ""){
$dir = Mage::getBaseDir('media') . DS . 'one_two_three/quote/';
$path = $dir . $newImagePath;
}else{
$dir = Mage::getBaseDir('media') . DS . 'example/super/';
$path = $dir . $imageFileName;
}
//echo $path."<br>";
if (file_exists($path)) {
try {
$product->addImageToMediaGallery($path, $imageType, false);
} catch (Exception $e) {
echo $e->getMessage();
}
} else {
echo "No image path found dear: `{$path}`<br/>";
}
}
$customerId = Mage::getSingleton('customer/session')->getCustomerId();
$product->setCreatedByCustomerId($customerId);
if ($doSave)
$product->save();
return $product;
// code for guest
} catch (Mage_Core_Exception $e) {
switch ($e->getCode()) {
case Mage_Customer_Model_Customer::EXCEPTION_EMAIL_NOT_CONFIRMED:
$message = Mage::helper('customer')->__('This account is not confirmed.username');
break;
case Mage_Customer_Model_Customer::EXCEPTION_INVALID_EMAIL_OR_PASSWORD:
$message = $e->getMessage();
break;
default:
$message = $e->getMessage();
}
$result['error'] = $message;
$session->setUsername($login['username']);
} catch (Exception $e) {
Mage::helper("ajaxlogin")->log("There has been an error during the login.");
// Mage::logException($e); // PA DSS violation: this exception log can disclose customer password
}
} else {
$result['error'] = Mage::helper('customer')->__('Login and password are required.');
}
}
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
// code for guest end
}
以下代码对注册用户正常工作:
protected function _thisProduct($type, $doSave=true, $originalProduct, $newImagePath="")
{
$product = Mage::getModel('catalog/product');
// product images
$images = array(
'thumbnail' => 'image.png', // displaying under cart page
//'small_image' => 'image.png',
'image' => 'image.png', // displaying under my design
);
foreach ($images as $imageType => $imageFileName) {
if($newImagePath != ""){
$dir = Mage::getBaseDir('media') . DS . 'one_two_three/quote/';
$path = $dir . $newImagePath;
}else{
$dir = Mage::getBaseDir('media') . DS . 'example/super/';
$path = $dir . $imageFileName;
}
//echo $path."<br>";
if (file_exists($path)) {
try {
$product->addImageToMediaGallery($path, $imageType, false);
} catch (Exception $e) {
echo $e->getMessage();
}
} else {
echo "No image path found dear: `{$path}`<br/>";
}
}
$customerId = Mage::getSingleton('customer/session')->getCustomerId();
$product->setCreatedByCustomerId($customerId);
if ($doSave)
$product->save();
return $product;
}
使用肥皂上传图片api
$base_url = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
$client = new SoapClient($base_url.'index.php/api/soap/?wsdl',array('trace' => 1,'connection_timeout' => 120));
$session = $client->login('admin','admin123');
function uploadImage($productId,$session,$client,$image)
{
// need to check on child image
$file_image = explode(".",basename(trim($image)));
$fileName = $file_image[0];
$file = array(
'content' => base64_encode(file_get_contents($image)),
'mime' => 'image/jpeg',
'name' => $fileName
);
$result = $client->call(
$session,
'catalog_product_attribute_media.create',
array(
$productId,
array('file'=>$file, 'label'=>'label', 'position'=>'100', 'types'=>array('small_image','image','thumbnail'), 'exclude'=>0)
)
);
}
我正在尝试通过以下代码以编程方式创建产品,一切正常,但如果访客创建产品,则自定义图片无法上传
谁能告诉我正确的方法?
protected function _thisProduct($type, $doSave=true, $originalProduct, $newImagePath="")
{
// code for Guest
$session = Mage::getSingleton('customer/session');
if ($session->isLoggedIn()) {
return;
}
$result = array(
'success' => false
);
if ($this->getRequest()->isPost()) {
$login = $this->getRequest()->getPost('login');
$productId=$this->getRequest()->getPost('product_id');
// echo $login['product_id'];die;
if (!empty($login['username']) && !empty($login['password'])) {
try {
$session->login($login['username'], $login['password']);
$result['redirect'] = $this->_getRefererUrl() ? $this->_getRefererUrl() : Mage::getUrl('customer/account', array('_secure' => true));
$result['success'] = true;
$customerId = Mage::getSingleton('customer/session')->getCustomerId();
// code for guest end
$product = Mage::getModel('catalog/product');
// product images
$images = array(
'thumbnail' => 'image.png', // displaying under cart page
//'small_image' => 'image.png',
'image' => 'image.png', // displaying under my design
);
foreach ($images as $imageType => $imageFileName) {
if($newImagePath != ""){
$dir = Mage::getBaseDir('media') . DS . 'one_two_three/quote/';
$path = $dir . $newImagePath;
}else{
$dir = Mage::getBaseDir('media') . DS . 'example/super/';
$path = $dir . $imageFileName;
}
//echo $path."<br>";
if (file_exists($path)) {
try {
$product->addImageToMediaGallery($path, $imageType, false);
} catch (Exception $e) {
echo $e->getMessage();
}
} else {
echo "No image path found dear: `{$path}`<br/>";
}
}
$customerId = Mage::getSingleton('customer/session')->getCustomerId();
$product->setCreatedByCustomerId($customerId);
if ($doSave)
$product->save();
return $product;
// code for guest
} catch (Mage_Core_Exception $e) {
switch ($e->getCode()) {
case Mage_Customer_Model_Customer::EXCEPTION_EMAIL_NOT_CONFIRMED:
$message = Mage::helper('customer')->__('This account is not confirmed.username');
break;
case Mage_Customer_Model_Customer::EXCEPTION_INVALID_EMAIL_OR_PASSWORD:
$message = $e->getMessage();
break;
default:
$message = $e->getMessage();
}
$result['error'] = $message;
$session->setUsername($login['username']);
} catch (Exception $e) {
Mage::helper("ajaxlogin")->log("There has been an error during the login.");
// Mage::logException($e); // PA DSS violation: this exception log can disclose customer password
}
} else {
$result['error'] = Mage::helper('customer')->__('Login and password are required.');
}
}
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
// code for guest end
}
以下代码对注册用户正常工作:
protected function _thisProduct($type, $doSave=true, $originalProduct, $newImagePath="")
{
$product = Mage::getModel('catalog/product');
// product images
$images = array(
'thumbnail' => 'image.png', // displaying under cart page
//'small_image' => 'image.png',
'image' => 'image.png', // displaying under my design
);
foreach ($images as $imageType => $imageFileName) {
if($newImagePath != ""){
$dir = Mage::getBaseDir('media') . DS . 'one_two_three/quote/';
$path = $dir . $newImagePath;
}else{
$dir = Mage::getBaseDir('media') . DS . 'example/super/';
$path = $dir . $imageFileName;
}
//echo $path."<br>";
if (file_exists($path)) {
try {
$product->addImageToMediaGallery($path, $imageType, false);
} catch (Exception $e) {
echo $e->getMessage();
}
} else {
echo "No image path found dear: `{$path}`<br/>";
}
}
$customerId = Mage::getSingleton('customer/session')->getCustomerId();
$product->setCreatedByCustomerId($customerId);
if ($doSave)
$product->save();
return $product;
}
使用肥皂上传图片api
$base_url = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
$client = new SoapClient($base_url.'index.php/api/soap/?wsdl',array('trace' => 1,'connection_timeout' => 120));
$session = $client->login('admin','admin123');
function uploadImage($productId,$session,$client,$image)
{
// need to check on child image
$file_image = explode(".",basename(trim($image)));
$fileName = $file_image[0];
$file = array(
'content' => base64_encode(file_get_contents($image)),
'mime' => 'image/jpeg',
'name' => $fileName
);
$result = $client->call(
$session,
'catalog_product_attribute_media.create',
array(
$productId,
array('file'=>$file, 'label'=>'label', 'position'=>'100', 'types'=>array('small_image','image','thumbnail'), 'exclude'=>0)
)
);
}