|  | 
|  | 1 | +<?php | 
|  | 2 | + | 
|  | 3 | +namespace BigCommerce\ApiV3\Api\Carts; | 
|  | 4 | + | 
|  | 5 | +use BigCommerce\ApiV3\Api\Generic\DeleteResource; | 
|  | 6 | +use BigCommerce\ApiV3\Api\Generic\UuidResourceWithUuidParentApi; | 
|  | 7 | +use BigCommerce\ApiV3\ResourceModels\Cart\CartItem; | 
|  | 8 | +use BigCommerce\ApiV3\ResponseModels\Cart\CartResponse; | 
|  | 9 | +use GuzzleHttp\RequestOptions; | 
|  | 10 | + | 
|  | 11 | +class CartItemsApi extends UuidResourceWithUuidParentApi | 
|  | 12 | +{ | 
|  | 13 | +    use DeleteResource; | 
|  | 14 | + | 
|  | 15 | +    private const CARTS_ENDPOINT = 'carts/%s/items'; | 
|  | 16 | +    private const CART_ENDPOINT  = 'carts/%s/items/%s'; | 
|  | 17 | + | 
|  | 18 | +    /** | 
|  | 19 | +     * Create a direct link to a Cart. | 
|  | 20 | +     */ | 
|  | 21 | +    public const INCLUDE_REDIRECT_URLS = 'redirect_urls'; | 
|  | 22 | + | 
|  | 23 | +    /** | 
|  | 24 | +     *  The Cart returns an abbreviated result. Use this to return physical items product options. | 
|  | 25 | +     */ | 
|  | 26 | +    public const INCLUDE_PHYSICAL_ITEMS = 'line_items.physical_items.options'; | 
|  | 27 | + | 
|  | 28 | +    /** | 
|  | 29 | +     * The Cart returns an abbreviated result. Use this to return digital items product options. | 
|  | 30 | +     */ | 
|  | 31 | +    public const INCLUDE_DIGITAL_ITEMS = 'line_items.digital_items.options'; | 
|  | 32 | + | 
|  | 33 | +    public function add(CartItem $cartItem, ?string $include = null): CartResponse | 
|  | 34 | +    { | 
|  | 35 | +        $query = $include ? ['include' => $include] : []; | 
|  | 36 | +        $response = $this->getClient()->getRestClient()->post( | 
|  | 37 | +            $this->multipleResourceUrl(), | 
|  | 38 | +            [ | 
|  | 39 | +                RequestOptions::JSON => $cartItem, | 
|  | 40 | +                RequestOptions::QUERY => $query, | 
|  | 41 | +            ] | 
|  | 42 | +        ); | 
|  | 43 | + | 
|  | 44 | +        return new CartResponse($response); | 
|  | 45 | +    } | 
|  | 46 | + | 
|  | 47 | +    public function update(CartItem $cartItem, ?string $include = null): CartResponse | 
|  | 48 | +    { | 
|  | 49 | +        $query = $include ? ['include' => $include] : []; | 
|  | 50 | +        $response = $this->getClient()->getRestClient()->put( | 
|  | 51 | +            $this->singleResourceUrl(), | 
|  | 52 | +            [ | 
|  | 53 | +                RequestOptions::JSON => $cartItem, | 
|  | 54 | +                RequestOptions::QUERY => $query, | 
|  | 55 | +            ] | 
|  | 56 | +        ); | 
|  | 57 | + | 
|  | 58 | +        return new CartResponse($response); | 
|  | 59 | +    } | 
|  | 60 | + | 
|  | 61 | +    public function multipleResourceUrl(): string | 
|  | 62 | +    { | 
|  | 63 | +        return sprintf(self::CARTS_ENDPOINT, $this->getParentUuid()); | 
|  | 64 | +    } | 
|  | 65 | + | 
|  | 66 | +    public function singleResourceUrl(): string | 
|  | 67 | +    { | 
|  | 68 | +        return sprintf(self::CART_ENDPOINT, $this->getParentUuid(), $this->getUuid()); | 
|  | 69 | +    } | 
|  | 70 | +} | 
0 commit comments