PHP code example of justbetter / laravel-akeneo-products

1. Go to this page and download the library: Download justbetter/laravel-akeneo-products library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

justbetter / laravel-akeneo-products example snippets




$schedule->command(\JustBetter\AkeneoProducts\Commands\Product\ProcessProductsCommand::class)->everyMinute();
$schedule->command(\JustBetter\AkeneoProducts\Commands\ProductModel\ProcessProductModelsCommand::class)->everyMinute();



use JustBetter\AkeneoProducts\Jobs\Product\RetrieveProductJob;
use JustBetter\AkeneoProducts\Jobs\ProductModel\RetrieveProductModelJob;

RetrieveProductJob::dispatch('::identifier::');

RetrieveProductModelJob::dispatch('::code::');



use JustBetter\AkeneoProducts\Contracts\Akeneo\FormatsAttributeValues;
use JustBetter\AkeneoProducts\Data\ProductData;
use JustBetter\AkeneoProducts\Enums\MappingType;
use JustBetter\AkeneoProducts\Models\Mapping;
use JustBetter\AkeneoProducts\Retrievers\Product\BaseProductRetriever;

class ExampleProductRetriever extends BaseProductRetriever
{
    public function __construct(
        protected FormatsAttributeValues $formatValue
    ) {
    }

    public function retrieve(string $identifier): ?ProductData
    {
        // Get all configured attributes from the database.
        $attributes = Mapping::of(MappingType::Attribute)->get();

        // This would be the product fetched from an ERP system.
        $product = [
            'SKU' => '1000',
            'Title' => 'Ziggy',
            'Category_Code' => 'STUFFED_TOYS',
            // ...
        ];

        // Build up the "values" array for Akeneo.
        $values = [
            //
        ];

        foreach ($product as $key => $value) {
            /** @var ?Mapping $mapped */
            $mapped = $attributes->firstWhere('source', '=', $key);

            if ($mapped === null || $value === null) {
                continue;
            }

            // Format the raw value into an appropriate attribute value for Akeneo.
            $data = $this->formatValue->format($mapped->destination, $value);

            // Add the value to the list, with the Akeneo field as the key.
            $values[$mapped->destination] = $data;
        }

        $data = [
            'identifier' => $identifier,
            'values' => $values,
        ];

        $family = Mapping::get(MappingType::Family, $product['Category_Code']);

        // Optionally, add a family. Note that you can also add categories.
        if ($family) {
            $data['family'] = $family->destination;
        }

        return ProductData::of($data);
    }
}



use JustBetter\AkeneoProducts\Data\ProductModelData;
use JustBetter\AkeneoProducts\Retrievers\ProductModel\BaseProductModelRetriever;

class ExampleProductModelRetriever extends BaseProductModelRetriever
{
    public function retrieve(string $code): ?ProductModelData
    {
        //

        return ProductModelData::of([
            //
        ]);
    }
}
bash
php artisan vendor:publish --provider="JustBetter\AkeneoProducts\ServiceProvider" --tag=config
bash
php artisan akeneo-products:retrieve {identifier}
bash
php artisan akeneo-products:process
bash
php artisan akeneo-products:update {identifier}
bash
php artisan akeneo-products:model:retrieve {code}
bash
php artisan akeneo-products:model:process
bash
php artisan akeneo-products:model:update {code}