I’m not quite following why you’re needing to update the extended fields with code (and where you’re getting the values to load them in by code) if they’re being properly populated on the back-end. It might help if you shared the link where you built your example from above. If you’re only trying to write the value ‘heel groot’ into the custom field ‘dimensions’, you don’t have to go through all of the above to do that. It looks like it’s just a text field, so you don’t have to go crazy there:
https://pods.io/docs/code/pods/save/
From your code, your issue is here:
$product = wc_get_product( $post_id ); // this returns a class, not the ID
This doesn’t return an $id (see wc_get_product docs https://docs.woocommerce.com/wc-apidocs/function-wc_get_product.html), it returns a class/object of the product, so your check against get_the_id()
will never be entered, so you’ll never write the value. You have $post_id which is the same as get_the_id(), so there’s no reason to make that step.
if (get_the_id() == $product) { // will never be true
$dimensions = "heel groot";
$data = array(
'dimensions' => $dimensions,
);
$pod = pods('product');
$pod->save($data); // never true, so never written
}
You need to remove that check, unless you’re trying to do something else here.
If you’re more comfortable conversing in your native language, I can try to get one of our translators to help you out.
-
This reply was modified 5 years, 5 months ago by Jim True.
Hi Jim, thanks for you reply. Very kind to offer the help from one of the translators but I think I can manage in English although I think you are a better judge of that.
Regarding the issue:
I need to update the fields by code because I try to create a process of auto create products. In the end it will be a kind of a stock photo site. The process of adding products manually is a very time consuming one. There are thousands and thousnads of products to add. That’s why I want to automate the process as much as possible.
In the original photo files is EXIF Meta data enclosed such as the dimensions in pixels.
I want to extract this data from the photo file and add it to the product. I know that it is already in the database but that is stored ass attachment meta data. I extract the metadata from the file with
<?php $filemeta = wp_get_attachment_metadata( $post_thumbnail_id, FALSE ); ?>
and then retreive it from the array with (for instance):
<?php $copyright = $filemeta['image_meta']['copyright'];?>
It means that when I want to edit some data (such as the license I have to back to the original file, change it there and upload it again to the product.
So, short: my goal is to extract the metadata and store it in the pods fields for the products. I want to do that only on product creation so I have to create if statement checking if it is a newly added product or just an edit to avoid that all metadata is overwritten from the original file.
I hope this clarify things and if you have any additional remarks (or tips)…. most welcome.
Best wishes,
Adri
Did you see my correction or notes about your code above? That was the reason you weren’t writing any updates because your if check was blocking any access.
You probably want to look for a different hook to check for new product (or the actual field value; as I indicated above, your verification check was not against product id but against a product class so your get_the_id() == $product
will never be true.
I’d look to see if WooCommerce provides a pre-save filter for only newly added products or something of that nature if you want to verify it only runs on initial product creation.