Adding a Woocommerce Downloadable File From Custom Field - wordpress

Using Advanced Custom Fields, I've added a "Post Object" field to my Woocommerce edit page, set to return Post IDs.
I'm trying to get data from these posts, and automatically populate my Woocommerce Downloadable Files from them.
The problem with my current code is that if I change the data in the custom field, and then save my product, it doesn't update the downloads. I need to update the product again for the downloads to be affected.
add_action( 'woocommerce_admin_process_product_object', 'mysite_add_downloads', 50, 1 );
function mysite_add_downloads( $product ){
$mysite_select_albums = get_field('mysite_select_albums');
$downloads = array(); // Clear $downloads variable
foreach ($mysite_select_albums as $album) {
// Gets Information About Download From Album Post Type
$post = get_post($album);
$file_title = $post->post_title;
$file_url = "http://changingthinstosimplify.com/";
$file_md5 = md5($file_url);
$download = new WC_Product_Download(); // Get an instance of the WC_Product_Download Object
// Set the download data
$download->set_name($file_title);
$download->set_id($file_md5);
$download->set_file($file_url);
$downloads[$file_md5] = $download; // Insert the new download to the array of downloads
}
$product->set_downloads($downloads); // Set new array of downloads
}

Related

WooCommerce update a product when opening single product view

I have to update my product with data that comes from an API query. I would like to update the products data when visiting the single products view. I'm new to WordPress and WooCommerce, but I think this can be done through hooks?
I've tried looking for hooks but haven't had any luck yet
You could do something like the example below.
This will run every time the product is viewed. Of course, you have to add your own API call and adapt the function to your data structure.
add_action('wp', 'update_product_data');
function update_product_data(){
if (class_exists('WooCommerce') && is_product()) {
$product = wc_get_product( get_the_ID() );
$product_id = $product->get_id();
// Call the external API to get the updated product data
$api_url = 'http://example.com/api/product/' . $product_id;
$response = wp_remote_get($api_url);
// Check for successful response and decode the JSON data
if (!is_wp_error($response) && wp_remote_retrieve_response_code($response) == 200) {
$data = json_decode(wp_remote_retrieve_body($response), true);
// Update the product's weight and description with the data from the API
$product->set_weight($data['weight']);
$product->set_description($data['description']);
$product->save();
}
}
}

Delete Gravity Forms file attachments after submission but keep all other fields?

OK, we have a job submission form on the site where users need to complete the form and attach resume and cover letter. For privacy reasons, we do not want to keep the resumes and cover letters on the server, so what we've been doing is send emails with form submission, attach resume and cover letter, and then completely delete the submission.
This is the code we've been using and everything works the way it should.
/**
*
* Target submissions from form ID 2.
*
* Change gform_after_submission_2 to reflect your target form ID,
* or use gform_after_submission to target all forms.
*
*/
add_action( 'gform_after_submission_2', 'remove_form_entry' );
function remove_form_entry( $entry ) {
GFAPI::delete_entry( $entry['id'] );
}
I was wondering if there's a way to keep the form entries but just delete the attachments?
I have just wrote the function, tested and it works fine to delete resume file.
You can add more code same way for cover letters.
add_filter( 'gform_after_submission_2', 'remove_form_entry', 10, 2 );
function remove_form_entry( $entry, $form ) {
global $wpdb;
$lead_id = $entry['id'];
$meta_key = 1; //it is the uploads field id.
$entry_table = $wpdb->prefix . 'gf_entry_meta'; //meta table of gravity forms
$get_resume_file_statement = $wpdb->prepare( "SELECT `meta_value` FROM `$entry_table` WHERE `meta_key` = %d AND `entry_id` = %d", $meta_key, $lead_id);
$meta_values_of_resume_file = $wpdb->get_col( $get_resume_file_statement ); // get uploaded csv file url
$resume_url = explode('uploads', $meta_values_of_resume_file [0]); //get file url after uploads folder. It returns file url like http://localhost/g2a/wp-content/uploads/gravity_forms/3-dbbe121585c30ed9e49ec2a6803270b0/2021/07/89636498_188586552577282_8867025936109797376_n.jpg
$resume_file_name = end($resume_url ); //get file url after uploads folder. value like /gravity_forms/3-dbbe121585c30ed9e49ec2a6803270b0/2021/07/89636498_188586552577282_8867025936109797376_n.jpg
$upload_dir = wp_upload_dir(); //uploads dir
$full_path = $upload_dir['basedir'] . $resume_file_name; //get full path of file
wp_delete_file( $full_path ); //delete the file
//update entry meta field value here or delete the entry meta.
}

Use ACF load_value hook to pre-populate the Upload File field

I'm trying to pre-populate the File upload field with files already submitted previously, using ACF PRO and ACF Frontend Form.
So far, I am able to retrieve a list of files that was previously uploaded but can't seem to figure out a way to set it in the file upload frontend.
The File upload field lives inside a Repeater group.
In the functions.php:
I added a load_value with key set to the repeater group:
add_filter('acf/load_value/key=field_5e80ed8f4516b', 'my_acf_set_file_list', 10, 3);
Then in the function I attempted to set the $value to be an array of file urls.
'field_5e80edf14516c' is the key for the File field.
function my_acf_set_file_list( $value, $post_id, $field ){
$order_id = $_GET['order'];
$value = array();
// get existing files
$existing_files = get_post_meta( $order_id, 'file', true);
if ( !empty( $existing_files ) ) {
$i = 0;
foreach ( $existing_files as $file ) {
$value[$i]['field_5e80edf14516c'] = $file;
// also tried
// $value[$i]['field_5e80edf14516c'] = $file['file'];
}
}
return $value;
}
The problem is in the frontend form, there seems to be no change at all. No file was pre-populated.
I'm pretty sure I didn't get the array structure correct, but I've searched all over and cannot find any related information.
The post data for when I upload existing files seem to indicate the attachment ID is used. But I'm not sure what is the row-0 and other id 5ebf042bba99f?
acf[field_5e80ed8f4516b]:
acf[field_5e80ed8f4516b][row-0][field_5e80edf14516c]: 381
acf[field_5e80ed8f4516b][5ebf042bba99f][field_5e80edf14516c]: 381
acf[field_5e80ed8f4516b][5ebf042cba9a0][field_5e80edf14516c]: 401
Thanks in advance!

