Custom Plugin

5 months ago
I am seeing a problem when installing plugins with different locations but it is being hardcoded, I want them to be more convenient by allowing admins with the highest rights to modify the location of any plugin according to their needs. My wishes on the interface page have already been configured, instead of having to go deep into the code to change the position. I'm really not good at English. I hope someone can understand my ideas and help me, thank you, I'm a young developer so I really need your help.
5 months ago
duyVN wrote:
I am seeing a problem when installing plugins with different locations but it is being hardcoded, I want them to be more convenient by allowing admins with the highest rights to modify the location of any plugin according to their needs

Can you provide more information on what you mean by location ?

duyVN wrote:
instead of having to go deep into the code to change the position

Can you provide more information on what you mean by position?
5 months ago
public Task<IList<string>> GetWidgetZonesAsync()
        {
            var lstDisplay = new List<string>();

            lstDisplay.Add(PublicWidgetZones.DanhMucFooter);
            return Task.FromResult<IList<string>>(lstDisplay);
        }
What I mean is that instead of declaring a fixed location in the plugin, I can customize the plugin to any location that I have declared in the admin section.
5 months ago
"Widget zones" in .cshtml files are the placeholders.  You can create new ones wherever you want in your admin .cshtml files.  Example:

@await Component.InvokeAsync(typeof(AdminWidgetViewComponent), new { widgetZone = AdminWidgetZones.TaxCategoryListButtons, additionalData = Model })
5 months ago
I understand that I have to do what is necessary to add a certain position on the interface. But my goal is not like that. I want them to be able to switch to each other right in the admin page
5 months ago
Ensure that widget names remain in the table, such as the settings table, a general attribute table, or a personalized table with an option to enable/disable. Verify upon startup whether the widget zone is enabled or disabled.
//Rashed
5 months ago
Do you mean you want to be able to select the Zone to use from a list of Widget Zones in Admin Config ?
This is how I do that

In the plugin file - Define the use of all widgets zones

        /// <summary>
        /// Gets widget zones where this widget should be rendered
        /// </summary>
        /// <returns>Widget zones</returns>
        public Task<IList<string>> GetWidgetZonesAsync()
        {
            return Task.FromResult<IList<string>>(
                new List<string> {
                    PublicWidgetZones.Footer,
                    PublicWidgetZones.ProductDetailsAfterBreadcrumb,
                    PublicWidgetZones.ProductDetailsAfterPictures,
                    PublicWidgetZones.ProductDetailsBeforeCollateral,
                    PublicWidgetZones.ProductDetailsBeforePictures,
                    PublicWidgetZones.ProductDetailsBottom,
                    PublicWidgetZones.ProductDetailsEssentialBottom,
                    PublicWidgetZones.ProductDetailsEssentialTop,
                    PublicWidgetZones.ProductDetailsInsideOverviewButtonsAfter,
                    PublicWidgetZones.ProductDetailsInsideOverviewButtonsBefore,
                    PublicWidgetZones.ProductDetailsOverviewBottom,
                    PublicWidgetZones.ProductDetailsOverviewTop,
                    PublicWidgetZones.ProductDetailsTop,
                    PublicWidgetZones.ProductPriceTop,
                    PublicWidgetZones.ProductPriceBottom,
                    PublicWidgetZones.OrderSummaryCartFooter,
                    PublicWidgetZones.OrderSummaryContentAfter,
                    PublicWidgetZones.OrderSummaryContentBefore,
                    PublicWidgetZones.OrderSummaryContentDeals,
                    PublicWidgetZones.OrderSummaryTotals
                }
            );
        }

In Admin Config create a selection list for one of the sets of Widget Zones

    public class Defaults
    {
        public static List<string> ProductPublicWidgetZoneValues = new List<string>
        {
            "0",
            PublicWidgetZones.ProductPriceBottom,
            PublicWidgetZones.ProductPriceTop,                    
            PublicWidgetZones.ProductDetailsAfterBreadcrumb,
            PublicWidgetZones.ProductDetailsAfterPictures,
            PublicWidgetZones.ProductDetailsBeforeCollateral,
            PublicWidgetZones.ProductDetailsBeforePictures,
            PublicWidgetZones.ProductDetailsBottom,
            PublicWidgetZones.ProductDetailsEssentialBottom,
            PublicWidgetZones.ProductDetailsEssentialTop,
            PublicWidgetZones.ProductDetailsInsideOverviewButtonsAfter,
            PublicWidgetZones.ProductDetailsInsideOverviewButtonsBefore,
            PublicWidgetZones.ProductDetailsOverviewBottom,
            PublicWidgetZones.ProductDetailsOverviewTop,
            PublicWidgetZones.ProductDetailsTop
        };
    }

            int count = 0;
            foreach (var item in Defaults.ProductPublicWidgetZoneValues)
            {
                if (count == 0)
                    model.ProductPublicWidgetZoneValues.Add(new SelectListItem(await _localizationService.GetResourceAsync("Plugins.Payments.Name.DoNotDisplay"), count.ToString(), model.ProductPublicWidgetZoneId == count++));
                else
                    model.ProductPublicWidgetZoneValues.Add(new SelectListItem(item, count.ToString(), model.ProductPublicWidgetZoneId == count++));
            }
...
            //save settings
            Settings.ProductPublicWidgetZoneId = model.ProductPublicWidgetZoneId;

In the Component - compare the current zone to the one saved in Settings

        public async Task<IViewComponentResult> InvokeAsync(string widgetZone, object additionalData)
        {
            string productZone = string.Empty;
            if (settings.ProductPublicWidgetZoneId != 0 &&
                settings.ProductPublicWidgetZoneId < Defaults.ProductPublicWidgetZoneValues.Count)
                productZone = Defaults.ProductPublicWidgetZoneValues[_afterpayPaymentSettings.ProductPublicWidgetZoneId];

            if (widgetZone == productZone)
            {
                // Do Something
            }
        }

I dont know if there is another more efficient way to do it
5 months ago
Thank you everyone for your help, I did it another way I thought of, I think it's quite difficult to capture the plugin key to determine which plugin this is and then save it to the database, giving the cx location like that , then I have to go through the extra work of manually adding locations for each plugin through the admin interface.