Product

Source

Retrieve a list of all products

Source

All retrieval examples utilise the Yaspa\AdminApi\Product\ProductService::getProducts method, which returns a Yaspa\Builders\PagedResultsIterator that will provide an iterate-able list of Yaspa\AdminApi\Product\Models\Product models.

The getProducts method accepts a Yaspa\AdminApi\Product\Builders\GetProductsRequest instance, which is a request builder that will provide hints to what options are available assuming one is using an IDE such as PHPStorm.

Get all products

Source

The example demonstrates how to get all products for a shop.

The response is a Yaspa\Builders\PagedResultsIterator that will provide an iterate-able list of Yaspa\AdminApi\Product\Models\Product models.

use Yaspa\AdminApi\Product\Builders\GetProductsRequest;
use Yaspa\AdminApi\Product\ProductService;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;

// Prepare the request
$credentials = Factory::make(ApiCredentials::class)
    ->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
$request = Factory::make(GetProductsRequest::class)
    ->withCredentials($credentials);

// Get a list of products
$service = Factory::make(ProductService::class);
$products = $service->getProducts($request);

Get all products that belong to a certain collection

Source

The example demonstrates how to get all products for a shop that belong to a collection.

The response is a Yaspa\Builders\PagedResultsIterator that will provide an iterate-able list of Yaspa\AdminApi\Product\Models\Product models.

use Yaspa\AdminApi\Product\Builders\GetProductsRequest;
use Yaspa\AdminApi\Product\ProductService;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;

// Prepare the request
$credentials = Factory::make(ApiCredentials::class)
    ->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
$request = Factory::make(GetProductsRequest::class)
    ->withCredentials($credentials)
    ->withCollectionId(841564295);

// Get a list of products
$service = Factory::make(ProductService::class);
$products = $service->getProducts($request);

Get a list of specific products

Source

The example demonstrates how to get specific products based on their IDs.

The response is a Yaspa\Builders\PagedResultsIterator that will provide an iterate-able list of Yaspa\AdminApi\Product\Models\Product models.

use Yaspa\AdminApi\Product\Builders\GetProductsRequest;
use Yaspa\AdminApi\Product\ProductService;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;

// Prepare the request
$credentials = Factory::make(ApiCredentials::class)
    ->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
$request = Factory::make(GetProductsRequest::class)
    ->withCredentials($credentials)
    ->withIds([632910392,921728736]);

// Get a list of products
$service = Factory::make(ProductService::class);
$products = $service->getProducts($request);

Get all products after the specified ID

Source

The example demonstrates how to get a list of products after a specified product ID.

The response is a Yaspa\Builders\PagedResultsIterator that will provide an iterate-able list of Yaspa\AdminApi\Product\Models\Product models.

use Yaspa\AdminApi\Product\Builders\GetProductsRequest;
use Yaspa\AdminApi\Product\ProductService;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;

// Prepare the request
$credentials = Factory::make(ApiCredentials::class)
    ->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
$request = Factory::make(GetProductsRequest::class)
    ->withCredentials($credentials)
    ->withSinceId(632910392);

// Get a list of products
$service = Factory::make(ProductService::class);
$products = $service->getProducts($request);

Get all products, showing only some attributes

Source

The example demonstrates how to get a list of products with only some fields/attributes populated.

The response is a Yaspa\Builders\PagedResultsIterator that will provide an iterate-able list of Yaspa\AdminApi\Product\Models\Product models.

use Yaspa\AdminApi\Product\Builders\GetProductsRequest;
use Yaspa\AdminApi\Product\Builders\ProductFields;
use Yaspa\AdminApi\Product\ProductService;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;

// Prepare the request
$credentials = Factory::make(ApiCredentials::class)
    ->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
$fields = Factory::make(ProductFields::class)
    ->withId()
    ->withImages()
    ->withTitle();
$request = Factory::make(GetProductsRequest::class)
    ->withCredentials($credentials)
    ->withProductFields($fields);

