woocommerce_email_recipient_customer_completed_order hook not being invoked - wordpress

There are a bunch of example of using this woocommerce hook woocommerce_email_recipient_customer_completed_order. So I added it to functions.php, and concatenated a couple example recipients as a test, but only the purchaser receives an email. It doesn't seem like this filter is getting invoked since if I turn on debugging and error_log(...) there's nothing in the log file.
Is there a reason this isn't working? I tried bumping the priority up to 99, but that doesn't work either. The site uses all the defaults and doesn't override any of the templates.
add_filter( 'woocommerce_email_recipient_customer_completed_order', 'custom_woocommerce_add_email_recipient', 10, 2);
function custom_woocommerce_add_email_recipient($recipient, $order) {
$recipient = $recipient . ', foo#example.com, bar#example.com';
return $recipient;
}

As I learn more about Woocommerce it looks like this site only goes as far as processing so I had to use the customer_processing_order mail ID instead to get this to work (thanks to #LoicTheAztec for verifying the customer_completed_order hook works), and instead of using the woocommerce_email_recipient_customer_processing_order hook I used woocommerce_email_headers and checked the mail ID.
add_filter( 'woocommerce_email_headers', 'woocommerce_email_headers_add_recipient', 10, 3);
function woocommerce_email_headers_add_recipient($headers, $mail_id, $order) {
if($mail_id == 'customer_processing_order') {
$member = null;
$memberMail = null;
foreach($order->get_meta_data() as $item) {
if ($item->key === 'member_name') {
$member = $item->value;
$memberMail = $this->getMemberMail($member);
$headers .= "BCC: {$member} <{$member_mail}>\r\n";
break;
}
}
}
return $headers;
}

Related

WordPress prevent delete taxonomy

I would like to prevent that some categories are accidentally deleted. For this I use a meta entry for the category to be protected.
I use the following code for this:
// edit: wrong hook! ** add_action( 'delete_term_taxonomy', 'taxonomy_delete_protection', 10, 1 );
add_action( 'pre_delete_term', 'taxonomy_delete_protection', 10, 1 );
function taxonomy_delete_protection ( $term_id )
{
if (get_term_meta ($term_id, 'delete-protect', true) === true)
{
wp_die('Cannot delete this category');
}
}
Unfortunately, instead of my error message, only "Something went wrong" is displayed. Why?
Edit: The `delete_term_taxonomy` is the wrong hook for my code, because it deleted the meta before i can check the meta entry. `pre_delete_term` does fire before anything happens with the category.
The "Why" is because of the following JavaScript that ships with WordPress:
$.post(ajaxurl, data, function(r){
if ( '1' == r ) {
$('#ajax-response').empty();
tr.fadeOut('normal', function(){ tr.remove(); });
/**
* Removes the term from the parent box and the tag cloud.
*
* `data.match(/tag_ID=(\d+)/)[1]` matches the term ID from the data variable.
* This term ID is then used to select the relevant HTML elements:
* The parent box and the tag cloud.
*/
$('select#parent option[value="' + data.match(/tag_ID=(\d+)/)[1] + '"]').remove();
$('a.tag-link-' + data.match(/tag_ID=(\d+)/)[1]).remove();
} else if ( '-1' == r ) {
$('#ajax-response').empty().append('<div class="error"><p>' + wp.i18n.__( 'Sorry, you are not allowed to do that.' ) + '</p></div>');
tr.children().css('backgroundColor', '');
} else {
$('#ajax-response').empty().append('<div class="error"><p>' + wp.i18n.__( 'Something went wrong.' ) + '</p></div>');
tr.children().css('backgroundColor', '');
}
});
The expected response to this POST request is:
'1' if the term was deleted
'-1' if your user doesn't have permission to delete the term.
For all other cases, "Something went wrong" is displayed.
You are terminating the script early with wp_die, yielding an unexpected response, which comes under "other cases".
There isn't a way to provide a custom error message in the notice box here without writing some JavaScript of your own.
This is my current solution, not perfect but it works.
The "Something went wrong" message show up if you delete the taxonomy with the row action. So i unset the "delete" action so it couldn't be triggered this way.
add_filter ('category_row_actions', 'unset_taxonomy_row_actions', 10, 2);
function unset_taxonomy_row_actions ($actions, $term)
{
$delete_protected = get_term_meta ($term->term_id, 'delete-protect', true);
if ($delete_protected)
{
unset ($actions['delete']);
}
return $actions;
}
Then i hide the "Delete" Link in the taxonomy edit form with css. It's still could be triggered if you inspect the site and it's link, but there is no hook to remove this action otherwise.
add_action( 'category_edit_form', 'remove_delete_edit_term_form', 10, 2 );
function remove_delete_edit_term_form ($term, $taxonomy)
{
$delete_protected = get_term_meta ($term->term_id, 'delete-protect', true);
if ($delete_protected)
{
// insert css
echo '<style type="text/css">#delete-link {display: none !important;}</style>';
}
}
Finally the check before deleting the taxonomy. This should catch all other ways, like the bulk action "delete". I didn't found another way yet to stop the script from deleting the taxonomy.
add_action ('pre_delete_term', 'taxonomy_delete_protection', 10, 1 );
function taxonomy_delete_protection ( $term_id )
{
$delete_protected = get_term_meta ($term_id, 'delete-protect', true);
if ($delete_protected)
{
$term = get_term ($term_id);
$error = new WP_Error ();
$error->add (1, '<h2>Delete Protection Active!</h2>You cannot delete "' . $term->name . '"!');
wp_die ($error);
}
}
This solution provides a way to disable all categories from being deleted by a non Admin. This is for anyone like myself who's been searching.
function disable_delete_cat() {
global $wp_taxonomies;
if(!current_user_can('administrator')){
$wp_taxonomies[ 'category' ]->cap->delete_terms = 'do_not_allow';
}
}
add_action('init','disable_delete_cat');
The easiest solution (that will automatically take care of all different places where you can possibly delete the category/term) and in my opinion the most flexible one is using the user_has_cap hook:
function maybeDoNotAllowDeletion($allcaps, $caps, array $args, $user)
{
if ($args[0] !== 'delete_term') return $allcaps;
// you can skip protection for any user here
// let's say that for the default admin with id === 1
if ($args[1] === 1) return $allcaps;
$termId = $args[2];
$term = get_term($termId);
// you can skip protection for all taxonomies except
// some special one - let's say it is called 'sections'
if ($term->taxonomy !== 'sections') return $allcaps;
// you can protect only selected set of terms from
// the 'sections' taxonomy here
$protectedTermIds = [23, 122, 3234];
if (in_array($termId, $protectedTermIds )) {
$allcaps['delete_categories'] = false;
// if you have some custom caps set
$allcaps['delete_sections'] = false;
}
return $allcaps;
}
add_filter('user_has_cap', 'maybeDoNotAllowDeletion', 10, 4);

Is there a hook that precedes Woocommerce API's duplicate SKU check?

I have a site with Woocommerce and WPML + Multilingual Woocommerce installed. My problem is that I try to insert a product as a translation of a previously entered product without being aware of the ID of the main product. If I enter the ID as translation_of it works; both products share the same SKU and the translation has the SKU field disabled, which is how I want it to work. But I don't want to enter translation_of into the data that gets sent to Woocommerce. I want to only use the SKU and then let Wordpress first check if a product with that SKU already exists and replace sku with translation_of if it does.
This is how I went about it:
add_filter('woocommerce_api_create_product_data', '__create_product_data', -100, 2);
function __create_product_data($data, $api) {
if(isset($data['sku']) && $product_id = wc_get_product_id_by_sku($data['sku'])) {
$product_id = apply_filters('wpml_object_id', $product_id, 'product');
$data['translation_of'] = $product_id;
unset($data['sku']);
}
return $data;
}
But it seems to me that execution arrives at this point long after the SKU has been checked, because I noticed that I can return nothing and I still get product_invalid_sku error back. What would be the correct hook or does such a hook even exist?
My own solution:
add_filter('rest_pre_dispatch', '__rest_pre_dispatch', 10, 3);
function __rest_pre_dispatch($result, $server, $request) {
$sku = $request->get_param('sku');
if ($sku) {
$id = wc_get_product_id_by_sku($sku);
if ($id) {
$product_id = apply_filters('wpml_object_id', $id, 'product');
$request->set_param('translation_of', $product_id);
$request->offsetUnset('sku');
}
}
return $result;
}

woocommerce_update_product action – fire only once for every product update

Which workaround is there for making the woocommerce_update_product action fire only once?
I've read that it fires twice because it needs to save once internally to retrieve an ID for images/variation saves.
But from a developer experience, this is really not what most need, I guess.
The only workaround I've found so far is to add the action and remove it in the hook directly:
add_action('woocommerce_update_product', 'my_product_update', 10, 2);
function my_product_update($product_id, $product){
remove_action('woocommerce_update_product');
// We'll get here only once!
}
However, this breaks when trying to do bulk edits, making it so that the hook only fires for the first product (because it gets removed afterwards!).
Which other way is there to work around this issue?
Thanks!
Maybe using WordPress Transient can help.
add_action('woocommerce_update_product', 'my_product_update', 10, 2);
function my_product_update($product_id, $product) {
$updating_product_id = 'update_product_' . $product_id;
if ( false === ( $updating_product = get_transient( $updating_product_id ) ) ) {
// We'll get here only once! within 2 seconds for each product id;
// run your code here!
set_transient( $updating_product_id , $product_id, 2 ); // change 2 seconds if not enough
}
}
Use a global variable to do this in memory, no database entry required:
add_action('woocommerce_update_product', 'my_product_update', 10, 2);
function my_product_update($product_id, $product){
global $previous_product_id;
if ($previous_product_id === $product_id){
// We'll get here only once (per product)!
}
$previous_product_id = $product_id;
}
i ran into the same issue (actually my hook was firing 5 times). I found a solution on the following page in the last comment.
Namely, the use of:
$times = did_action('woocommerce_update_product');
if( $times === 1){
// Do some stuff
}
A simpler solution would be to set a $_POST variable and check at the start of the function.
add_action( 'woocommerce_update_product', 'my_action', 10, 1 );
function my_action(){
if( isset($_POST['action_performed']) ){
//Prevent running the action twice
return;
}
// do you stuff here
$_POST['action_performed'] = true;
}

Polylang automatic sync for posts

I'm working on a multilingual wordpress website using the Polylang plug-in on pages and custom post types.
What I'm looking for is a way to have every post synch automatically, without user input. When creating a new post type, a translation would be automatically created and all contents copied.
So the user wouldn't see this panel at all, or at least not have the chance to edit the translation or (especially) turn the sync off. I guess this could be done by changing user roles privileges but the post would definitely have to automatically sync.
I checked this article but it didn't do anything.
Needed something similar, dug up this undocumented function:
global $polylang;
// third parameter sets synchronisation
$polylang->sync_post_model->copy_post($post_id, $lang, true);
This duplicates the content to the chosen language and enables synchronisation.
Example using 'save_post:
function auto_translate($post_id, $post, $update)
{
if (!$update) {
return;
}
// prevent recursion when publishing translations
remove_action('save_post', 'auto_translate', 999, 3);
global $polylang;
$langs = ['nb', 'se', 'dk'];
$current_translations = pll_get_post_translations($post_id);
foreach ($langs as $lang) {
if (!isset($current_translations[$lang])) {
$polylang->sync_post_model->copy_post($post_id, $lang, true);
}
}
}
// needs low priority or the synchronisation option wont be saved
add_action('save_post', 'auto_translate', 999, 3);
The solution offered by steinoy is good but generates an additional unwanted draft post. Here's an improved solution:
function auto_translate($post_id, $post, $update)
{
if (!$update) {
return;
}
// prevent creation of additional duplicate draft posts
if ( $post->post_status == 'draft' ){
return;
}
// prevent recursion when publishing translations
remove_action('save_post', 'auto_translate', 999, 3);
global $polylang;
$langs = ['nb', 'se', 'dk'];
$current_translations = pll_get_post_translations($post_id);
$post_type = get_post_type($post_id);
if ($post_type == 'post') {
foreach ($langs as $lang) {
if (!isset($current_translations[$lang])) {
$polylang->sync_post_model->copy_post($post_id, $lang, true);
}
}
}
}
// needs low priority or the synchronisation option wont be saved
add_action('save_post', 'auto_translate', 999, 3);

Wordpress additional login rules

I made a plugin that requires the user to validate their email address. It creates "activation_key" meta key with random string for the user and sets it to '' once the user validates. So far so good. But now I need to hook into login and check that activation_key == ''.
This is what I thought should be, but it doesn't get to here.
add_filter( 'authenticate', 'check_if_activated', 10, 3 );
function check_if_activated($user, $username, $password)
{
// check if user has activated their email
return $user;
}
Wordpress already adds filters to authenticate, so you have to register your lower.
add_filter( 'authenticate', 'check_if_activated', 50);
function check_if_activated($user)
{
// If we have an error, no need to check the activation key
// (Wrong credentials, for instance)
if (is_wp_error($user))
{
return $user;
}
// Checks the meta and returns an error if needed
$validation_key = get_user_meta($user->ID, 'activation_key', true);
return empty($validation_key) ? $user : new WP_Error('your_plugin_error_code', 'your error message');
}

Resources