Search code examples
phpebay-api

Ebay SalesMaximizerAPI PHP sample


I am looking for PHP samples on the Sales Maximizes API for ebay. (Formally called the Related Items API).

However, the details for this particular API seem to be scattered unlike other eBay API's. Is there a source with a simple PHP call to create a product bundle or fetch a product bundle?


Solution

  • If you are familiar with using Composer for PHP there is a SDK available at https://github.com/davidtsadler/ebay-sdk-php, that will help with using the eBay API. (Full disclosure: I'm the author of the SDK).

    Below is an example of how to create a bundle using the Related Items service. In order to use the example you will need your developer App, Cert and Dev IDs for the sandbox environment. You also require an auth token for the sandbox eBay seller that you wish to create bundles for.

    Please note that while the SDK makes it easier to integrate with the API, it will not teach you everything about it. It's important that you read the documentation for the createBundles operation to see what fields and options are available.

    Examples of how to find and delete bundles can also be found at https://github.com/davidtsadler/ebay-sdk-examples/tree/master/related-items

    <?php
    
    require __DIR__.'/vendor/autoload.php';
    
    use \DTS\eBaySDK\Constants;
    use \DTS\eBaySDK\RelatedItemsManagement\Services;
    use \DTS\eBaySDK\RelatedItemsManagement\Types;
    use \DTS\eBaySDK\RelatedItemsManagement\Enums;
    
    /**
     * Request to the API are made through a service object.
     */
    $service = new Services\RelatedItemsManagementService([
        'credentials' =>  [
            'appId'  => 'your-app-id',
            'certId' => 'your-cert-id',
            'devId'  => 'your-dev-id'
        ],
        'authToken'   => 'your-auth-token',
        'globalId'    => Constants\GlobalIds::US,
        'sandbox'     => true
    ]);
    
    $request = new Types\CreateBundlesRequest();
    
    /**
     * A bundle has a primary product and related products in the bundle.
     */
    $bundle = new Types\Bundle();
    $bundle->bundleName = "Example Bundle";
    $bundle->primarySKU = ['123456789'];
    $bundle->scheduledStartTime = new \DateTime('2017-03-01 00:00:00', new \DateTimeZone('UTC'));
    $bundle->scheduledEndTime = new \DateTime('2017-03-07 00:00:00', new \DateTimeZone('UTC'));
    
    /**
     * Add two products that will be bundled with the main product.
     */
    $group = new Types\RelatedProductGroup();
    $group->groupName = "Example Group";
    
    $product = new Types\RelatedProduct();
    $product->SKU = 'AAABBBCCC';
    $group->relatedProduct[] = $product;
    
    $product = new Types\RelatedProduct();
    $product->SKU = 'DDDEEEFFF';
    $group->relatedProduct[] = $product;
    
    $bundle->relatedProductGroup[] = $group;
    
    $request->bundle[] = $bundle;
    
    /**
     * Send the request.
     */
    $response = $service->createBundles($request);
    
    /**
     * Output the result of the operation.
     */
    foreach ($response->bundleStatus as $bundleStatus) {
        if ($bundleStatus->ack !== 'Failure') {
            printf(
                "Bundle Created (%s) %s\n",
                $bundleStatus->bundleID,
                $bundleStatus->bundleName
            );
        }
    }