Woocommerce change order download files - woocommerce

I am trying to manipulate the download URL link for files inside an order.
Basically, I have a use case when a download product is bought I need to add files to the order to be able to download it. So I cannot add an order ID to a product because there is no order at that moment.
Point is, every downloadable product needs to have a dynamic link because it will be generated on the fly .
So how can I add data to the product download URL link ->

WooCommerce offers 2 filters that can be used to achieve what you are looking for:
A) woocommerce_download_product_filepath used to specify what URL contains the file to be downloaded and it takes the following parameters:
#param string $file_path File path
#param string $email_address Email address
#param WC_Order|bool $order Order object or false
#param WC_Product $product Product object
#param WC_Customer_Download $download Download data
And it is registered like this:
add_filter( 'woocommerce_download_product_filepath', 'change_woocommerce_file_download_path', 10, 5 );
More info here https://wp-kama.com/plugin/woocommerce/hook/woocommerce_download_product_filepath
B) woocommerce_file_download_filename used to manipulate the downloaded file's name and it takes the following parameters:
#param string $filename Downloadable file name.
#param int $product_id WC product id
And it is registered like this:
add_filter( 'woocommerce_file_download_filename', 'change_woocommerce_file_download_filename', 10, 2 );
More info here https://wp-kama.com/plugin/woocommerce/hook/woocommerce_file_download_filename

Related

wpallimport - how do I replace sku with product id?

I use a feed from a manufacturer to upload to a WooCommerce custom field.
The custom field expects a product id as input, but the feed is not aware of this as it's created by WooCommerce.
I use SKU as the key to compare existing products with the feed.
The imported field is called {metaparent_sku[1]} and the content has several values separated by | , for example:
BDCD7 | BDAUX | BDCA1 | ADUP89
These are unique product sku’s referring to products already in WooCommerce.
What I’d like to do is replace the sku value with the corresponding product ID value in as it exists already in WooCommerce.
I’ve been trying to use str_replace but can’t figure out how to set it up, if that's possible at all. Or should I use lookup?
If anyone could help with some sample code I'd be very grateful.
To find the product ID by a sku field.
[wc_get_product_id_by_sku({sku[1]})]
So in your case that would be:
[wc_get_product_id_by_sku({metaparent_sku[1]})]
For when you have multiple sku's in one field make a custom function.
Place the following function in the Function Editor.
/**
* Find product id(s) by sku(s)
* #param string $sku
* #return string with product id(s)
*/
function wp_all_import_get_product_ids_by_sku($sku, $seperator = "|") {
// define empty $product_ids
$product_ids = array();
// remove spaces from sku
$sku = str_replace(" ", "", $sku);
// convert to array
$skus = explode($seperator, $sku);
// find product ids
foreach ($skus as $sku) {
$product_ids[] = wc_get_product_id_by_sku($sku);
}
// return product ids
return implode(" | ", $product_ids);
}
Then use it like this
[wp_all_import_get_product_ids_by_sku({metaparent_sku[1]})]

WooCommerce renaming a digital download file

I've been using woocommerce to sell a few digital goods. I have set the following setting "Append a unique string to filename for security" And I was wondering if there is a way to rename the file back to it's original name when the customer downloads it.
For example the filename is product-9j978f.zip and when the customer downloads it would be renamed to product.zip instead.
Thank you and any help would be gladly appreciated
Here is the code
/**
* Change the downloadable file name
*
* #param string $filename Downloadable file name.
* #param int $product_id WC product id.
* #return string
*/
function change_woocommerce_file_download_filename( $filename, $product_id ) {
// Get the file extension from the processed file name.
$filetype = wp_check_filetype( $filename );
// You can generate a new file using Product ID if you need to.
$new_file_name = 'New File Name';
// Join the new file name with file extension with (.).
return join( '.', array( $new_file_name, $filetype['ext'] ) );
}
add_filter( 'woocommerce_file_download_filename', 'change_woocommerce_file_download_filename', 10, 2 );
You get 2 parameters in this filter hook. 1: Filename 2: Product ID then you can do the name change according to your need and return the file name.
NOTE: new file name should have a file extension with it.

