将自定义图像分配给程序创建的产品

时间:2017-04-20 10:00:09

标签: 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
    }

1 个答案:

答案 0 :(得分:0)

您需要替换代码:

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;
}

使用此代码:

$path = '';
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;
    }