// Get a list of products
$service = Factory::make(ProductService::class);
$products = $service->getProducts($request);

Receive a count of all products

Source

The count endpoint for the product resource supports a lot more filtering options than other resources. Therefore, the builder class Yaspa\AdminApi\Product\Builders\CountProductsRequest is needed to provide a fluent interface for available options.

All examples utilise the count request builder and calls the Yaspa\AdminApi\Product\ProductService::countProducts method, which will return an integer representing the product count satisfying filter options provided in the request.

Counts all products that belong to a certain collection

Source

The example shows how to get a count of the number of products in a given collection id.

The response is an integer value representing the count.

use Yaspa\AdminApi\Product\Builders\CountProductsRequest;
use Yaspa\AdminApi\Product\ProductService;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;

// Prepare the request
$credentials = Factory::make(ApiCredentials::class)
    ->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
$request = Factory::make(CountProductsRequest::class)
    ->withCredentials($credentials)
    ->withCollectionId(841564295);

// Count products
$service = Factory::make(ProductService::class);
$productsCount = $service->countProducts($request);

Counts all products

Source

The example shows how to get a count of all products.

The response is an integer value representing the count.

use Yaspa\AdminApi\Product\Builders\CountProductsRequest;
use Yaspa\AdminApi\Product\ProductService;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;

// Prepare the request
$credentials = Factory::make(ApiCredentials::class)
    ->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
$request = Factory::make(CountProductsRequest::class)
    ->withCredentials($credentials);

// Count products
$service = Factory::make(ProductService::class);
$productsCount = $service->countProducts($request);

Receive a single product

Source

Get product has very few options, therefore the methods are implemented directly without a builder class.

Get only particular fields

The example demonstrates how to get a product with only some fields/attributes populated.

The response is a Yaspa\AdminApi\Product\Models\Product model.

use Yaspa\AdminApi\Product\ProductService;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;

// Prepare the request
$credentials = Factory::make(ApiCredentials::class)
    ->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
$fields = Factory::make(ProductFields::class)
    ->withId()
    ->withImages()
    ->withTitle();

// Get product
$service = Factory::make(ProductService::class);
$product = $service->getProductById($credentials, 632910392, $fields);

Get a single product by ID

The example demonstrates how to get a single product.

The response is a Yaspa\AdminApi\Product\Models\Product model.

use Yaspa\AdminApi\Product\ProductService;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;

// Prepare the request
$credentials = Factory::make(ApiCredentials::class)
    ->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');

// Get product
$service = Factory::make(ProductService::class);
$product = $service->getProductById($credentials, 632910392);

Create a new product

Source

Creating a product involves populating a Yaspa\AdminApi\Product\Models\Product model. The product resource also supports creating related resources at the same time. To do so, populate models such as Yaspa\AdminApi\Product\Models\Variant and attach it to the product with a setter.

The method Yaspa\AdminApi\Product\ProductService::createNewProduct will generally return a product model back. Although it seems redundant, one should still use the Shopify returned product model as the trusted version of the created product given that Shopify is the source of truth.

Create a new product with default variant and base64 image

Source

The example demonstrates how to create a new product with the default variant and base64 encoded image.

The response is a Yaspa\AdminApi\Product\Models\Product model.

use Yaspa\AdminApi\Product\Models\Image;
use Yaspa\AdminApi\Product\Models\Product;
use Yaspa\AdminApi\Product\ProductService;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;

// Prepare the request
$credentials = Factory::make(ApiCredentials::class)
    ->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
$image = (new Image())
    ->setAttachment('R0lGODlhAQABAIAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==');
$product = (new Product())
    ->setTitle('Burton Custom Freestyle 151')
    ->setBodyHtml('<strong>Good snowboard!</strong>')
    ->setVendor('Burton')
    ->setProductType('Snowboard')
    ->setImages([$image]);

// Create product
$service = Factory::make(ProductService::class);
$newProduct = $service->createNewProduct($credentials, $product);