Woocommerce get items in the orders before it is process on checkout

I am implementing syncing feature between WordPress and my other site. I want to sync orders to my other site before it is processed during checkout. I needed an items to be sync too, but I don't know what hook to used for this approach.
I tried using woocommerce_checkout_order_processed the order is already processed and save on the database. I want a hook that will only provide me some information about the order like booking, items and order data before it is being saved on the database. I needed this because if there is some errors during the sync I can cancel the order and it won't be save on the WordPress database.
I tried woocommerce_checkout_process hook. The problem is I can't get the items using this code.
function syncOrderAsEstimates($order_id) {
$order = wc_get_order($order_id);
$order->getItems(); // returns empty
}
add_action('woocommerce_checkout_process', 'syncOrderAsEstimates', 10, 1);
#Yves Try using this action hook(/includes/class-wc-checkout.php) :
/**
* Action hook to adjust order before save.
*
* #since 3.0.0
*/
do_action( 'woocommerce_checkout_create_order', $order, $data );
Thanks

How to modify uploaded file URL in Gravity Forms using "gform_save_field_value" filter

I want to modify the uploaded file URL before saving to database when user submits the Gravity form.
I am trying to acheive this by using "gform_save_field_value" filter in Gravity form. But when i print fields inside the filter function i never get the file upload field ID. All other fields are accessable inside filter function except file upload input field.
add_filter( 'gform_save_field_value', 'save_field_value', 10, 4 );
function save_field_value( $value, $lead, $field, $form ) {
print_r($field);
}
The gform_upload_path filter will allow you to change the upload path of the file (and the URL).
add_filter( 'gform_upload_path', 'change_upload_path', 10, 2 );
function change_upload_path( $path_info, $form_id ) {
$path_info['path'] = '/home/public_html/yourdomainfolder/new/path/';
$path_info['url'] = 'http://yourdomainhere.com/new/path/';
return $path_info;
}
If you need to change the actual name of the file, here is a snippet that makes it a cinch.
http://gravitywiz.com/rename-uploaded-files-for-gravity-form/

Change wordpress gallery shortcode slug to filename

Is it possible to change the attachment page slug to the referring filename? In short... i use the Gallery Shortcode to build a simple page based gallery.
I change the orignal filename (like DSC1223.jpg) during the upload process (to 3b1871561aab.jpg) but it does not appery as slug within the url. It uses only DSC1223.
Is there anyway to change ist?
Regards, Steve
The best way would be to write something like this in my functions.php
function twentyten_filter_wp_handle_upload () {
$upload = wp_handle_upload();
}
add_filter( 'wp_handle_upload', 'twentyten_filter_wp_handle_upload', 10, 2);
Add this to the hash upload filename plugin and you should be good to go;
/**
* Filter new attachments and set the post_name the same as the hashed
* filename.
*
* #param int $post_ID
*/
function change_attachment_name_to_hash($post_ID)
{
$file = get_attached_file($post_ID);
$info = pathinfo($file);
$name = trim( substr($info['basename'], 0, -(1 + strlen($info['extension'])) ) );
wp_update_post(array(
'ID' => $post_ID,
'post_name' => $name
));
}
add_action('add_attachment', 'change_attachment_name_to_hash');
Don't hesitate to ask if you're not sure what each line does!
UPDATE:
This function hooks onto the add_attachment event, just after a new attachment is saved to the database. This action is called from within wp_insert_attachment().
We first grab the filename of the attachment (get_attached_file()). Then we use a native PHP function pathinfo() to get the path components, and strip the directory path and file extension away.
Then we call wp_update_post(), updating the post_name of the attachment in the database.

Resources