Skip to content
Advertisement

How to check value of Custom Checkbox Product Meta when looping through order items

I created a custom checkbox meta in Dokan new-single-product.php template:

JavaScript

Then used the following code to store the values:

JavaScript

Now I want the logic to be: Go through Order Items, IF item has custom checkbox checked, add item line price total to $amount variable. This runs on order complete.

JavaScript

I’ve got 3 different $custom_field variables as I’ve been trying every solution that I’ve found. But nothing’s working.

If I inspect element on the checkbox in the product edit menu for Dokan, it shows this: screenshot

It seems there are 2 inputs, 1 hidden. Maybe I should be trying to retrieve the attribute ‘checked’ rather than the actual values?

Advertisement

Answer

There are a couple more articles on this and I have been thinking my problem is different however I did the following and fixed it with the general solution I found on the other posts:

  1. Checked what meta value is actually being stored in the database for the meta key.

  2. Checked get_post_meta method’s parameters are readable to the method (in my case $item_id was invalid, the method requires the actual post ID of the product)

  3. Check the condition was actually using the right values: if( $custom_field == “yes” )

Final code for foreach function:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement