slince / shopify-api-php

:rocket: Shopify API Client for PHP
MIT License
128 stars 48 forks source link

All is protected #53

Closed zerdmer closed 4 years ago

zerdmer commented 4 years ago

For example if I do it:

$product = $client->getProductManager()->find(00000000000); $product->getVariants(); echo $product->title;

It doesn't work because title is protected, i know i can use gettitle, but imagine other, for example.... datecreated, i want to now how i can get the object array information

maximzasorin commented 4 years ago

Hello, you can use the getCreatedAt method to get a product creation date.

$product = $client->getProductManager()->find(xxx);
$product->getCreatedAt(); // \DateTime object

Or you can use CRUD methods, like this:

$response = $client->get('products/xxx'); // array
$product = $response['product'];
$product['created_at']; // ISO 8601 string
zerdmer commented 4 years ago

That's perfect. But when I do it:

$products = $client->get('products', [ // Filter your products ]);

it just show arround 20 products, how get all?

maximzasorin commented 4 years ago

You need to use cursor-based pagination, see more in this article. The library supports cursor-based pagination using the paginate method, but you will not be able to use this method using CRUD, only through a manager. To understand how to use cursor-based pagination with CRUD, see how pagination is implemented in the CursorBasedPagination class.

zerdmer commented 4 years ago

Can you give me a example how use CursorBasedPagination ? thanks you a lot, and congratulations for this project

maximzasorin commented 4 years ago

See an example in readme: https://github.com/slince/shopify-api-php/blob/master/README.md#use-manager-to-manipulate-your-data

zerdmer commented 4 years ago

please, can you check it out?

$pagination = $client->getProductManager()->paginate([ // filter your product 'limit' => 3 ]);

$currentProducts = $pagination->current(); //current page

while ($pagination->hasNext()) { $nextProducts = $pagination->next(); foreach ($nextProducts as $product) { $product->getTitle(); } }

zerdmer commented 4 years ago

don't worry , i got it

$pagination = $client->getProductManager()->paginate([ // filter your product 'limit' => 3 ]);

$currentProducts = $pagination->current(); //current page

while ($pagination->hasNext()) { $nextProducts = $pagination->next(); foreach ($nextProducts as $product) { foreach ($product->getVariants() as $variant) { echo $variant->getPrice(); } exit; } }

zerdmer commented 4 years ago

now my problem is: $product->getId() doesn't work, so how can i get the id ?

maximzasorin commented 4 years ago

now my problem is: $product->getId() doesn't work, so how can i get the id ?

getId is the correct method, what error do you get?

maximzasorin commented 4 years ago

To iterate over all products you can use loop like this:

$pagination = $client->getProductManager()->paginate([
    'limit' => 250, // max value for limit
]);

$products = $pagination->current();

while (true) {
    foreach ($products as $product) {
        var_dump($product->getId()); // process product
    }

    if (!$pagination->hasNext()) {
        break;
    }

    $products = $pagination->next();
}