How to check if total of 1 particular field in structure equals 0

Hello,

In my blueprints I have the below structure field. The field “inventory_quantity” will have a number input between 0-10. I would like to check if the total of the field “inventory_quantity” for all items in the structure equals 0. The end goal is to create an If Statement so that when the total is 0, I can add a sold out banner.

shopifyVariants:
        label: Variants
        type: structure
        fields:
          title:
            label: Title
            type: text
          price:
            label: Price
            type: text
          compare_at_price:
            label: Compare at price
            type: text
          inventory_quantity:
            label: Inventory
            type: text
          sku:
            label: SKU
            type: text

I am just getting very stuck at the moment…

Any help is greatly appreciated!

$inventory_quantity = array_sum($page->shopifyVariants()->toStructure()->pluck('inventory_quantity', ','));

$page->shopifyVariants()->toStructure()->pluck('inventory_quantity', ',') plucks all the inventory_quantity values from the structure , array_sum then sums up all the values in that array.

Consider using a number field instead of a text field, so that only numbers can be added. It also allows you to set a min and max value.

If this doesn’t work because the array contains strings instead of numbers, then we have to first cast to number before summing up. Edit: seems to work, though.

Thanks a lot @texnixe, that worked perfectly. I will go ahead and mark it solved.