vienthuong / shopware-php-sdk

A PHP SDK for Shopware 6 Admin API

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to create promotions through API?

png-prakash opened this issue · comments

Hi,

I wanted to create promotion through API and this is my code


$promotionRepository = RepositoryFactory::create(PromotionDefinition::ENTITY_NAME);

$promotionRepository = $promotionRepository->create([
    'name' => 'TEST123',
    'active' => true,
    'exclusive' => false,
    'useCodes' => false,
    'useIndividualCodes' => false,
    'useSetGroups' => false,
    'salesChannels' => [
            'salesChannelId' => 'fd22b2830eded4e381b457d44781551'
    ],
    'discounts' => [
        'scope' => 'cart',
        'value' => 5,
        'type' => 'absolute'
    ],
], $context);

and I am getting error like this

{"errors":[{"code":"0","status":"500","title":"Internal Server Error","detail":"Argument 2 passed to Shopware\\Core\\Framework\\Api\\Converter\\ApiVersionConverter::convertPayload() must be of the type array, string given, called in \/var\/www\/shopware6\/vendor\/shopware\/core\/Framework\/Api\/Converter\/ApiVersionConverter.php on line 66"}]}

I have to pass any extra parameter?
I am using Shopware v6.4.6.1
How to solve this issue? Thanks

Hi @png-prakash
I checked the promotion definition and I think you need a payload like this to make it works. Notice the double [ bracket and considerAdvancedRules, priority property are missing in your request.

$promotionRepository->create([
            'name' => 'TEST123',
            'active' => true,
            'exclusive' => false,
            'useCodes' => false,
            'useIndividualCodes' => false,
            'useSetGroups' => false,
            'salesChannels' => [[
                'salesChannelId' => <your_applied_promo_sales_channel_id>,
                'priority' => 1
            ]],
            'discounts' => [[
                'scope' => 'cart',
                'value' => 5,
                'type' => 'absolute',
                'considerAdvancedRules' => true
            ]],
        ], $context);