OFFLINE-GmbH / oc-mall-plugin

:convenience_store: E-commerce solution for October CMS
https://offline-gmbh.github.io/oc-mall-plugin
MIT License
167 stars 112 forks source link

Allow PaymentGatewaySettings to be different between multisites #1135

Open chocolata opened 1 month ago

chocolata commented 1 month ago

Hi,

I'm using the OFFLINE Mall in a multisite setup and it works to the largest extent.

The only, very important detail I can't find an answer for is to allow the payment gateway settings to be different across different sites. Normally we'd use $translatable for this, in the Model.

My working Payment solution uses the basics of the example implementation here: https://offline-gmbh.github.io/oc-mall-plugin/development/core/payment-providers.html

I thought to do something like this, but it doesn't work:

In my classes file:

    public function settings(): array
    {
        return [
            "mollie_mode" => [
                "label" => "chocolata.mallmolliepayments::lang.settings.mollie_mode",
                "default" => "test",
                "comment" => "chocolata.mallmolliepayments::lang.settings.mollie_mode_label",
                "span" => "left",
                "type" => "dropdown",
                "translatable" => true,
                "options" => [
                    "test" => "Test",
                    "live" => "Live",
                ],
            ],
            "test_api_key" => [
                "label" => "chocolata.mallmolliepayments::lang.settings.test_api_key",
                "comment" => "chocolata.mallmolliepayments::lang.settings.test_api_key_label",
                "span" => "left",
                "translatable" => true,
                "type" => "text",
            ],
            "live_api_key" => [
                "label" => "chocolata.mallmolliepayments::lang.settings.live_api_key",
                "comment" => "chocolata.mallmolliepayments::lang.settings.live_api_key_label",
                "span" => "left",
                "translatable" => true,
                "type" => "text",
            ],
        ];
    }

In my Plugin.php boot method:

        PaymentGatewaySettings::extend(function($model) {

            // Implement the TranslatableModel behavior if not already implemented
            if (!$model->isClassExtendedWith('RainLab.Translate.Behaviors.TranslatableModel')) {
                $model->implement[] = 'RainLab.Translate.Behaviors.TranslatableModel';
            }

            // Add fields to be translatable
            $model->addDynamicProperty('translatable', [
                'live_api_key',
                'test_api_key',
            ]);
        });

I feel like I might be close... Can anyone point me in the right direction please?

SamBrishes commented 1 month ago

Hello,

as far as I can tell so far, your code can't work this way. The PaymentGatewaySettings model is a simple October SettingsModel, which stores the corresponding data as json inside the core system_settings.value column using "offline_mall_settings" as key. However, $translatable works only column-based and does not support JSON-indexes, even if the SettingsModel "unpacks" the value and allows direct access.

If you're using OC 3.5 (or 3.6, dunno when multisite was introduced) you could theoretically use the new MultiSite trait / feature as described here. But I guess, this requires some changes on the core Mall files and so far I have not been able to integrate it functionally.

I guess, this needs some more attention to get it working (and ensure, that no breaking changes are introduced).

Sincerely, Sam.

chocolata commented 1 month ago

Hi Sam,

Thanks for taking the time to look into this. Looks like I was a little too eager. I don't have the time to investigate this further.

So I'm gonna try to solve this another way. I'll create payment settings fields for each multisite, by prefixing them with each site code.

So the settings of all the sites would be visible regardless of which context the backend is in.

I would apply the correct settings based on the order that is placed (I have previously extended the order fields to include the context of the site where the order was placed).

I will let you know how it goes. Thanks a lot!