Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento Cart API not showing prices

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.

like image 892
Justin Avatar asked May 27 '11 13:05

Justin


2 Answers

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

like image 88
Justin Avatar answered Oct 19 '22 09:10

Justin


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.

like image 24
Jason Neumann Avatar answered Oct 19 '22 08:10

Jason Neumann