Syncing Prismic.io and Moltin (create products via PHP SDK?)

products
php

#1

Hey everyone,

I am currently building a small webshop with a Nuxt.js frontend that utilises Prismic.io as its backend.
Since Snipcart is rather limited in its customization options (CSS only), I’ve decided to give Moltin a try for all the cart and checkout functionalities.

Since I’d like for my client to be able to control all data via one panel, I am currently trying to write a little helper app in php that will sync the product data hosted on Prismic over to Moltin when a Prismic webhook is called.

So far I have an array of products from Prismic that I’d like to copy over to Moltin via some sort of foreach loop containing a $moltin->products->create function.

Ideally I’d like to use the Moltin PHP SDK, but I can’t find any documentation in regards to creating products for it.

Could anyone point me in the right direction?


#2

Hey there! This includes a product create example in PHP https://github.com/moltin/php-sdk/blob/master/examples/ProductInSpecificCurrency.php#L43

Very excited to see a sync between Moltin and Prismic, we will support you however we can!

Thanks,
Matt


#3

Is it possible to set the id while creating a product? Ideally I’d like to use the same IDs on Prismic and Moltin, since this probably would make comparing both sets of data easier.


#4

Hi @studioscholz

Unfortunately it’s not possible to set the ID before the product is created.

You could use the product.created event that is triggered by our integrations API to perform any additional cleanup and data transfer with Prismic.

Hopefully this helps :smile:


#5

I take it the ID can’t be edited after the fact either, right? In that case I’ll probably use either a custom field or the SKU to store the Prismic ID.


#6

Hey @studioscholz,

Correct, the ID is automatically generated and set by our system and can not be altered.

You’re assumption is also correct, you could store the ID from Prismic as a custom field within moltin which may be helpful if you’re trying to match data sets up.

Here’s a video for extending addresses & customers, you would simply create a flow with the name & slug products and then create a string field to hold the Prismic ID.


#7

How would I go about updating a product? The following throws an error:

  try {
    $price = isset($article->data->price) ? $article->data->price * 100 : 0;
    $productUpdateResponse = $moltin->products->update($product->id, [
        'name' => $article->data->title,
        'slug' => $article->slugs[0],
        'sku' =>  $article->id,
        'description' => 'Updated with MoltinPrismicSync',
        'price' => [
            [
                'amount' => $price
            ]
        ]
    ]);
    if ($productUpdateResponse->getStatusCode() === 201) {
      $product = $productUpdateResponse->data();
      echo "Product updated (" . $productUpdateResponse->getExecutionTime() . " secs)\n";
    }
  } catch(Exception $e) {
    echo 'An exception occurred calling the moltin API:';
    echo "<pre>"; // Lil helper
    var_dump($e);
    exit;
  }

#8

Hi @studioscholz

When defining a price you also need to pass the includes_tax and currency properties for each price:

    "price": [
      {
        "amount": "100",
        "currency": "USD",
        "includes_tax": true
      }
    ]

#9

Thanks, the error still persists though. Any other mistakes in the snippet above?


#10

It’s difficult to say without seeing the error response, but you could try adding:

'type' => 'product',
'id' => $product->id,
'name' => $article->data->title,
// etc...

#12

I’ve narrowed it down to:

["message":protected]=>
  string(279) "Client error: `PUT https://api.moltin.com/v2/products/f2b6d8c0-5bb1-415d-88f4-d3344ab946ac` resulted in a `422 Unprocessable Entity` response:
{"errors":[{"title":"Failed Validation","detail":"The data.id field is required."},{"title":"Failed Validation","detail" (truncated...)

I get the error with the followng snippet:

try {
        // $price = isset($article->data->price) ? $article->data->price * 100 : 0;
        $price = 1000;
        $productUpdateResponse = $moltin->products->update($product->id, [
            'type' => 'product',
            'id' => $product->id,
            'name' => $article->data->title,
            'slug' => $article->slugs[0],
            'description' => 'Updated with MoltinPrismicSync',
            'manage_stock' => false,
            'status' => 'live',
            'commodity_type' => 'physical',
            'price' => [
                [
                    'amount' => $price,
                    'currency' => 'GBP',
                    'includes_tax' => true
                ]
            ]
        ]);
        // if ($productUpdateResponse->getStatusCode() === 201) {
        //   $product = $productUpdateResponse->data();
        //   echo "Product updated (" . $productUpdateResponse->getExecutionTime() . " secs)\n";
        // }
      } catch(Exception $e) {
        echo 'An exception occurred calling the moltin API:';
        echo "<pre>"; // Lil helper
        var_dump($e);
        exit;
      }

#13

Could you try wrapping your payload in a data array?

$productUpdateResponse = $moltin->products->update($product->id, 
    ['data' => 
        [
            'type' => 'product',
            'id' => $product->id,
            'name' => $article->data->title,
            'slug' => $article->slugs[0],
            'description' => 'Updated with MoltinPrismicSync',
            'manage_stock' => false,
            'status' => 'live',
            'commodity_type' => 'physical',
            'price' => [
                [
                    'amount' => $price,
                    'currency' => 'GBP',
                    'includes_tax' => true
                ]
            ]
        ]
    ]
);

#14

Thanks! Working now :blush:


#15

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.