1

I am trying to pull the same sipping methods that Magento shows on the front end for a product via the API, but I'm not getting the desired results. Please see my code below:

$proxy = new SoapClient("https://www.mydomain.com/api/v2_soap/?wsdl");
$sessionId = $proxy->login("myuser", "mypass");

$filter = array (
    'complex_filter' => array (
        array (
            'key' => 'type',
            'value' => array ('key' => 'in', 'value' => 'simple,grouped,configurable,bundle')
        )
    )
);

$store_view = '1';
$customer = array (
    'firstname' => 'testFirstName',
    'lastname' => 'testLastName',
    'email' => 'testapi@mydomain.com',
    'mode' => 'guest',
    'website_id' => '1',
    'store_id' => '1',
    'group_id' => '1'
);
$customerAddress = array (
    array (
        'mode' => 'billing',
        'firstname' => 'testFirstName',
        'lastname' => 'testLastName',
        'street' => '123 Elm Street',
        'city' => 'Kansas City',
        'region' => 'KS',
        'postcode' => '66101',
        'country_id' => 'US',
        'telephone' => '123456789',
        'is_default_billing' => 1
    ),
    array (
        'mode' => 'shipping',
        'firstname' => 'testFirstName',
        'lastname' => 'testLastName',
        'street' => '123 Elm Street',
        'city' => 'Kansas City',
        'region' => 'KS',
        'postcode' => '66101',
        'country_id' => 'US',
        'telephone' => '123456789',
        'is_default_shipping' => 1
    )
);

$cart = $proxy->shoppingCartCreate($sessionId, $store_view);
$cartCustomer = $proxy->shoppingCartCustomerSet($sessionId, $cart, $customer, $store_view);
$cartAddresses = $proxy->shoppingCartCustomerAddresses($sessionId, $cart, $customerAddress, $store_view);

$result = $proxy->catalogProductList($sessionId, $filter);

foreach ($result as $product) {
    print_r ($product);
    $add = array (
        array (
            'product_id' => $product->product_id,
            'sku' => $product->sku,
            'qty' => '1',
            'options' => null,
            'bundle_option' => null,
            'bundle_option_qty' => null,
            'links' => null
        )
    );
    $cart_add = $proxy->shoppingCartProductAdd($sessionId, $cart, $add, $store_view);
    $cartInfo = $proxy->shoppingCartInfo($sessionId, $cart);
    $cartShipping = $proxy->shoppingCartShippingList($sessionId, $cart, $store_view);
    print_r ($cartShipping);
    exit;
}

When I run this, the results are:

The product I'm adding to the cart....

stdClass Object
(
    [product_id] => 71
    [sku] => BB-514
    [name] => Stainless Steel Hanging Ash Bucket
    [set] => 10
    [type] => simple
    [category_ids] => Array
        (
            [0] => 1073
            [1] => 1166
            [2] => 1626
        )

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

)

Shipping...

Array
(
    [0] => stdClass Object
        (
            [code] => freeshipping_freeshipping
            [carrier] => freeshipping
            [carrier_title] => Free Shipping
            [method] => freeshipping
            [method_title] => Free
            [price] => 0
        )

    [1] => stdClass Object
        (
            [code] => productrate_productrate
            [carrier] => productrate
            [carrier_title] => Ground
            [method] => productrate
            [method_title] => Shipping
            [price] => 0
        )

    [2] => stdClass Object
        (
            [code] => dropship_error
            [carrier] => dropship
            [carrier_title] => Shipping
            [price] => 0
        )

)

When I add the product via our website, and use the same customer details, I am shown that the only available shipping method for this product is "FedEx Home Delivery (1-7 Days)" for a cost of $12.77, and this is what it should be and what I'm expecting to see via the API. I'm sure I'm missing something, but I just can't find it. An extra set of eyes would be greatly appreciated.

1

I am having the same exact problem when using the SOAP API V2. Except that for me I get NO shipping results just an empty array. I believe that SOAP API V2 is broken for shoppingCartShippingList function.

However I am able to get the desired results using SOAP API V1. Using the following test code.

$proxy = new SoapClient('http://www.domain.com/index.php/api/soap/?wsdl');
$sessionId = $proxy->login('api-user', 'api-pass');

$cartId = $proxy->call( $sessionId, 'cart.create', array() );

// load the customer list and select ID 2 from the list
$complexFilter = array(
    'entity_id' => array('eq' => '2')
);
$customerList = $proxy->call($sessionId, 'customer.list', array($complexFilter));
$customer = $customerList[0];
$customer['mode'] = 'customer';

// Set the customer to quote/cart
$resultCustomerSet = $proxy->call($sessionId, 'cart_customer.set', array($cartId, $customer) );

// load the product list and select product from the list
$productFilter = array('product_id' => array('eq' => '859') );
$productList = $proxy->call($sessionId, 'catalog_product.list',array($productFilter));
$product = $productList[0];
$product['qty'] = 1;

echo '<pre>';
print_r($product);
echo '</pre>';

// Add product to Cart
$resultCartProductAdd = $proxy->call($sessionId,'cart_product.add', array($cartId,array($product)) );

// Load customers address list
$addressList = $proxy->call($sessionId, 'customer_address.list', $customer['customer_id']);

$shipping = $addressList[2];
$shipping['mode'] = 'shipping';
$shipping['is_default_shipping'] = 1;//Optional
$shipping['is_default_billing'] = 0;//Optional

$billing = $addressList[2];
$billing['mode'] = 'billing';
$billing['is_default_shipping'] = 0;//Optional

$address = array(
    $shipping,
    $billing
);

// Set customers Address to quote/cart
$resultCustomerAddresses = $proxy->call($sessionId,'cart_customer.addresses', array($cartId,$address) );

// Get cart/quote info
$cartInfo = $proxy->call($sessionId, 'cart.info', $cartId);
echo '<pre>';
print_r($cartInfo);
echo '</pre>';

// Get shipping method list
$shippingList = $proxy->call($sessionId, 'cart_shipping.list', $cartId);
echo '<pre>';
print_r($shippingList);
echo '</pre>';

// If you don't need the session anymore
$proxy->endSession($sessionId);

The $cartInfo call is optional, I use it to check the quote/cart contents. Also the product and customer information is dependent on your environment. I suggest outputting those arrays to see your options and using id's you know exist, you may not have a test customer that has an address at $addressList[2]!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.