douzhun5971
2015-05-20 18:20
浏览 49
已采纳

如何将自定义产品添加到magento购物车 - 请解释这些选项

Can someone please explain how to properly configure and where to get the ID's, names, options, etc of this code?

I've been searching but other questions only leads to the solution of some mistake, but I need first to know where to get the numbers that are requered.

It's supposed to get a custom product and add it to the cart.

$product_id = 12;
$id_opt_value = 12;
$final_opt_value = 12;

$product = Mage::getModel('catalog/product')->load($product_id);
$cart = Mage::getModel('checkout/cart');
$cart->init();
$params = array(
    'product' => $product_id,
    'qty'     => 1,
    'options' => array(         
        $id_opt_value => '123456',
        $final_opt_value => 'black gloss finish',
     )
);

This is the complete code:

require_once '../app/Mage.php';
umask(0);
/* not Mage::run(); */
Mage::app('default');

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

$product_id = 12;
$id_opt_value = 12;
$final_opt_value = 12;

$product = Mage::getModel('catalog/product')->load($product_id);
$cart = Mage::getModel('checkout/cart');
$cart->init();
$params = array(
    'product' => $product_id,
    'qty'     => 1,
    'options' => array(         
        $id_opt_value => '123456',
        $final_opt_value => 'black gloss finish',
     )
);

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

Any help is appreciated.

图片转代码服务由CSDN问答提供 功能建议

有人可以解释如何正确配置以及从何处获取此代码的ID,名称,选项等?

我一直在寻找,但其他问题只会导致一些错误的解决方案,但我首先需要知道在哪里获得被重新编号的数字。 \ n

它应该得到一个自定义产品并将其添加到购物车。

  $ product_id = 12; 
 $ id_opt_value = 12; 
 $ final_opt_value = 12  ; 
 
 $ product = Mage :: getModel('catalog / product') - > load($ product_id); 
 $ cart = Mage :: getModel('checkout / cart'); 
 $ cart-  > init(); 
 $ params = array(
'product'=> $ product_id,
'qty'=> 1,
'options'=> array(
 $ id_opt_value =>  ;'123456',
 $ final_opt_value =>'黑色光泽完成',
)
); 
   
 
 

这是完整的代码:

  require_once'../ app / Math.php'; 
uumask(0); 
 / *不是Mage :: run();  * / 
Mage :: app('default'); 
 
Mage :: getSingleton(“core / session”,array(“name”=>“frontend”)); 
 
 $ product_id = 12;  
 $ id_opt_value = 12; 
 $ final_opt_value = 12; 
 
 $ product = Mage :: getModel('catalog / product') - > load($ product_id); 
 $ cart = Mage :: getModel  ('checkout / cart'); 
 $ cart-> init(); 
 $ params = array(
'product'=> $ product_id,
'qty'=> 1,
'  options'=> array(
 $ id_opt_value =>'123456',
 $ final_opt_value =>'black gloss finish',
)
); 
 
 $ cart-> addProduct($  product,$ params); 
 $ cart-> save(); 
   
 
 

感谢任何帮助。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dopr25398 2015-05-20 18:58
    已采纳

    I guess I made it, but I'm still unsure how to check the custom ID.

    $product = Mage::getModel('catalog/product')->load($product_id);
    $cart = Mage::getModel('checkout/cart');
    $cart->init();
    $params = array(
        'product' => $product_id,
        'qty'     => 1,
        'options' => array(
            1 => 1, //(custom option ID like color) => (custom value for color, like red)
            2 => 3 //
         )
    );
    

    If anyone could inform me how to know exactly the numbers of options I'll chose as correct answer.

    The logic says that 1 => 1 is (first custom attribute => first value) but after that it's not the same because the 2 => 3 returned me as (second custom attribute => first value of that attribute).

    This is probably because the fist attribute has 2 values so the second attribute gets the 3rd place and so on...

    ---EDIT---

    So I finally found the answer on this other question (if anyone needs it in the future) :) Magento - Get Custom Option Value details from Option Value ID

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题