7

I am attempting to use the Magento Enterprise 1.10 XML-RPC API to handle cart/catalog functions outside of the Magento installation. The issue that I am having is when I add to cart. I can connect just fine to the API endpoint, login, and retrieve data. The following is the code that I am using to discover the workings of the Magento API.

<?php    
   require $_SERVER['DOCUMENT_ROOT'].'/Zend/XmlRpc/Client.php';

   $url = 'http://mymagento.com/api/xmlrpc';
   $user = 'apiuser';
   $pass = 'apipass';

   $proxy = new Zend_XmlRpc_Client( $url );
   $sess = $proxy->call( 'login', array( $user, $pass ) );
   $cartId = $proxy->call( 'call', array( $sess, 'cart.create', array( 1 ) ) );

   $pList = $proxy->call( 'call', array( $sess, 'product.list', array() ) );
   $cList = $proxy->call( 'call', array( $sess, 'customer.list', array() ) );

   $cList[0]['mode'] = 'customer';

   $setCart = $proxy->call( 'call', array( $sess,
      'cart_customer.set',
      array( $cartId, $cList[0] ) ) );

   foreach( $pList as $prod)
   {
      if( $prod['product_id'] == 5 )
      {
          $prod['qty'] = 5;
          $addCart = $proxy->call( 'call', array( $sess, 
              'cart_product.add',
              array( $cartId, $pAdd ) ) );
      }
   }

   $cList = $proxy->call( 'call', array( $sess, 'cart.info', array( $cartId ) ) );
   print_r( $cList );

Outputs:

[store_id] => 1
[created_at] => 2011-05-27 13:30:57
[updated_at] => 2011-05-27 13:31:00
[converted_at] => 0000-00-00 00:00:00
[is_active] => 0
[is_virtual] => 0
[is_multi_shipping] => 0
[items_count] => 1
[items_qty] => 5.0000
[orig_order_id] => 0
[store_to_base_rate] => 1.0000
[store_to_quote_rate] => 1.0000
[base_currency_code] => USD
[store_currency_code] => USD
[quote_currency_code] => USD
[grand_total] => 0.0000
[base_grand_total] => 0.0000
[checkout_method] => customer
...
[items] => Array
(
    [0] => Array
        (
            [item_id] => 93
            [quote_id] => 119
            [created_at] => 2011-05-27 13:31:00
            [updated_at] => 2011-05-27 13:31:00
            [product_id] => 5
            [store_id] => 1
            [parent_item_id] => 
            [is_virtual] => 1
            [sku] => product1
            [name] => product
            [description] => 
            [applied_rule_ids] => 
            [additional_data] => 
            [free_shipping] => 0
            [is_qty_decimal] => 0
            [no_discount] => 0
            [weight] => 
            [qty] => 5
            [price] => 0.0000
            [base_price] => 0.0000
            [custom_price] => 
            [discount_percent] => 0.0000
            [discount_amount] => 0.0000
            [base_discount_amount] => 0.0000

However, I am to just call the following using the same above session

<?php
    $pInfo = $proxy->call( 'call', array( $sess, 'catalog_product.info', '5' ) );
    print_r( $pInfo );

I get the following information about the product:

[product_id] => 5
[sku] => product1
[set] => 9
[type] => virtual
[categories] => Array
    (
    )

[websites] => Array
    (
        [0] => 1
    )

[type_id] => virtual
[name] => product
[description] => Test
[short_description] => Test
[news_from_date] => 
[old_id] => 
[news_to_date] => 
[status] => 1
[visibility] => 4
...
[created_at] => 2011-05-25 15:11:34
[updated_at] => 2011-05-25 15:11:34
...
[price] => 10.0000

In the end, the API sees that the price of the item is in fact $10.00, but when added to the cart via API the prices are not properly reflected.

nithin alex
  • 111
  • 13
Justin
  • 131
  • 1
  • 5
  • 3
    solution found, http://www.magentocommerce.com/boards/viewthread/227044/ I spent two days searching for this, and today come up with an obscure search term to try and find the solution. – Justin May 27 '11 at 14:29
  • You can answer your own questions and close this out, FYI. – B00MER May 27 '11 at 15:46
  • I dont have high enough contribution, can't answer it for 4 more hours. =\ – Justin May 27 '11 at 19:11

3 Answers3

6

Just so it can be an officially answered question, here is the solution found, http://magentocommerce.com/boards/viewthread/227044 I spent two days searching for this, and today come up with an obscure search term to try and find the solution

Justin
  • 131
  • 1
  • 5
  • 3
    the link in your answer was changed and doesn't point to the solution any more. this one does: https://web.archive.org/web/20120624021427/http://www.magentocommerce.com/boards/viewthread/227044 – snorpey Jun 05 '15 at 10:59
1

I've been looking at this issue for a couple days now. It didn't make sense to me that if you add a product to your cart through the normal web interface [ie Mage_Checkout_CartController::addAction() ]. It knows the price without you providing an address. I finally found the difference between the two. In addAction() they create an instance of Mage_Checkout_Model_Cart, add the product to that, and save it. In the api they use Mage_Sales_Model_Quote instead. If you look at Mage_Checkout_Model_Cart::save() you will see these two lines:

$this->getQuote()->getBillingAddress();
$this->getQuote()->getShippingAddress();

These two lines actually create empty Mage_Sales_Model_Quote_Address objects that get saved into the db.

If you are willing/able to modify magento's code, you can modify Mage_Checkout_Model_Cart_Api::create() and add a call to these two methods before $quote->save() and the api and the web interface will work the same way.

I have only tested this a little bit, but I really think this is a bug and not a feature. I'll see about getting this in front of actual magento devs and maybe they'll include it in the next release.

Jason Neumann
  • 245
  • 1
  • 5
  • 13
0

Magento frontend and API both are different. In frontend after registering customer it creates quote for customer and also set address with that created quote id. But in API it creates only quote using shoppingCartCreate service. For proper we need to customize create service. I did and it worked for me.

Here am providing solution:

Edit function in file - Mage/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');
        $quote->setStoreId($storeId)
                ->setIsActive(false)
                ->setIsMultiShipping(false)
                ->save();

/* Customized this for saving default address for quote and it will show price in cart info*/
 $quote->getBillingAddress();
    $quote->getShippingAddress()->setCollectShippingRates(true);
    $quote->collectTotals();
    $quote->save();

/* End cart here */

    } catch (Mage_Core_Exception $e) {
        $this->_fault('create_quote_fault', $e->getMessage());
    }
    return (int) $quote->getId();
}
Gaurav Khatri
  • 387
  • 3
  • 11