请指定产品所需的选项“自定义选项”类型文件


8

我只有一个自定义选项,即文件类型,我试图通过编程方式将产品添加到购物车,如下所示:

$logoSku = 'lg-brnd01';
$productLogo = Mage::getModel('catalog/product')->loadByAttribute('sku',$logoSku);
$logoOptions = $productLogo->getOptions();
$opts = Mage::getSingleton('catalog/product_option')->getProductOptionCollection($productLogo);
$i =0;
$cart = Mage::getModel('checkout/cart');
$cart->init();
$params = array(
    'product' => $productLogo->getId(),
    'qty' => 1,
    'form_key' => Mage::getSingleton('core/session')->getFormKey(), 
);
foreach ($opts->getData() as $data) {
    $optionId = $data['option_id'];
    $image = $paths[0]['name'];
    $options =  array(
    $optionId => array(
        'type' => $paths[0]['type'],
        'title' => $image,
        'quote_path' => DS.'media'.DS.'uploads'.DS.'logo-branding'.DS.$quote_id.DS.$image,
        'order_path' =>  DS.'media'.DS.'uploads'.DS.'logo-branding'.DS.$quote_id.DS.$image,
        'fullpath' => $path.$image,
        'secret_key' => substr(md5(file_get_contents($path.$image)), 0, 20)),
    );
    $params['options_'.$optionId.'_file_action'] = 'save_new';
}
$params['options'] = $options;
print_r($params);
$request = new Varien_Object();
$request->setData($params);
try {
    $cart->addProduct($productLogo->getId(), $request);
    $cart->save();
    $i++;
} catch (Exception $e) {
    print_r($e->getMessage());
}

但我得到了这样的错误:

请指定产品的必需选项


您能否在183行的app / code / core / Mage / Catalog / Model / Product / Option / Type / File.php中记录原始异常并将其发布?
LDusan '18

Answers:


0

您尝试向卡中添加可配置的产品,但未修复此产品的选项:因此可能是我的代码将对您有所帮助:

<?php

include 'app/Mage.php';

Mage::app();

// Need for start the session

Mage::getSingleton('core/session', array('name' => 'frontend'));

try {

    $product_id = '126'; // Replace id with your product id

    $product = Mage::getModel('catalog/product')->load($product_id);

    $cart = Mage::getModel('checkout/cart');

    $cart->init();

    $params = array(

        'product' => $product_id,

        'super_attribute' => array(

            525 => 100,
            //525 is the attribute id of size and 100 is the selected option value (small) of that attribute.

        ),

        'qty' => 2,

    );

    $cart->addProduct($product, $params);

    $cart->save();

    Mage::getSingleton('checkout/session')->setCartWasUpdated(true);

    Mage::getSingleton('core/session')->addSuccess('Product added successfully');

    header('Location: ' . 'index.php/checkout/cart/');

} catch (Exception $e) {

    echo $e->getMessage();

}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.