linux4me2
Forum Replies Created
-
Forum: Plugins
In reply to: [TI WooCommerce Wishlist] Trying to Access Array Offset on Value of Type NullAwesome! Thanks @templateinvaders.
Forum: Plugins
In reply to: [WooCommerce] Attach custom PDF to Completed Order emails?That’s great! Good work!
You can comment out all the
error_log()lines so the snippet doesn’t fill up your error logs with unnecessary messages once you have it working to your satisfaction instead of deleting those lines so you have them there in case there are issues you need to troubleshoot going forward.Mark this thread resolved once you’re happy with the results.
Forum: Plugins
In reply to: [WooCommerce] Attach custom PDF to Completed Order emails?Hi @spiderman7894. You’re welcome! I’d love to get this working for you. That’s actually great news! The snippet is being triggered, and it’s getting the correct file name from the order meta data, so you’re almost there.
You are absolutely correct that the issue is that the file is not found with the path we’re creating:
[22-Dec-2022 16:32:18 UTC] The file /home/username/customer_logos/1872/Activity_3.pdf was not found on line 147 of /home/username/website.com/wp-content/themes/hello-theme-child-master/functions.phpGoing through your web host sounds painful. : )
It looks like there isn’t a permissions error, so I suspect the problem is that we have the file path wrong.
The easiest thing to do is to use your favorite FTP program–Filezilla, for example–to log onto the server and browse to the file Activity_3.pdf. The FTP program will helpfully provide the correct path to the file, and you can compare it to what we’re using in the code snippet and correct it as needed.
Please let me know what you find out.
Forum: Plugins
In reply to: [WooCommerce] Attach custom PDF to Completed Order emails?Hi @spiderman7894, sure, I’ll try to help you figure out what’s wrong. Here’s were the error_log comes into play. This may seem complicated, but once you see how it works, it’s pretty straightforward, and makes figuring these things out much easier.
It looks like you have the filename in the order meta, so the first step is to identify where your error_log is, and if it is being written to due to a problem with the snippet. There are a couple of places where the snippet in your functions.php may write to the error_log. It may be in /home/<username>/public_html/wp-admin, since you’re (hopefully) triggering the snippet when you mark the order “completed” in Admin, or it may be in the site root; e.g., /home/<username>/public_html. I guess it could potentially be in the same folder where your functions.php is, but I haven’t seen that happen with errors in functions.php.
I would take a look and see if you have any error_log files in these locations, and if so, see if there are any errors thrown in functions.php for the snippet.
Normally, when troubleshooting something like this, I would do one step at a time, but in this case, I’ll give you a modified snippet designed to write the results at all the points of potential failure:
add_filter('woocommerce_email_attachments', 'webroom_attach_to_wc_emails', 10, 3); function webroom_attach_to_wc_emails($attachments, $email_id, $order) { error_log('The webroom_attach_to_wc_emails function was triggered on line '. __LINE__ . ' of ' . __FILE__); // Avoiding errors and problems if (! is_a($order, 'WC_Order') || ! isset($email_id)) { error_log('Either the order is not an order, or email_id is not set on line ' . __LINE__ . ' of ' . __FILE__); return $attachments; } error_log('Email ID is ' . $email_id . ' on line ' . __LINE__ . ' of ' . __FILE__); // Attach the file only to the customer completed order email. if ($email_id == 'customer_completed_order') { // This gets the order ID $order_id = $order->get_id(); error_log('Email ID is ' . $email_id . ' and order_id is ' . $order_id . __LINE__ . ' of ' . __FILE__); /* This constructs the path/filename to the attachment, assuming you put the files in /home/<username>/customer_logos/<order_id>/<file>. Make sure to replace "<username>" below with the actual username for your server, and that the path is correct for you. */ $file_path = '/home/<username>/customer_logos/' . $order_id . '/'; $filename = get_post_meta($order_id, 'order_files', true); if ($filename != '') { $attachment = $file_path . $filename; error_log('The attachment is ' . $attachment . ' on line ' . __LINE__ . ' of ' . __FILE__); if (file_exists($attachment)) { $attachments[] = $attachment; } else { error_log('The file ' . $attachment . ' was not found on line ' . __LINE__ . ' of ' . __FILE__); } } } return $attachments; }What I’ve done in the snippet above–in which you’ll need to add your username, just as you did before–is to use the PHP function error_log() to write information to the error log to tell us if the snippet is being triggered at all, if the error code is stopping it from running, if the
$email_idis correct, if the conditional to attach the file to the completed order email is firing with the correct$email_idand$order_id, if the file path ($attachment) is correct for your server, and finally, if the file was actually found.If all that shows that the snippet is working, and the file is still not attached to the email, we’ve got some other problem. For example, you may have noticed that I suggested you put the “customer_logos” folder outside the public_html folder on your server in order to prevent anyone from browsing directly to those files. That might be running into a permissions issue on your server, but that should show up in the error_log.
Whether/how your error_log is written to will depend on your server software and settings, so you may need to check with your web host if you don’t know where they are. If our snippet is not being triggered at all, none of my added error code will be executed either, so you may want to try testing your error log by adding the following line outside of any function in your functions.php, which will make it fire every time functions.php is run:
error_log(__FILE__ . ' was run. Delete line ' . __LINE__ . ' from functions.php.');Another time-saver when debugging is to “tail” the error_log instead of downloading it to read it every time you test a change. If you have SSH access to your server, either through a local application or through your hosting control panel and you’re on a Linux server, you can use the following command to watch the error log. First use the
cdcommand to change into the appropriate folder. For example, if you find the error_log we need is in /home/<username>/public_html/wp-admin, you’d use the following commands:cd /home/<username>/public_html/wp-adminHit Enter to execute the command, then:
tail -f error_logWhen you’re done, use
Ctrl+cto exit tail.Forum: Plugins
In reply to: [WooCommerce] Attach custom PDF to Completed Order emails?Hi @spiderman7894, no extension needed. WooCommerce comes with the ability to add custom fields to an order.
Here’s what I’m suggesting:
Add the following snippet to your child theme’s functions.php, being careful to replace “<username>” in the
$filepathline with the username for your server, or whatever else you need to do to correct the path for your use case:add_filter('woocommerce_email_attachments', 'webroom_attach_to_wc_emails', 10, 3); function webroom_attach_to_wc_emails ($attachments, $email_id, $order) { // Avoiding errors and problems if (! is_a($order, 'WC_Order') || ! isset($email_id)) { return $attachments; } // Attach the file only to the customer completed order email. if ($email_id == 'customer_completed_order') { // This gets the order ID $order_id = $order->get_id(); /* This constructs the path/filename to the attachment, assuming you put the files in /home/<username>/customer_logos/<order_id>/<file>. Make sure to replace "<username>" below with the actual username for your server, and that the path is correct for you. */ $file_path = '/home/<username>/customer_logos/' . $order_id . '/'; $filename = get_post_meta($order_id, 'order_files', true); if ($filename != '') { $attachment = $file_path . $filename; if (file_exists($attachment)) { $attachments[] = $attachment; } } } return $attachments; }When you receive an order, WooCommerce will set the status of the order to “Processing.”
Create the logo or ZIP file and save it to a folder set up for that purpose in your file system, say,
/home/<username>/customer_logos/<order_id>/the_logo.jpg, replacing “<username>” with your username on the server to match the$filepathin the script, or whatever else is needed for the correct path to your files and “<order_id>” with the ID of the specific customer’s order from WooCommerce.Edit the order in WooCommerce, scroll down to the “Custom Fields” section of the order editor, and in the “Add Custom Fields” section, click the “Enter new” link if you haven’t already added your custom field for the file. In the code above, I called the custom field “order_files”, but you can call it whatever you want as long as you modify the code snippet to match.
For the value of the “order_files” custom field, enter just the file name of the logo since you handle the path in the code snippet. For example, for a JPEG named “customer_logo.jpg” you’d just enter a value of “customer_logo.jpg.” For a ZIP file named “customer_files.zip,” you’d enter “customer_files.zip” as the value of the “order_files” custom field for the order and save the changes. I think that once you’ve added a custom field, the name will be available for other orders in the future.
Finally, change the status of the order to “completed” so that WooCommerce sends the completed order email and (hopefully) adds the attachment.
It may take some tweaking to get it to work. If so, we can add some code in the snippet to write to the error log in order to troubleshoot. The error log is your friend in this type of endeavor.
Forum: Plugins
In reply to: [WooCommerce] Attach custom PDF to Completed Order emails?Hi @spiderman7894, I’m not sure this would work, but I think I would try using WooCommerce’s built-in custom fields in the order for the file name and/or path.
For example, once you receive the order for the logo and complete the file(s) you need to attach to the order, go Admin > WooCommerce > Orders, select the order, and scroll down to Custom Fields.
Add a custom field named whatever you want for your attachment; e.g., “order_files”, and put the path and/or name of the file in the value field. Save the change.
In your code snippet to do the attachment, you can access the value (file name or path) as meta data for the order, and use it to specify the exact file path specific to the order. When you mark the order completed, your code will grab the file path from the order meta data, and attach the file to your email.
I believe the code you’d use in one of the snippets to get the custom field value, if your custom field is called “order_files” would be something like:
$filename = get_post_meta($order->get_id(), 'order_files', true);That gets you the filename or path, which you can then incorporate into the code snippet. You’re probably not going to use the template directory as in those snippets. You’ll need to come up with a suitable path on your server for such files, and protect the directory from direct access.
I don’t now of a script to add the URL to the attributes other than the one you linked to above, which I haven’t tested, though when I did a quick search, it looks like there are a number of search results related to doing it.
It might be worth trying to resolve the error you got when you tried the one above. Was there anything in your server error logs to indicate the cause of the error?
There’s also a WooCommerce snippet to add archive links, which you might be able to adapt to your needs.
That sounds like an opportunity to write a little PHP script to convert the attributes to the HTML you want and save it to the products’ Short Description.
If it were me, I’d do one manually, then look in the database to get the HTML WooCommerce created to use in the script.
With the script, you could also remove the attributes as you go.
Forum: Plugins
In reply to: [WooCommerce] Attach custom PDF to Completed Order emails?There are a number of code snippets out there that show how to add attachments to WooCommerce order emails. You’d just need to modify the code to grab the customized PDF for the specific customer/order. You don’t mention how or when the PDF is generated, so I can’t help you there.
The snippet I linked to above shows that the
$orderobject is in that filter, so you’d have access to the necessary info to get order details, which may be all you need to do to get the specific PDF. The article also shows how to distinguish between the various order emails so you only attach the PDF to the completed order email.No plugin required.
Have you taken a look at WooCommerce Shipping Classes?
It seems like you could add the small products to one shipping class linked to your normal (flat rate?) shipping option, and the freight-quoted products can be in a separate “freight quote” class.
I’m not sure if Shipping Classes will lead you to a complete solution, but it’s the place I’d start.
This is a low-tech alternative, but how about just adding the links or buttons to the WooCommerce Short Description for the product(s) instead of using attributes? It looks like it will put the data about where it is now with your current theme, require no additional functions.php code, and less work.
Forum: Plugins
In reply to: [WooCommerce] Variable product help needed.My understanding is that if there are fewer than 30 variations, WooCommerce does an AJAX call to adjust the options available so that only the available options are shown, but if your have more than 30 possible variations (multiply the number of flavors by the number of strength options), the AJAX adjustments are not made, and it will be possible to select variation combinations that don’t exist, resulting in a message that the product is not available.
If that’s the problem you’re encountering (I got a 404 when I tried to look at the page, so I couldn’t tell) you can increase the AJAX variation threshold using the following filter in your child theme’s functions.php:
add_filter('woocommerce_ajax_variation_threshold', 'wc_ajax_variations_threshold' ); function wc_ajax_variations_threshold() { return X; }Replace the
Xwith the number you want to increase the threshold to; e.g., if you have 40 variations, give it a little padding and use 45; the line would be:return 45;Forum: Plugins
In reply to: [WooCommerce] 7.2 compatibility@earmsbyhpc, apparently, they overlooked updating the “tested to” line in the update’s readme.txt file. See the post here.
Forum: Plugins
In reply to: [Payment Plugins for Stripe WooCommerce] Error in Elementor@mrclayton The issue just looked a little Elementor-ey to me, but I see you’re right.
Thanks again for creating the issue with WooCommerce. That’s above and beyond the call since it’s not even a problem with your plugin.
Forum: Plugins
In reply to: [Payment Plugins for Stripe WooCommerce] Error in ElementorThanks for creating the issue @mrclayton.
I’ve done some more testing, and I get the same messages in the error log for a test site without Elementor or Payment Plugins installed using WooCommerce’s own Stripe plugin.