skip to Main Content

I’m new with c# and i start learning with blazor. I’m trying to get all products from woocommerce api from my store. To check code is working i want to count products in a list but still no success. All the time list with products is empty. This is a full code from razor component:

@page "/woocommercepage"

@using WooCommerceNET.WooCommerce.v3;
@using WooCommerceNET.WooCommerce.v3.Extension;


<p>Products count:@products.Count()</p>
<br />


@code 
{
    public static List<Product> products = new List<Product>();

    public class woo
    {
       
        public static async Task<List<Product>> call()
        {

            RestAPI rest = new RestAPI("https://mypage.com/wp-json/wc/v3/", "ck_000000", "cs_000000");
            WCObject wc = new WCObject(rest);

            string SKU = "box";

            Dictionary<string, string> pDic = new Dictionary<string, string>();
            pDic.Add("sku", SKU);

            var products = await wc.Product.GetAll(pDic);

            return products;
        }

    }

}

2

Answers


  1. You are creating a class in your razor view that does not get called.
    In the @code block there are several overrides available. In this case you will need the OnInitializedAsync() method. This method gets called when your razor view is being served. Make your call here and update your viewstate.

    public static List<Product> products = new List<Product>();
    
        protected override async Task OnInitializedAsync()
        {
            RestAPI rest = new RestAPI("https://mypage.com/wp-json/wc/v3/", "ck_000000", "cs_000000");
            WCObject wc = new WCObject(rest);
    
            string SKU = "box";
    
            Dictionary<string, string> pDic = new Dictionary<string, string>();
            pDic.Add("sku", SKU);
    
            var products = await wc.Product.GetAll(pDic);
    
            StateHasChanged();
        }
    
    Login or Signup to reply.
  2. I have this code (in VB…sory). See if it helps…

            Public Function WC_LstProducts(Optional ByVal status As String = "any", Optional ByVal limit As Integer = 50) As List(Of Product)
                Dim res As New List(Of Product)
                Dim tmpRes As New List(Of Product)
                Dim page As Integer = 1
                Dim hasPages As Boolean = True
                Try
                    Do While hasPages
                        tmpRes = WCAPI.Product.GetAll(New Dictionary(Of String, String)() From {
                                                            {"status", status},
                                                            {"per_page", limit},
                                                            {"page", page}
                                                        }).Result
                        If tmpRes.Count <= 0 Then
                            hasPages = False
                        Else
                            res.AddRange(tmpRes)
                            page += 1
                        End If
                    Loop
    
                Catch ex As Exception                    
                    MessageBox.Show(ex.Message, "WC_LstProducts (ClassWoo): ", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    Err.Clear()
                Finally
                    page = Nothing
                    tmpRes = Nothing
                End Try
                Return res
            End Function
    

    It should not be hard to convert to C# (it will be a good practice for you:))

    Then you can use it like this:

    Dim myList As List(Of Product)
    myList = New List(Of Product)
    myList = WC_LstProducts()
    Dim counter As Integer
    counter = myList.Count
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search