magento 1.9.2.2 添加到购物车,自定义选项图像不起作用

magento 1.9.2.2 add to cart with custom option image not working

我有一个脚本,可以在购物车中添加一个带有自定义选项图像的产品,它在 CE 1.9.2.1 之前一直运行良好,但是在升级到最新版本之后,出现异常 Please specify the product's required option(s).

下面是代码,如果新版本需要更改某些内容,请指导我。

 <?php
    $productId = xxx;
    $image = 'path to image(tested image exists)';
    $product = Mage::getModel('catalog/product')->load($product_id);
    $cart = Mage::getModel('checkout/cart');
    $cart->init();
    $params = array(
        'product' => $productId,
        'qty' => 1,
        'options' => array(
            $optionId3inmycase => array(
                'type' => 'image/tiff',
                'title' => $image,
                'quote_path' => '/media/custom/' . $image,
                'order_path' => '/media/custom/' . $image,
                'fullpath' => Mage::getBaseDir() . '/media/custom/' . $image,
                'secret_key' => substr(md5(file_get_contents(Mage::getBaseDir() . '/media/custom/' . $image)), 0, 20)),
        )
    );
    $request = new Varien_Object();
    $request->setData($params);
    $cart->addProduct($product, $request);
    $cart->save();
    if ($itemId > 0) {
        $cartHelper = Mage::helper('checkout/cart');
        $cartHelper->getCart()->removeItem($itemId)->save();
    }
    Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
    $this->_redirect('checkout/cart/');
 ?>

一个快速的解决方案是重写 Mage_Catalog_Model_Product_Option_Type_File class 并更改 validateUserValue() 函数。

129行左右,替换

    $fileInfo = $this->_getCurrentConfigFileInfo();

    $fileInfo = null;
    if (isset($values[$option->getId()]) && is_array($values[$option->getId()])) {
        // Legacy style, file info comes in array with option id index
        $fileInfo = $values[$option->getId()];
    } else {
        /*
         * New recommended style - file info comes in request processing parameters and we
         * sure that this file info originates from Magento, not from manually formed POST request
         */
        $fileInfo = $this->_getCurrentConfigFileInfo();
    }

但它的旧代码会有 APPSEC-1079 安全问题。

并按订单详细信息下载此上传图片等。在同一型号中添加此功能 class。

/**
 * changed the image save address as we are saving image in custom
 * Main Destination directory
 *
 * @param boolean $relative If true - returns relative path to the webroot
 * @return string
 */
public function getTargetDir($relative = false)
{
    $fullPath = Mage::getBaseDir('media') . DS . 'custom';
    return $relative ? str_replace(Mage::getBaseDir(), '', $fullPath) : $fullPath;
}