Create a new product with the default product variant

Source

The example demonstrates how to create a new product with the default variant.

The response is a Yaspa\AdminApi\Product\Models\Product model.

use Yaspa\AdminApi\Product\Models\Product;
use Yaspa\AdminApi\Product\ProductService;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;

// Prepare the request
$credentials = Factory::make(ApiCredentials::class)
    ->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
$product = (new Product())
    ->setTitle('Burton Custom Freestyle 151')
    ->setBodyHtml('<strong>Good snowboard!</strong>')
    ->setVendor('Burton')
    ->setProductType('Snowboard')
    ->setTags(['Barnes & Noble', 'John\'s Fav', 'Big Air']);

// Create product
$service = Factory::make(ProductService::class);
$newProduct = $service->createNewProduct($credentials, $product);

Trying to create a product without a title will return an error

Source

The request attempts to create a product without a title.

The result is a Guzzle exception. Although it may vary depending on how the Guzzle client is configured. See Guzzle http_errors options for more information.

use Yaspa\AdminApi\Product\Models\Product;
use Yaspa\AdminApi\Product\ProductService;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;

// Prepare the request
$credentials = Factory::make(ApiCredentials::class)
    ->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
$product = (new Product())->setBodyHtml('A mystery!');

/**
 * Create product, a Guzzle exception will be thrown
 */
$service = Factory::make(ProductService::class);
$newProduct = $service->createNewProduct($credentials, $product);

Create a new product with external image

Source

The example demonstrates how to create a new product with the default variant and a link to the product image.

The response is a Yaspa\AdminApi\Product\Models\Product model.

use Yaspa\AdminApi\Product\Models\Image;
use Yaspa\AdminApi\Product\Models\Product;
use Yaspa\AdminApi\Product\ProductService;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;

// Prepare the request
$credentials = Factory::make(ApiCredentials::class)
    ->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
$image = (new Image())->setSrc('http://via.placeholder.com/300x300');
$product = (new Product())
    ->setTitle('Burton Custom Freestyle 151')
    ->setBodyHtml('<strong>Good snowboard!</strong>')
    ->setVendor('Burton')
    ->setProductType('Snowboard')
    ->setImages([$image]);

// Create product
$service = Factory::make(ProductService::class);
$newProduct = $service->createNewProduct($credentials, $product);

Create a new unpublished product

Source

The example demonstrates how to create a new product that is not published.

The response is a Yaspa\AdminApi\Product\Models\Product model.

use Yaspa\AdminApi\Product\Models\Product;
use Yaspa\AdminApi\Product\ProductService;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;

// Prepare the request
$credentials = Factory::make(ApiCredentials::class)
    ->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
$product = (new Product())
    ->setTitle('Burton Custom Freestyle 151')
    ->setBodyHtml('<strong>Good snowboard!</strong>')
    ->setVendor('Burton')
    ->setProductType('Snowboard')
    ->setPublished(false);

// Create product
$service = Factory::make(ProductService::class);
$newProduct = $service->createNewProduct($credentials, $product);

Create a new product with multiple product variants

Source

The example demonstrates how to create a new product with multiple variants.

The response is a Yaspa\AdminApi\Product\Models\Product model.

use Yaspa\AdminApi\Product\Models\Product;
use Yaspa\AdminApi\Product\Models\Variant;
use Yaspa\AdminApi\Product\ProductService;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;

// Prepare the request
$credentials = Factory::make(ApiCredentials::class)
    ->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
$variant1 = (new Variant())
    ->setOption1('First')
    ->setPrice(10.00)
    ->setSku('123');
$variant2 = (new Variant())
    ->setOption1('Second')
    ->setPrice(20.00)
    ->setSku('123');
$product = (new Product())
    ->setTitle('Burton Custom Freestyle 151')
    ->setBodyHtml('<strong>Good snowboard!</strong>')
    ->setVendor('Burton')
    ->setProductType('Snowboard')
    ->setVariants([$variant1, $variant2]);

