Skip to content

When trying to add a product, or update, the api returns error 400. Reading and deleting products works fine.  #330

@rich-a-potter

Description

@rich-a-potter

I am unable to update or add products via the shopify API. I can read products fine, and delete products, but updating or adding will not work.

This is logged CurlException: Request failed with HTTP Code 400.

And this is the output to the screen
Fatal error: Uncaught PHPShopify\Exception\CurlException: Request failed with HTTP Code 400. in /Users/xxxredactedxxxxx/Sites/MCMS/vendor/phpclassic/php-shopify/lib/HttpRequestJson.php:209

When called from the controller, the model class constructor sets up the connection

public function __construct()
{
    $config = array(
        "ShopUrl" => $_ENV["SHOPIFY_SHOP_URL"],
        "AccessToken" => $_ENV["SHOPIFY_ACCESS_TOKEN"],
        "Content_type" => "application/json",
        "Curl" => array(
            CURLOPT_TIMEOUT => 10,
            CURLOPT_FOLLOWLOCATION => true
        )
    );

    $this->shopify = new ShopifySDK($config);

}

and then the controller calls an update method

public function apiUpdateProduct($id, $data) : void
{
    $this->validate($data);

    if(!empty($this->errors)){
        return;
    };

    $data = json_encode($data, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT);

    try {
        $response = $this->shopify->Product($id)->put($data);
    } catch (CurlException $e) {
        $this->addError("product", $e->getMessage());
        error_log("CurlException: " . $e->getMessage());
    } catch (\Exception $e) {
        $this->addError("product", $e->getMessage());
        error_log("Exception: " . $e->getMessage());
    }

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions