duanpang2751 2014-07-05 14:15
浏览 46
已采纳

Magento添加到购物车按钮

So I am working freelance on a project and need to know how to create a working 'add to cart' button using Magento that takes the user to the cart with the product populated. The company uses specific SKU's to identify the individual products. So, how do create such a button? The button needs to pass the SKU to the cart page (which uses a simple URL, the same one for all products). Thanks.

  • 写回答

1条回答 默认 最新

  • dongnao3990 2014-07-07 05:11
    关注

    That is provided with the default installation of magento, ain't it?

    But if you are referring to custom Add To cart button for a customized page/product then create a button/link and pass product ID and quantity and options value to the controller and handle as shown below:

    For simple product

    $params = array(
        'product' => 1, //prooduct ID
        'qty' => 2, //Quantity
    );
    $cart = Mage::getSingleton('checkout/cart'); 
    $product = Mage::getModel('catalog/product')->load(1); //loading product obj
    $cart->addProduct($product, $params); //adding to cart
    $cart->save(); 
    Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
    //now redirect user to either cart or checkout
    

    For Configurable product

    the param set up will be a little different

    $params = array(
        'product' => 1,
        'super_attribute' => array(
             51 =>25, //this is for the options 51 - option code //25 is the option selected
        ),
        'qty' => 2,
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