// Create product
$service = Factory::make(ProductService::class);
$newProduct = $service->createNewProduct($credentials, $product);

Create a product with a metafield

Source

The example demonstrates how to create a new product with a metafield.

The response is a Yaspa\AdminApi\Product\Models\Product model.

use Yaspa\AdminApi\Metafield\Models\Metafield;
use Yaspa\AdminApi\Product\Models\Product;
use Yaspa\AdminApi\Product\ProductService;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;

// Prepare the request
$credentials = Factory::make(ApiCredentials::class)
    ->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
$metafield = (new Metafield())
    ->setKey('new')
    ->setValue('newvalue')
    ->setValueType('string')
    ->setNamespace('global');
$product = (new Product())
    ->setTitle('Burton Custom Freestyle 151')
    ->setBodyHtml('<strong>Good snowboard!</strong>')
    ->setVendor('Burton')
    ->setProductType('Snowboard')
    ->setMetafields([$metafield]);

// Create product
$service = Factory::make(ProductService::class);
$newProduct = $service->createNewProduct($credentials, $product);

Modify an existing product

Source

Product modifications need to be made through a Yaspa\AdminApi\Product\Builders\ModifyExistingProductRequest and setting a product with a set id as well the fields that should be modified.

Please note that Yaspa does not defensively check that an id exists in a product model when used in an update request as the expectation is that Shopify will return an error.

Hide a published product

Source

The example demonstrates how to hide a published product by changing the published attribute to false.

The response is a Yaspa\AdminApi\Product\Models\Product model.

use Yaspa\AdminApi\Product\Models\Product;
use Yaspa\AdminApi\Product\ProductService;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;

// Prepare the request
$credentials = Factory::make(ApiCredentials::class)
    ->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
$toBeUpdatedProduct = (new Product())
    ->setId(632910392)
    ->setPublished(false);
$request = Factory::make(ModifyExistingProductRequest::class)
    ->withCredentials($credentials)
    ->withProduct($toBeUpdatedProduct);

// Update product
$service = Factory::make(ProductService::class);
$updatedProduct = $service->modifyExistingProduct($request);

Update a product by reordering the product variants

Source

The example demonstrates how to reorder the product variants in an existing product.

The response is a Yaspa\AdminApi\Product\Models\Product model.

use Yaspa\AdminApi\Product\Models\Product;
use Yaspa\AdminApi\Product\Models\Variant;
use Yaspa\AdminApi\Product\ProductService;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;

// Prepare the request
$credentials = Factory::make(ApiCredentials::class)
    ->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
$toBeUpdatedVariant1 = (new Variant())->setId(457924702);
$toBeUpdatedVariant2 = (new Variant())->setId(39072856);
$toBeUpdatedVariant3 = (new Variant())->setId(49148385);
$toBeUpdatedVariant4 = (new Variant())->setId(808950810);
$toBeUpdatedProduct = (new Product())
    ->setId(632910392)
    ->setVariants([$toBeUpdatedVariant2, $toBeUpdatedVariant1, $toBeUpdatedVariant3, $toBeUpdatedVariant4]);
$request = Factory::make(ModifyExistingProductRequest::class)
    ->withCredentials($credentials)
    ->withProduct($toBeUpdatedProduct);

// Update product
$service = Factory::make(ProductService::class);
$updatedProduct = $service->modifyExistingProduct($request);

Update a product's title

Source

The example demonstrates how to update a product's title.

The response is a Yaspa\AdminApi\Product\Models\Product model.

use Yaspa\AdminApi\Product\Models\Product;
use Yaspa\AdminApi\Product\ProductService;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;

// Prepare the request
$credentials = Factory::make(ApiCredentials::class)
    ->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
$toBeUpdatedProduct = (new Product())
    ->setId(632910392)
    ->setTitle('New product title');
$request = Factory::make(ModifyExistingProductRequest::class)
    ->withCredentials($credentials)
    ->withProduct($toBeUpdatedProduct);

