Magento 未为编程创建的产品上载图像
我正在尝试下面的代码创建产品编程,一切都很好,但若客人创建产品,比自定义图像不上传 谁能给我指路吗Magento 未为编程创建的产品上载图像,magento,magento-1.8,Magento,Magento 1.8,我正在尝试下面的代码创建产品编程,一切都很好,但若客人创建产品,比自定义图像不上传 谁能给我指路吗 protected function _thisProduct($type, $doSave=true, $originalProduct, $newImagePath="") { // code for Guest $session = Mage::getSingleton('customer/session'); if ($session->isLogg
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\u thisProduct($type,$doSave=true,$originalProduct,$newImagePath=”“)
{
//客人代码
$session=Mage::getSingleton('customer/session');
如果($session->isLoggedIn()){
返回;
}
$result=数组(
“成功”=>错误
);
如果($this->getRequest()->isPost()){
$login=$this->getRequest()->getPost('login');
$productId=$this->getRequest()->getPost('product_id');
//echo$login['product_id'];die;
如果(!empty($login['username'])和(&!empty($login['password'])){
试一试{
$session->login($login['username'],$login['password']);
$result['redirect']=$this->\u getRefererUrl()?$this->\u getRefererUrl():Mage::getUrl('customer/account',array('u secure'=>true));
$result['success']=true;
$customerId=Mage::getSingleton('customer/session')->getCustomerId();
//来宾端代码
$product=Mage::getModel('catalog/product');
//产品形象
$images=数组(
'缩略图'=>'image.png',//显示在购物车页面下
//'small_image'=>'image.png',
'image'=>'image.png',//在我的设计下显示
);
foreach($imageType=>$imageFileName形式的图像){
如果($newImagePath!=“”){
$dir=Mage::getBaseDir('media').DS.'one'u two'u three/quote/';
$path=$dir.$newImagePath;
}否则{
$dir=Mage::getBaseDir('media').DS.'example/super/';
$path=$dir.$imageFileName;
}
//echo$path。“
”;
如果(文件_存在($path)){
试一试{
$product->addImageToMediaGallery($path,$imageType,false);
}捕获(例外$e){
echo$e->getMessage();
}
}否则{
echo“未找到映像路径亲爱的:`{$path}`
”;
}
}
$customerId=Mage::getSingleton('customer/session')->getCustomerId();
$product->setCreatedByCustomerId($customerId);
如果($doSave)
$product->save();
退回$product;
//客人代码
}捕获(法师核心例外$e){
开关($e->getCode()){
案例经理\客户\型号\客户::例外情况\电子邮件\未确认:
$message=Mage::helper('customer')->__;('This account not confirmation.username');
打破
案例图像\u客户\u型号\u客户::异常\u无效\u电子邮件\u或\u密码:
$message=$e->getMessage();
打破
违约:
$message=$e->getMessage();
}
$result['error']=$message;
$session->setUsername($login['username']);
}捕获(例外$e){
Mage::helper(“ajaxlogin”)->log(“登录过程中出现错误”);
//Mage::logException($e);//PA DSS冲突:此异常日志可能会泄露客户密码
}
}否则{
$result['error']=Mage::helper('customer')->(需要登录名和密码);
}
}
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
//来宾端代码
}
以下代码适用于注册用户:
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;
}
protected function\u thisProduct($type,$doSave=true,$originalProduct,$newImagePath=”“)
{
$product=Mage::getModel('catalog/product');
//产品形象
$images=数组(
'缩略图'=>'image.png',//显示在购物车页面下
//'small_image'=>'image.png',
'image'=>'image.png',//在我的设计下显示
);
foreach($imageType=>$imageFileName形式的图像){
如果($newImagePath!=“”){
$dir=Mage::getBaseDir('media').DS.'one'u two'u three/quote/';
$path=$dir.$newImagePath;
}否则{
$dir=Mage::getBaseDir('media').DS.'example/super/';
$path=$dir.$imageFileName;
}
//echo$path。“
”;
如果(文件_存在($path)){
试一试{
$product->addImageToMediaGallery($path,$imageType,false);
}捕获(例外$e){
echo$e->getMessage();
}
}否则{
echo“未找到映像路径亲爱的:`{$path}`
”;
}
}
$customerId=Mage::getSingleton('customer/session')->getCustomerId();
$product->setCreatedByCustomerId($customerId);
如果($doSave)
$product->save();
退回$product;
}
使用soap 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)
)
);
}
日志中有哪些错误?您是否已尝试调试代码以查看问题所在?@lxx似乎
$newImagePath
未发送任何值。。。。。您能帮我解决这个问题吗…..再添加一些日志记录来检查newimagepath或imagepath是否正确或与您假设的相同吗?@lxx我不太擅长编码,我会照您所说的那样尝试。。。但是如果你能帮我写一些代码,那就太好了……你需要了解一下