How to get ACF data from WooCommerce customers for order export

On a Wordpress-Shop I use WooCommerce (WC) with Advenced-Custom-Fields (ACF)
and WP-All-Import (WPAI) + WP-All-Export (WPAE).
I added a ACF field CustomerNumber to the WC-Customer (which enhanced the WP-User).
On the WPAI-XML-Import I set the CustomerNumber with a value from a ERP.
So all customers have a unique CustomerNumber.
I now need to export the WC-Orders (to import them in the ERP again).
The Order-XML must include the CustomerNumber from the Customer belongs to the Order.
As I see, the other standard fields from the customer – like name and address – are copied automatically to the order (by WooCommerce itself).
My question is now: How I have to do this for the ACF’s?
Did I have to do this by code on my own? Adding the same AC-fields to the WC-Order and hook into the order checkout and copy the values from the customer to the order?
Or is there some kind of setup which do that and which I did not recognize?
Thx
I did not really found an answer.
My current solution is now as I described it in my question.
I added a new rule for these acf to also make them available on the orders.
Then I added a hook to new created orders, determine the acf from the user and copied the necessary values into the order acf.
function di_woocommerce_new_order( $order_id ) {
$order = new WC_Order( $order_id );
if ( !$order ) return;
$customer_id = $order->get_customer_id();
if ( !$customer_id ) return;
$customer = new WC_Customer($customer_id);
if ( !$customer ) return;
$customer_number = $customer->get_meta('customernumber');
if ( !$customer_number ) return;
// add customer number
$order->add_meta_data('customernumber', $customer_number);
// update order modified timestamp
$order->add_meta_data('order_last_updated', current_time( 'mysql' ));
$order->save_meta_data();
}
add_action( 'woocommerce_checkout_order_processed', 'di_woocommerce_new_order', 10, 1);

Trying to get shipping fields with a new custome field created

I am trying to get the array for all the shipping forms with a new custom element that I already added. This code is not working:
$newObj = new WC_Checkout();
$shipping_fields = $newObj->get_checkout_fields($fieldset = 'shipping');
Woocommerce WC_Checkout methods:
Visit https://docs.woocommerce.com/wc-apidocs/source-class-WC_Checkout.html#197-281 for more info!
https://superuser.com?
I get NULL so the object is not existing
In detail:
I'm developing a website, where the total price is calculated according to a delivery area in some city. I decided to create a custom field within the shipping-form:
// Adding districts for the city of Lima on shipping form
add_filter('woocommerce_checkout_fields', 'custom_district_checkout_field');
function custom_district_checkout_field($fields) {
//the list for this example was shortened
$option_cities = array(
'' =>__('Select your district'),
'chorrillos' =>'Chorrillos',
'miraflores' =>'Miraflores'
};
$fields['shipping']['shipping_district']['type'] = 'select';
$fields['shipping']['shipping_district']['options'] = $option_cities;
$fields['shipping']['shipping_district']['class'] = array('update_totals_on_change');
$fields['shipping']['shipping_district']['input_class'] = array('wc-enhanced-select');
$fields['billing']['billing_district']['type'] = 'select';
$fields['billing']['billing_district']['options'] = $option_cities;
$fields['billing']['billing_district']['input_class'] = array('wc-enhanced-select');
wc_enqueue_js("jQuery( ':input.wc-enhanced-select' ).filter( ':not(.enhanced)' ).each( function() {var select2_args = { minimumResultsForSearch:5};jQuery( this ).select2( select2_args ).addClass( 'enhanced' );})");
return $fields;
I can confirm that the custome field is working. For other hand I'm trying to change the way the WooCommerce Advance Shipping plugin works as Jeroen Sormani (who is the developer) explain in his blogs:
How the plugin works!
and WAS Shipping fields conditions
The idea is to add to the condition list the shipping fields, the plugin shows by default this fields: WC Advanced Shipping Fields by Default
The goal is to been able to select the newly created field in the conditions (for example: "districts") so the price would appear in the cart when the user select the correct option, the plugin already has a list of the different districts with their respective prices. However, there is an error in the plugin because this line is not working (check the Github for the WAS Shipping fields conditions inside the first function:
$shipping_fields = WC()->checkout()->checkout_fields['shipping'];
I have been trying to solve this for weeks, hence the original ask in this post.
/**
* WAS all checkout fields
*/
add_filter('was_conditions', 'was_conditions_add_shipping_fields', 10, 1);
function was_conditions_add_shipping_fields($conditions) {
$newObj = new WC_Checkout();
$shipping_fields = $newObj->get_checkout_fields($fieldset = 'shipping');
debugToConsole($shipping_fields);
foreach ($shipping_fields as $key => $values) :
if (isset($values['label'])) :
$conditions['Shipping Fields'][$key] = $values['label'];
endif;
endforeach;
return $conditions;
}
The above results in a NULL with the debugToConsole function.

Resources