Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

SOAP API Question
 
DANNEWSOME
Jr. Member
 
Total Posts:  1
Joined:  2011-07-07
 

I’m trying to do the following with the SOAP API:

1) Create a customer (OK)
2) Create an address (OK)
3) Create a cart (OK, I think)
4) Assign that cart to a user (?dunno)
5) Add Product
6) Apply a coupon

Heres my code.... can anyone tell me what might be going wrong?

I frequently get either no cart created, or “Call to a member function setFinalPrice() on a non-object”

Thanks in advance everyone!

$proxy = new SoapClient(Yii::app()->params['storeHost']"/api/soap/?wsdl");
             
$sessionId $proxy->login(Yii::app()->params['magentoapiuser']Yii::app()->params['magentoapikey']);
        
            
$newCustomer = array(
                
'firstname'  => $firstname,
                
'lastname'   => $lastname,
                
'email'      => $email,
                  
//for this version of magento (1.3.2.4) you SHOULD NOT
                    // hash the password, as in:
                    // 'password_hash' => 'password'
                  
'password'   => ($password),
                    
// password hash can be either regular or salted md5:
                    // $hash = md5($password);
                    //$salt="b40cc28d14b2f6572897df1b9d227d4f";    
                    //$hash = md5($salt.$password).':'.$salt;
                    // both variants are valid
                  
'store_id'   => 1,
                    
'website_id' => 1
            
);
    
            
$newCustomerId $proxy->call($sessionId'customer.create', array($newCustomer));
             
//return $newCustomerId;
            //Create new customer address
        
            
if ($country=="CAN"{
                $country
="CA";
            
else 
                $country
="US";
            
}
            
foreach ($directory_country_region as $key => $arr){
                
if ($arr["code"== $state){
                    $region_id
=$arr["region_id"];
                
}
            }
        
            $region_id 
=0;
            
            
$newCustomerAddress = array(    
                
'firstname'  => $firstname,
                
'lastname'   => $lastname,
                
'country_id' => $country,
                
'region_id' => $region_id
                
'region' => $state
                
'country_id' => $country,
                
'city'       => $city,
                
'street'     => array($address1,$address2),
                
'telephone'  => $phone,
                
'postcode'   => $zipcode,
             
                
'is_default_billing'  => true,
                
'is_default_shipping' => true
            
);
     
            
            
$newAddressId $proxy->call($sessionId'customer_address.create', array($newCustomerId$newCustomerAddress))
                
$store_id 1;
                
$couponCode Yii::app()->session['promotion']['Offer']['OfferCoupon'];
                
$shoppingCartId $proxy->call$sessionId'cart.create', array($store_id ) );
            
        
//        echo $shoppingCartId ."<br>";
        //    echo '<pre>';
        //        var_dump($proxy->call($sessionId, 'customer.info', $newCustomerId))            
        //echo '</pre>';
        
            
                
$customer = array(
                    
'entity_id' => $newCustomerId,
                    
'mode' => 'customer'
            
                
);
            
                
// add products into shopping cart
                
$arrProducts = array(
                    array(
                        
"sku" => "OneMonth",
                        
"qty" => 1
                    
)
                );    

        
$arrAddresses = array(
            array(
                
"mode" => "shipping",
                
"firstname" => $firstname,
                
"lastname" => $lastname,
                
"company" => "",
                
"street" => array($address1,$address2),
                
"city" => $city,
                
"region" => $state,
                
"postcode" =>  $zipcode,
                
"country_id" =>  $country,
                
"telephone" => $phone,
                
"fax" => $phone,
                
"is_default_shipping" => 0,
                
"is_default_billing" => 0
            
),
            array(
                
"mode" => "billing",
                
"firstname" => $firstname,
                
"lastname" => $lastname,
                
"company" => "",
                
"street" => array($address1,$address2),
                
"city" => $city,
                
"region" => $state,
                
"postcode" => $zipcode,
                
"country_id" =>   $country,
                
"telephone" => $phone,
                
"fax" => $phone,
                
"is_default_shipping" => 0,
                
"is_default_billing" => 0
            
)
        );
        
$resultCustomerAddresses $proxy->call($sessionId"cart_customer.addresses", array($shoppingCartId$arrAddresses));
         
            
$resultCustomerSet $proxy->call($sessionId'cart_customer.set', array( $shoppingCartId$customer) );
            
            
//echo $resultCustomerSet ."<br>";
            
$resultCartProductAdd $proxy->call($sessionId"cart_product.add", array($shoppingCartId$arrProducts));
            
//echo $resultCartProductAdd ."<br>";
                        
            //    echo $resultCustomerSet ."<br>";
                
$resultCartCouponRemove $proxy->call($sessionId"cart_coupon.add", array($shoppingCartId$couponCode));
            
//    echo $resultCartCouponRemove ."<br>";
            
}
 
