将自定义图像分配给以编程方式创建的产品-Magento 1.8

时间:2017-04-27 09:14:35

标签: magento

一旦访客用户点击“添加到购物车1 ”按钮,我们就会做以下事情:

1]我们以编程方式创建简单的产品

2]我们正在为该产品分配自定义图像。

3]点击“登录”按钮

,再显示一个登录站点的弹出窗口

一旦用户点击“登录按钮,我们就会做以下事情:

1]我们正在以编程方式创建简单的产品

2]我们没有为该产品分配自定义图像。

但是我们需要为该产品分配自定义图像。

public function thisSimpleProductAndRedirectAction()
    {
        $originalProductId = $this->getRequest()->getParam("id");
        // $productNameArray = explode(" - ",$this->getRequest()->getParam("id"));
        // $originalProductId = $productNameArray[0];
        $newImagePath      = $this->getRequest()->getParam("image");
        $originalProduct   = Mage::getModel('catalog/product')->setStoreId(Mage::app()->getStore()->getId())->load($originalProductId);

        if ($product = $this->_createProduct(Mage_Catalog_Model_Product_Type::TYPE_SIMPLE, true, $originalProduct, $newImagePath)) {
            $this->getResponse()->setBody(Mage::helper('core')->jsonEncode("custom image saving"));
        }
        else{
            $this->getResponse()->setBody(Mage::helper('core')->jsonEncode("custom image not saving"));
        }

    }

 protected function _thisImage($type, $doSave = true, $originalProduct, $newImagePath = "")
    {

        // code for Guest
        Mage::register('isSecureArea',true);    
        $session = Mage::getSingleton('customer/session');



        $result = array(
            'success' => false
        );

        $login ="";
        $productId ="";
        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 {
                    if(isset($login) && (is_array($login) && !empty($login)) || ($login!="")){
                        $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();
                    }
                    else{
                        $customerId = "";
                    }

                    // code for guest end        

                    $product->setName($originalProduct->getName());

                    // add images

                    $images = array(
                        'thumbnail' => 'image.png', // displaying under cart page

                        'image' => 'image.png' // displaying under my design
                    );

                    foreach ($images as $imageType => $imageFileName) {
                        if ($newImagePath != "") {
                            $dir  = Mage::getBaseDir('media') . DS . 'custom_product_preview/quote/';
                            $path = $dir . $newImagePath;
                        } else {
                            $dir  = Mage::getBaseDir('media') . DS . 'example/amasty/';
                            $path = $dir . $imageFileName;
                        }
                        //echo $path."<br>";
                        if (file_exists($path)) {
                            try {
                                $product->addImageToMediaGallery($path, $imageType, false);
                            }
                            catch (Exception $e) {
                                echo $e->getMessage();
                            }
                        } else {
                            echo "Can not find image by path: `{$path}`<br/>";
                        }
                    }                  

                    $emailimage = Mage::helper('catalog/image')->init($product, 'thumbnail');

                    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. <a href="%s">Click here</a> to resend confirmation email.', Mage::helper('customer')->getEmailConfirmationUrl($login['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) {
                    $result = "ERROR :".$e->getMesage();

        Mage::unregister('isSecureArea');
        $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
        // code for guest end
    }

更新了问题=&gt;

添加到cart1代码如下:

<button onclick="return setproductlogin(\'<?php echo 

Mage::registry("current_product")->getId()?>\', event);" 

 id="submit-editorApply-{{rand}}" >Add to cart1</button>
  

更新2:

点击add to cart1后,它会使用图片参数

重定向到link5

当我们点击login时,它会重定向到link1而没有图片参数,所以只有图像才能创建登录.....

1 个答案:

答案 0 :(得分:0)

请在media / import文件夹中上传产品图片

$importDir = Mage::getBaseDir('media') . DS . 'import/';
$img1=$importDir.'sample.jpg';
$img2=$importDir.'sample1.jpg';
$imgarray=array('sample.jpg','sample.jpg');
if(count($imgarray)>0){
    $simpleproductImg = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
    $simpleproductImg->setMediaGallery(array('images' => array(), 'values' => array()));
    foreach($imgarray as $imkey=>$imgpath){
        if($imkey==0){
            $simpleproductImg->addImageToMediaGallery($imgpath, array('image','thumbnail','small_image'), false, false);
        }else{
            $simpleproductImg->addImageToMediaGallery($imgpath, array(), false, false);
        }
    }
    $simpleproductImg->save();
}