// Update product
$service = Factory::make(ProductService::class);
$updatedProduct = $service->modifyExistingProduct($request);

Update a product's tags

Source

The example demonstrates how to update a product's tags.

The response is a Yaspa\AdminApi\Product\Models\Product model.

use Yaspa\AdminApi\Product\Models\Product;
use Yaspa\AdminApi\Product\ProductService;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;

// Prepare the request
$credentials = Factory::make(ApiCredentials::class)
    ->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
$toBeUpdatedProduct = (new Product())
    ->setId(632910392)
    ->setTags(['Barnes & Noble', "John's Fav"]);
$request = Factory::make(ModifyExistingProductRequest::class)
    ->withCredentials($credentials)
    ->withProduct($toBeUpdatedProduct);

// Update product
$service = Factory::make(ProductService::class);
$updatedProduct = $service->modifyExistingProduct($request);

Update a product and one of its variants

Source

The example demonstrates how to update a product and one of its variants, leaving the remainder intact.

The response is a Yaspa\AdminApi\Product\Models\Product model.

use Yaspa\AdminApi\Product\Models\Product;
use Yaspa\AdminApi\Product\Models\Variant;
use Yaspa\AdminApi\Product\ProductService;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;

// Prepare the request
$credentials = Factory::make(ApiCredentials::class)
    ->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
$toBeUpdatedVariant1 = (new Variant())
    ->setId(808950810)
    ->setPrice(2000.00)
    ->setSku('Updating the Product SKU');
$toBeUpdatedVariant2 = (new Variant())->setId(49148385);
$toBeUpdatedVariant3 = (new Variant())->setId(39072856);
$toBeUpdatedVariant4 = (new Variant())->setId(457924702);
$toBeUpdatedProduct = (new Product())
    ->setId(632910392)
    ->setTitle('Updated Product Title')
    ->setVariants([$toBeUpdatedVariant2, $toBeUpdatedVariant1, $toBeUpdatedVariant3, $toBeUpdatedVariant4]);
$request = Factory::make(ModifyExistingProductRequest::class)
    ->withCredentials($credentials)
    ->withProduct($toBeUpdatedProduct);

// Update product
$service = Factory::make(ProductService::class);
$updatedProduct = $service->modifyExistingProduct($request);

Show a hidden product

Source

The example demonstrates how to show a hidden product by changing the published attribute to true.

The response is a Yaspa\AdminApi\Product\Models\Product model.

use Yaspa\AdminApi\Product\Models\Product;
use Yaspa\AdminApi\Product\ProductService;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;

// Prepare the request
$credentials = Factory::make(ApiCredentials::class)
    ->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
$toBeUpdatedProduct = (new Product())
    ->setId(632910392)
    ->setPublished(true);
$request = Factory::make(ModifyExistingProductRequest::class)
    ->withCredentials($credentials)
    ->withProduct($toBeUpdatedProduct);

// Update product
$service = Factory::make(ProductService::class);
$updatedProduct = $service->modifyExistingProduct($request);

Add a metafield to an existing product

Source

The example demonstrates how to add a metafield to an existing product.

The response is a Yaspa\AdminApi\Product\Models\Product model.

use Yaspa\AdminApi\Metafield\Models\Metafield;
use Yaspa\AdminApi\Product\Models\Product;
use Yaspa\AdminApi\Product\ProductService;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;

// Prepare the request
$credentials = Factory::make(ApiCredentials::class)
    ->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
$metafield = (new Metafield())
    ->setKey('new')
    ->setValue('newvalue')
    ->setValueType('string')
    ->setNamespace('global');
$toBeUpdatedProduct = (new Product())
    ->setId(632910392)
    ->setMetafields([$metafield]);
$request = Factory::make(ModifyExistingProductRequest::class)
    ->withCredentials($credentials)
    ->withProduct($toBeUpdatedProduct);

// Update product
$service = Factory::make(ProductService::class);
$updatedProduct = $service->modifyExistingProduct($request);