Magento Community Magento Community
Magento Community
Magento Community
 
inertia1024
Jr. Member
 
Total Posts:  14
Joined:  2009-06-15
 

Yeah, I’m getting the exact same error (SoapFault).  The code that triggers it for me…

$shoppingCartProducts $proxy->call($sessionId'cart_product.list', array($cart_id));

I’m following the example directly from the API docs…
http://www.magentocommerce.com/wiki/doc/webservices-api/api/cart

I’m able to create a cart, add products to the cart, etc.  But as soon as I get to the step for “cart_product.list” or “cart.info,” it blows up.

SoapFault Object [message:protected] => Call to a member function setFinalPrice() on a non-object
 
Magento Community Magento Community
Magento Community
Magento Community
 
nightowl723
Jr. Member
 
Total Posts:  1
Joined:  2011-05-26
 

I ended up hacking the login to accept login via get variable, then I just used a plug in that would add coupons to the shopping cart (Its called laprobeta I think), then using the url product/add feature....

So I just sent headers over and over to add everything I needed.

I got the sense that the XML API was never going to work.

Contact me if you need more details

-Dan

 
Magento Community Magento Community
Magento Community
Magento Community
 
inertia1024
Jr. Member
 
Total Posts:  14
Joined:  2009-06-15
 

So after doing more experimenting, it turns out that the problem was caused by adding products to the cart using SKU rather than product ID.  I know the API documentation says it’s acceptable to use a SKU, but it doesn’t seem to work.  I’ve had other issues in the past with trying to utilize SKUs rather than IDs.

In any case, as soon as I changed to product_id, the cart API behaved correctly and no more SoapFaults thrown.

What’s strangest is that the API call that adds the product with a SKU doesn’t cause an error (the method returns true).  The error doesn’t manifest until you try to get into or list the cart.

 
Magento Community Magento Community
Magento Community
Magento Community
 
irush
Jr. Member
 
Total Posts:  2
Joined:  2010-09-22
 

Hi!!

I think that the problem is the function getProduct in app/code/core/Mage/Catalog/Helper/Product.php

I don’t understand the variable $loadByIdOnFalse. What is the functionality of this variable? and I don’t understand the preg_math, It’s possible a product_id with + or -? I replace this function with my function and works OK in magento 1.6.2. I post my function and the old function:

Old Code:

public function getProduct($productId$store$identifierType null{
        $loadByIdOnFalse 
false;       
        if (
$identifierType == null{      
            
if (is_string($productId) && !preg_match("/^[+-]?[1-9][0-9]*$|^0$/"$productId)) {
                $identifierType 
'sku';        
                
$loadByIdOnFalse true;    
            
else {
                $identifierType 
'id';     
            
}
        } 

        
/** @var $product Mage_Catalog_Model_Product */
        
$product Mage::getModel('catalog/product');
        if (
$store !== null{
            $product
->setStoreId($store);
        

        
if ($identifierType == 'sku'{     
            $idBySku 
$product->getIdBySku($productId);
            if (
$idBySku{    
                $productId 
$idBySku;      
            
}
            
if ($loadByIdOnFalse{             
                $identifierType 
'id';     
            
}
        }

        
if ($identifierType == 'id' && is_numeric($productId)) {
            $productId 
= !is_float($productId) ? (int) $productId 0;
            
$product->load($productId); 
        
}

        
return $product;
    
}

New Function:

public function getProduct($productId$store$identifierType null{
        
if ($identifierType == null{
            
if (is_string($productId) && !preg_match("/[1-9][0-9]*$|^0$/"$productId)) {
                $identifierType 
'sku';
            
else {
                $identifierType 
'id';
            
}
        }

        
/** @var $product Mage_Catalog_Model_Product */
        
$product Mage::getModel('catalog/product');
        if (
$store !== null{
            $product
->setStoreId($store);
        
}
        
if ($identifierType == 'sku'{
            $idBySku 
$product->getIdBySku($productId);
            if (
$idBySku{
                $product
->load($idBySku);
            
}
        } 
else if ($identifierType == 'id' && is_numeric($productId)) {
            $productId 
= !is_float($productId) ? (int) $productId 0;
            
$product->load($productId);
        
}

        
return $product;
    
}

I change the function in local directory.

Regards

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top