Forum

Thread tagged as: Question, Problem, Shop

Perch Shop Product Importer API

Hello,

I am trying to test the Product Importer API. I set up a perch installation from scratch, installed Perch Shop and added Shop and Members to my apps.php.

Just used the sample script:

<?php

include('/perch/runtime.php');

$API      = new PerchAPI(1.0, 'my_importer');
$Importer = $API->get('PerchShopProductImporter');

try {
    $Importer->add_item([
        'sku'               => 'TSHIRT01',
        'title'             => 'My first t-shirt',
        'slug'              => 'my-first-t-shirt',
        'description'       => 'This is a really smashing t-shirt.',
        'image_assetID'     => 630,
        'img_desc'          => 'An image of the product',
        'status'            => true,
        'brand'             => 1,
        'category'          => 'products/clothes/',
        'catalog_only'      => false,
        'price'             => ['gbp' => 10, 'eur'=>12, 'usd'=>13],
        'sale_price'        => ['gbp' => 8,  'eur'=>10, 'usd'=>11],
        'trade_price'       => ['gbp' => 7,  'eur'=>9,  'usd'=>10],
        'on_sale'           => false,
        'tax_group'         => 1,
        'stock_status'      => '1',
        'stock_level'       => '199',
        'stock_location'    => true,
        'max_in_cart'       => 10,
        'requires_shipping' => true,
        'weight'            => 100,
        'width'             => 20,
        'height'            => 15,
        'depth'             => 30,
    ]);    
} catch (Exception $e) {
    die('Error: '.$e->getMessage());
}

?>

But all I get is a HTTP 500 error and no added product. What am I missing?

Mike Hendriks

Mike Hendriks 0 points

  • 2 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

What's the error message you're getting?

I am just getting a blank page, can't see the debug

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Hello Mike,

I am just getting a blank page, can't see the debug

At this point you need to check your error logs: https://docs.grabaperch.com/perch/building/troubleshooting/why-am-i-getting-a-blank-page/

I set up a perch installation from scratch, installed Perch Shop and added Shop and Members to my apps.php.

I think you also need to complete the Shop configuration before you can import products. From the installation guide:

Before you can add your first product, you need to perform some configuration such as setting up supporting items like tax locations, shipping options, categories and brands.

https://docs.grabaperch.com/addons/shop/installation/

Hello Hussein,

I did complete the shop installation and can add a product. The problem happens from this line:

$API = new PerchAPI(1.0, 'my_importer');

When removing everything to this line I get my normal HTML output, but from this line of code I only get a blank screen.

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

What error(s) do you see in your error logs?

My error logs are empty, is that possible?

Drew McLellan

Drew McLellan 2638 points
Perch Support

It's possible, but not likely. If you can't get PHP to log, consider turning on the display of error messages in your php.ini.

Oh, I did find something, was looking in the wrong place.

Maybe this could be of some help:

[Thu Nov 22 11:42:44.917335 2018] [fcgid:warn] [pid 9074:tid 140224743061248] [client IP] mod_fcgid: stderr: PHP Fatal error:  Uncaught Error: Class 'PerchAPI' not found in /home/kndrxwbst/domains/kendrix.website/public_html/fiets/index.php:5
Drew McLellan

Drew McLellan 2638 points
Perch Support

Do you get an error for this path? It looks incorrect.

include('/perch/runtime.php');

Well, I was using

include($_SERVER['DOCUMENT_ROOT'].'/perch/runtime.php');

because with this path I got the HTTP 500 error:

include('/perch/runtime.php');

Perch is installed like I always do

Drew McLellan

Drew McLellan 2638 points
Perch Support

If this is a routed Runway master page then you shouldn't need either.

Sorry it is a normal Perch installation. With the document_root line I don't get the error.

Anyone?

Drew McLellan

Drew McLellan 2638 points
Perch Support

With the document_root line I don't get the error.

Is this not resolved then? You were saying you were no longer getting the error.

Hi Drew,

No I was talking about the runtime include. I get a HTTP 500 error when using include('/perch/runtime.php'); ,and when using include($_SERVER['DOCUMENT_ROOT'].'/perch/runtime.php'); I don't get the HTTP 500 error, but just a blank page. Nothing is happening in the backend. I think I am not able to call the API for some reason. Also tried it on an other install of shop I had on another website, I can't get the products to be added.

All code after the $API = new PerchAPI(1.0, 'my_importer'); doesn't get output.

Okay let's start over with my explanation, because It is getting vague at this point.

I have a website using Perch with a working shop installation. When I manually add a product with: - Title: Bike - SKU: BIKE - Slug: bike-bike

the product is being added.

Now I would like to test the Perch Shop product importer API to add this product using the API.

My code:

<?php

include($_SERVER['DOCUMENT_ROOT'].'/perch/runtime.php');

perch_layout('top');

$API      = new PerchAPI(1.0, 'my_importer');
$Importer = $API->get('PerchShopProductImporter');

try {
    $Importer->add_item([
        'sku'               => 'BIKE',
        'title'             => 'Bike',
        'slug'              => 'bike-bike',
    ]);    
} catch (Exception $e) {
    die('Error: '.$e->getMessage());
}

PerchUtil::output_debug(); ?>

Now I can only see my top layout being output, nothing else. Also no product is added. So something is wrong with calling the API. What could be the problem?

Drew McLellan

Drew McLellan 2638 points
Perch Support

I have a website using Perch with a working shop installation.

You can't use content importers with Perch. You need to use Perch Runway for that.

Maybe it would be better to add to the top of the documentation that it is for Runway only (https://docs.grabaperch.com/api/import/shop/products/). I now spent a couple of hours figuring it out, because I thought it was suitable for both.

Thank you anyway for the help.

Mike Hendriks said:

Maybe it would be better to add to the top of the documentation that it is for Runway only (https://docs.grabaperch.com/api/import/shop/products/). I now spent a couple of hours figuring it out, because I thought it was suitable for both.

Thank you anyway for the help.

Mike... on the initial page for Importing Content... https://docs.grabaperch.com/api/import/

Importing Content

Perch Runway provides a number of APIs for importing content directly into the system and into supporting add-ons. This can be useful when migrating content from a different system or reading content from a file.

Drew McLellan

Drew McLellan 2638 points
Perch Support

If you have enough products that you need to programatically manage them then you should be using Runway, not Perch.