c++ - 通过 Web 服务在 Magento 中填充客户购物车(使用 C++)

标签 c++ web-services magento soap

我正在通过 Soap 使用 Magento 的 Web 服务 API 版本 2。我已经使用 gSoap 为 Soap 调用生成包装器/代理类,到目前为止,一切正常。我可以

  • 在商店中检索产品
  • 通过 ID 检索客户
  • 登录/注销

而且我能够在我的桌面应用程序中获得我需要的所有信息。

我现在想做的是:我的桌面应用程序中有一个购物网站,用户可以在其中选择产品(这意味着将它们添加到购物车)并通过单击按钮确认他的选择,例如'买'。 通过单击此按钮,他将被重定向到浏览器中的网上商店,现在应该有(可能在验证后)他的购物车,其中包含他在桌面应用程序中选择的所有产品。

所以,我尝试做的是

(不要担心丑陋的类和方法名;o))

生成新的购物车:

void SoapManager::createShoppingCart()
{
    ns1__shoppingCartCreateResponse shoppingCartCreation;
    int x = m_pProxy->shoppingCartCreate(m_stLoginID, "1", shoppingCartCreation);
    if(x!=0)
       printFaultDetails(m_pProxy->fault);
    m_currentShoppingCart = shoppingCartCreation;
}

将现有客户设置为创建的购物车:

void SoapManager::setCustomerToCart(ns1__shoppingCartCustomerEntity *customer)
{
    ns1__shoppingCartCustomerSetResponse customerResponse;
    int x = m_pProxy->shoppingCartCustomerSet(m_stLoginID,  m_currentShoppingCart.quoteId, customer, "1", customerResponse);
    if(x!=0 || !customerResponse.result)
        printFaultDetails(m_pProxy->fault);
}

将简单(示例)产品添加到购物车:

void SoapManager::addProductToCurrentCart(shoppingCartProductEntityArray *productsToAdd)
{
    ns1__shoppingCartProductAddResponse productAdded;
    int x = m_pProxy->shoppingCartProductAdd(m_stLoginID, m_currentShoppingCart.quoteId, productsToAdd, "1", productAdded);
    if(x!=0 || !productAdded.result)
        printFaultDetails(m_pProxy->fault);
}

一般来说,仅此而已。我期望的是产品附加到客户的购物车,该购物车已设置到购物车。 但是在重定向到网上商店后,购物车中没有产品,管理面板中也没有购物车。

当我使用报价 ID 以编程方式检索购物车时,其中包含所有已添加的产品,所以我认为这可能是我的理解错误。

任何建议,我做错了什么?

或者只能使用查询字符串将产品添加到(网上商店)购物车吗? (如果是这样:如何添加多个产品?)

提前致以最诚挚的问候和感谢!

一月

最佳答案

有点早了,但我想我最终是通过混合使用半知半解和试错来做到的;o)除此之外,我不打算采用这个解决方案,而是将 magento 完全集成到我的桌面中-无需将用户重定向到浏览器的应用程序。

但回到2主题: 有一些关于 magento 核心代码的事情,这显然是一个小错误,至少使用 webservice api 是这样。

首先,当为用户创建一个新的购物车时,这个购物车没有被标记为事件的。 在“app/code/core/checkout/model/cart/Api.php”中,您有以下代码片段,它显然创建了一个新购物车:

public function create($store = null)
{
    $storeId = $this->_getStoreId($store);

    try {
        /*@var $quote Mage_Sales_Model_Quote*/
        $quote = Mage::getModel('sales/quote');
        //Changed is active to true in order to activate the cart when created
        /*$quote->setStoreId($storeId)
                ->setIsActive(false)
                ->setIsMultiShipping(false)
                ->save();*/
        $quote->setStoreId($storeId)
                ->setIsActive(true)
                ->setIsMultiShipping(false)
                ->save();           
    } catch (Mage_Core_Exception $e) {
        $this->_fault('create_quote_fault', $e->getMessage());
    }
    return (int) $quote->getId();
}

评论的代码是原始代码,默认情况下这会将购物车设置为 false,这导致(对我而言)我无法使用它。这是对我有用的解决方案的一部分。 第二部分是关于扩展 magento-webservice-api 的自定义 webservice-module:

public function getSession($customerID)
{
    require_once("app/Mage.php");
    Mage::app ();
    umask(0);

    $webSites = Mage::app()->getWebsites();

    $code = $webSites[1]->getCode();
    $session = Mage::getSingleton("customer/session"); // This initiates the PHP session        
    // The following line is the trick here. You simulate that you
    // entered Magento through a website (instead of the API) so if you log in your user
    // his info will be stored in the customer_your_website scope
    $session->init('customer_' . $code);
    $bool = $session->loginById($customerID);  // Just logging in some example user     
    return session_id();     // this holds your session id
}

上面的代码片段模拟了通过 magento 网站上的正常登录过程的条目。在这里,我使用我的购物页面的主网站代码/标识符($webSites[1]->getCode();)初始化一个 session ,并使用 customerID 登录用户。 之后我将 sessionId 返回到我的桌面应用程序。当用户现在想要购买他选择的东西(添加方式与我在问题中提到的相同)时,他单击一个按钮并被重定向到浏览器到一个小的 php 脚本,如下所示:

<?php
// Make sure you match the cookie path magento is setting
setcookie('frontend', $_GET['session'], 0, '/');
//echo $_GET['session'];
header('Location: http://yourdomain.com/magento/checkout/cart');
?>

相应的调用/查询字符串如下所示:

http://yourdomain.com/setCookie.php?session="+sessionId

'+sessionId' 表示我从网络服务收到的 session ID。我在这里所做的只是为用户设置 cookie 并将他重定向到他的购物车,仅此而已。用户应该登录并看到他选择的产品。 一件重要的事情是您必须将正确的目录设置为 magento 存储其 cookie 的位置(上面的“/”)。

其实我想给你一堆信息丰富的网站的链接,这些网站在这个烦人的过程中对我有很大帮助,但 stackoverflow 不允许超过 2 个超链接,不幸的是,我的代码中就是上面的 2 个。对不起。

希望能帮到你一点点。如果您有更多问题,请联系我或在这里写信,也许我可以帮助您。 :o)

最好的问候, 简

关于c++ - 通过 Web 服务在 Magento 中填充客户购物车(使用 C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9127637/

相关文章:

具有默认参数的 C++ 模板类,可能还有元编程

c++ - 由于超时取消async_read

c++ - 比较两个数组

c++ - 使用指向成员函数的指针 C++ 将成员函数作为参数传递

web-services - Web 服务 - SOAPui - 接收器无法序列化节点 AXIS2

jquery - AJAX - 购物车 Magento 总计和购物车中的商品

web-services - Asp.net 路由、Web 服务和 IIS7 经典

python - 如何在curl重定向上发送数据?

php - Magento 我可以禁用 cookie 吗?

magento - 在magento中限制可下载产品的购买数量有什么好方法