Search code examples
shopware

shopware 6: how can I get a price rule ID at updating productrepository


I have a question about programmatically updating a product via the Shopware 6 ProductRepository. I will update actionprices for my product. But then rule Id is a required field. But how can I get this Rule ID field?

    $price = [[
        'linked' => false,
        'net' => (float)$netPrice,
        'gross' => (float)$grossPrice,
        'currencyId' => Defaults::CURRENCY,

    ]];

    $prices = [
        [
            'quantityStart' => 1,
            'ruleId' => '???', // How can I get this???
            'quantityEnd' => 10,
            'price' => [
                [
                    'currencyId' => Defaults::CURRENCY,
                    'gross' => $actionGrossPrice,
                    'net' => $actionNetPrice,,
                    'linked' => false
                ]
            ]
        ]
    ];

So how can I get this rule ID field?


Solution

  • Unfortunately you did not post the full code, especially the portion where you call the update function. I am assuming, that you are trying to set the "advanced pricing" of a product.

    Before doing something programmatically, it might be a good idea to check how it's done manually.

    In the admin panel (Shopware v6.4.3.0), creating advanced price rules also needs a rule id:

    Screenshot of Shopware asking for the rule ID to create an advanced price

    This refers to a rule created before unter Settings -> Shop -> Rule Builder.

    There are some rules created by default, for example "all customers":

    Rule all customers

    You can pick the rule ID from the URL when clicking that rule and use this in your code.

    pick the rule id

    This might be fine if you write a plugin for one specific shop. But the rule would differ in other shops.

    In case you are writing a plugin for a broader audience, I recommend to make the rule to be applied configurable in the plugin configuration.

    You could of course also search programmatically for a rule "All customers" - but it might have been deleted by the shop owner.