Metafield

Source

Retrieve all metafields for a shop

Source

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

The getMetafields method accepts a Yaspa\AdminApi\Metafield\Builders\GetMetafieldsRequest 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 metafields after the specified ID

Source

The example demonstrates how to get metafields with ids greater/more-recent than the provided value.

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

use Yaspa\AdminApi\Metafield\Builders\GetMetafieldsRequest;
use Yaspa\AdminApi\Metafield\MetafieldService;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;

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

// Submit the request and get a list of metafields
$service = Factory::make(MetafieldService::class);
$metafields = $service->getMetafields($request);

Get all metafields that belong to a store

Source

The example demonstrates how to get all metafields that belong to a store.

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

use Yaspa\AdminApi\Metafield\Builders\GetMetafieldsRequest;
use Yaspa\AdminApi\Metafield\MetafieldService;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;

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

// Submit the request and get a list of metafields
$service = Factory::make(MetafieldService::class);
$metafields = $service->getMetafields($request);

Get metafields that belong to a product

Source

The example demonstrates how to get all metafields that belong to a product.

A design decision was made to deviate from the documentation and implement the method in Yaspa\AdminApi\Product\ProductService as the method is specific to the product resource.

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

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');

// Submit the request and get a list of metafields
$service = Factory::make(ProductService::class);
$metafields = $service->getProductMetafieldsById($credentials, 632910392);

Get all metafields that belong to the images of a product

Source

This method is not yet supported at 19th of October, 2017.

@todo Write docs once related classes are implemented

// @todo Provide example

Get a count of all metafields for a store

Source

The example shows how to get a count of the number of metafields in a shop.

The response is an integer value representing the count.

use Yaspa\AdminApi\Metafield\MetafieldService;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;

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

// Count metafields
$service = Factory::make(MetafieldService::class);
$metafieldsCount = $service->countMetafields($credentials);

Get a count of all metafields that belong to a product

Source

The example shows how to get a count of the number of metafields in a shop.

A design decision was made to deviate from the documentation and implement the method in Yaspa\AdminApi\Product\ProductService as the method is specific to the product resource.

The response is an integer value representing the count.

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

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

// Count metafields
$service = Factory::make(ProductService::class);
$metafieldsCount = $service->countProductMetafieldsById($credentials, 632910392);

Get a single store metafield by its ID

Source

The example shows how to get a single metafield by ID.

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

use Yaspa\AdminApi\Metafield\MetafieldService;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;

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

// Count metafields
$service = Factory::make(MetafieldService::class);
$metafield = $service->getMetafieldById($credentials, 721389482);

Get a single product metafield using the metafield's nested resource path

Source

The example shows how to get a single metafield for a product.

A design decision was made to deviate from the documentation and implement the method in Yaspa\AdminApi\Product\ProductService as the method is specific to the product resource.

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

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

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

// Get metafields
$service = Factory::make(ProductService::class);
$metafield = $service->getProductMetafieldById($credentials, 632910392, 845366454);

Create a new metafield

Source

Creating a new metafield involves creating and populating a Yaspa\AdminApi\Metafield\Models\Metafield instance.

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

Create a new metafield for a store

Source

The example shows how to create a new global metafield.

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

use Yaspa\AdminApi\Metafield\MetafieldService;
use Yaspa\AdminApi\Metafield\Models\Metafield;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;

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

// Prepare request
$metafield = (new Metafield())
    ->setNamespace('inventory')
    ->setKey('warehouse')
    ->setValue(25)
    ->setValueType('integer');

// Create a new metafield
$service = Factory::make(MetafieldService::class);
$createdMetafield = $service->createNewMetafield($credentials, $metafield);

Trying to create a metafield without a key will return an error

Source

The request attempts to create a metafield with no details.

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\Metafield\MetafieldService;
use Yaspa\AdminApi\Metafield\Models\Metafield;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;

// Get credentials
$credentials = Factory::make(ApiCredentials::class)
    ->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
$metafield = new Metafield();

/**
 * Create metafield, a Guzzle exception will be thrown
 */
$service = Factory::make(MetafieldService::class);
$createdMetafield = $service->createNewMetafield($credentials, $metafield);

Create a new metafield for a product

Source

The example shows how to create a metafield for a product.

A design decision was made to deviate from the documentation and implement the method in Yaspa\AdminApi\Product\ProductService as the method is specific to the product resource.

The response is a Yaspa\AdminApi\Metafield\Models\Metafield 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;

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

// Prepare request
$product = (new Product())
    ->setId(632910392);
$metafield = (new Metafield())
    ->setNamespace('inventory')
    ->setKey('warehouse')
    ->setValue(25)
    ->setValueType('integer');

// Get metafields
$service = Factory::make(ProductService::class);
$createdMetafield = $service->createNewProductMetafield($credentials, $product, $metafield);

Update a store metafield

Source

Modifying, or updating, a metafield involves populating a Yaspa\AdminApi\Metafield\Models\Metafield instance, however, the id attribute must be set.

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

Shopify also notes that "A metafield belonging to any resource can be updated this way. Namespace and key of an existing metafield cannot be changed."

use Yaspa\AdminApi\Metafield\MetafieldService;
use Yaspa\AdminApi\Metafield\Models\Metafield;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;

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

// Prepare request
$metafield = (new Metafield())
    ->setId(721389482)
    ->setValue('something new')
    ->setValueType('string');

// Update metafield
$service = Factory::make(MetafieldService::class);
$updatedMetafield = $service->updateMetafield($credentials, $metafield);

Update a product metafield

Source

Modifying, or updating, a metafield involves populating a Yaspa\AdminApi\Metafield\Models\Metafield instance, however, the id attribute must be set.

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

Shopify also notes that "Namespace and key of an existing metafield cannot be changed."

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;

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

// Prepare request
$product = (new Product())
    ->setId(632910392);
$metafield = (new Metafield())
    ->setId(845366454)
    ->setValue('titre')
    ->setValueType('string');

// Update metafield
$service = Factory::make(ProductService::class);
$updatedMetafield = $service->updateProductMetafield($credentials, $product, $metafield);

Delete a store metafield

Source

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

Shopify notes that "A metafield belonging to any resource can be deleted this way."

The response is an empty object.

use Yaspa\AdminApi\Metafield\MetafieldService;
use Yaspa\AdminApi\Metafield\Models\Metafield;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;

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

// Delete the metafield
$service = Factory::make(MetafieldService::class);
$result = $service->deleteMetafieldById($credentials, 721389482);

Delete a product metafield

Source

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

A design decision was made to deviate from the documentation and implement the method in Yaspa\AdminApi\Product\ProductService as the method is specific to the product resource.

The response is an empty object.

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

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

// Delete product metafield
$service = Factory::make(ProductService::class);
$result = $service->deleteProductMetafieldById($credentials, 632910392, 845366454);

results matching ""

    No results matching ""