Update a product by reordering product image

Source

The example demonstrates how to reorder images that belong to a product.

The response is a Yaspa\AdminApi\Product\Models\Product model.

use Yaspa\AdminApi\Product\Models\Image;
use Yaspa\AdminApi\Product\Models\Product;
use Yaspa\AdminApi\Product\ProductService;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;

// Prepare the request
$toBeUpdatedImage1 = (new Image())
    ->setId(850703190)
    ->setPosition(2);
$toBeUpdatedImage2 = (new Image())
    ->setId(562641783)
    ->setPosition(1);
$toBeUpdatedProduct = (new Product())
    ->setId(632910392)
    ->setImages([$toBeUpdatedImage1, $toBeUpdatedImage2]);
$request = Factory::make(ModifyExistingProductRequest::class)
    ->withCredentials($credentials)
    ->withProduct($toBeUpdatedProduct);

// Update product
$service = Factory::make(ProductService::class);
$updatedProduct = $service->modifyExistingProduct($request);

Update a product's SEO title and description

Source

The example demonstrates how to update a product's SEO title and description.

The response is a Yaspa\AdminApi\Product\Models\Product model.

use Yaspa\AdminApi\Product\Models\Product;
use Yaspa\AdminApi\Product\ProductService;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;

// Prepare the request
$toBeUpdatedProduct = (new Product())
    ->setId(632910392)
    ->setMetafieldsGlobalTitleTag('Brand new title')
    ->setMetafieldsGlobalDescriptionTag('Brand new description');
$request = Factory::make(ModifyExistingProductRequest::class)
    ->withCredentials($credentials)
    ->withProduct($toBeUpdatedProduct);

// Update product
$service = Factory::make(ProductService::class);
$updatedProduct = $service->modifyExistingProduct($request);

Update a product by adding a new product image

Source

The example demonstrates how add a new product image to a product.

The response is a Yaspa\AdminApi\Product\Models\Product model.

use Yaspa\AdminApi\Product\Models\Image;
use Yaspa\AdminApi\Product\Models\Product;
use Yaspa\AdminApi\Product\ProductService;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;

// Prepare the request
$toBeUpdatedImage1 = (new Image())->setId(850703190);
$toBeUpdatedImage2 = (new Image())->setId(562641783);
$newImage = (new Image())->setSrc('http://via.placeholder.com/301x301');
$toBeUpdatedProduct = (new Product())
    ->setId(632910392)
    ->setImages([$toBeUpdatedImage1, $toBeUpdatedImage2, $newImage]);
$request = Factory::make(ModifyExistingProductRequest::class)
    ->withCredentials($credentials)
    ->withProduct($toBeUpdatedProduct);

// Update product
$service = Factory::make(ProductService::class);
$updatedProduct = $service->modifyExistingProduct($request);

Update a product by clearing product images

Source

The example demonstrates how to remove all images from a product.

The response is a Yaspa\AdminApi\Product\Models\Product model.

use Yaspa\AdminApi\Product\Models\Product;
use Yaspa\AdminApi\Product\ProductService;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;

// Prepare the request
$credentials = Factory::make(ApiCredentials::class)
    ->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
$toBeUpdatedProduct = (new Product())
    ->setId(632910392)
    ->setImages([]);
$request = Factory::make(ModifyExistingProductRequest::class)
    ->withCredentials($credentials)
    ->withProduct($toBeUpdatedProduct);

// Update product
$service = Factory::make(ProductService::class);
$updatedProduct = $service->modifyExistingProduct($request);

Remove a product from the database

Source

The example shows how to delete a product using its id.

The response is an empty object.

use Yaspa\AdminApi\Product\Models\Product;
use Yaspa\AdminApi\Product\ProductService;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;

// Prepare the request
$credentials = Factory::make(ApiCredentials::class)
    ->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');

// Update product
$service = Factory::make(ProductService::class);
$result = $service->deleteProductById($credentials, 632910392);

results matching ""

    No results matching ""