通过Web服务(使用C++)在Magento填写客户购物车

Filling the Customers Shopping Cart in Magento via Web Service (using C++)

本文关键字:Magento 客户 购物车 服务 Web 使用 C++ 通过      更新时间:2023-10-16

我通过Soap使用Magento的Web服务API版本2。我已经使用gSoap为Soap调用生成了Wrapper/Proxy类,到目前为止,一切都很好。我可以

  • 在商店中检索产品
  • 按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核心代码有一些问题,这显然有点bug,至少在使用webserviceapi时是这样。

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

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();
}

注释的代码是原始的,这将shoppingcart默认设置为false,这导致(对我来说)我无法使用它。这是对我有效的解决方案的一部分。第二部分是关于我的自定义Web服务模块,它扩展了magento Web服务api:

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();)并使用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'表示我从Web服务接收到的会话ID。我在这里所做的就是为用户设置cookie,并将他重定向到他的购物车,仅此而已。用户应该登录并查看他选择的产品。一件重要的事情是,您必须将正确的目录设置为magento存储cookie的位置(上面的"/")。

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

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

谨致问候,一月

我正在尝试做同样的事情(ios+soap V1),我认为目前不可能做到,但一个解决方案是将mageto-core中的日志方法更改为"捕获"用户名和密码(通过GET或POST),并自动调用日志函数,然后您将获得cart-raedy。

你不是唯一一个这样做的人,但如果你已经这样做了,请告诉我怎么做?