email when images change
-
I am attempting to send out an email anytime a woocommerce product has its images changed in any way.
I started off by attempting to get the $_POST of the image gallery, but I think I am calling it the wrong thing… I have tried the following:
$images = $_POST['image_ids']; $images = $_POST['gallery_image_ids']; Then I am comparing that value to the current image ids: $oldImages = $product->get_gallery_image_ids(); If they do not match I am using wp_mail to send an email to various people. I am doing all this inside a custom php function using: add_action('pre_post_update', 'content_change_email', 10, 2); function content_change_email($post_ID, $data) { }
I assume the problem is originating with the
$images = $_POST['image_ids']; $images = $_POST['gallery_image_ids'];
part of the code.
Can someone let me know what the correct meta field name for the gallery images is?
- This topic was modified 3 years, 4 months ago by Jan Dembowski.
-
I hope this could help you:
// Add the Meta Box function shift8_portfolio_add_custom_meta_box() { add_meta_box( 'custom_meta_box', // $id 'Shift8 Portfolio Fields', // $title 'shift8_portfolio_show_custom_meta_box', // $callback 'post', // $page 'normal', // $context 'high'); // $priority } add_action('add_meta_boxes', 'shift8_portfolio_add_custom_meta_box');
Create A Field Array For The Gallery
// Field Array $prefix = 'shift8_portfolio_'; $custom_meta_fields = array( array( 'label'=> 'Main Image', 'desc' => 'This is the main image that is shown in the grid and at the top of the single item page.', 'id' => $prefix.'image', 'type' => 'media' ), array( 'label'=> 'Gallery Images', 'desc' => 'This is the gallery images on the single item page.', 'id' => $prefix.'gallery', 'type' => 'gallery' ), );
Create A Callback Function For The Meta Box To Display The Fields
// The Callback function shift8_portfolio_show_custom_meta_box($object) { global $custom_meta_fields, $post; // Use nonce for verification echo ''; // Begin the field table and loop echo ''; foreach ($custom_meta_fields as $field) { // get value of this field if it exists for this post $meta = get_post_meta($post->ID, $field['id'], true); // begin a table row with echo ''; } // end foreach echo ' '.$field['label'].' '; switch($field['type']) { case 'media': $close_button = null; if ($meta) { $close_button = ''; } echo ' ' . $close_button . '
break; case 'gallery': $meta_html = null; if ($meta) { $meta_html .= ' '; $meta_array = explode(',', $meta); foreach ($meta_array as $meta_gall_item) { $meta_html .= ' '; } $meta_html .= ' '; } echo ' ' . $meta_html . ' '; break; } //end switch echo ' '; // end table }
No this does not help what I am trying to achieve in any way. I do not want to create another meta field for display.
To recap:
An employee changes the images (adds, deletes, adds and deletes), one or all of the images.
Then BEFORE the product is saved, it compares the post value of the image gallery to the current image gallery value, if they are different (they have been changed) an email is sent to someone.I want to model it after this currently working code that notifies people when changes have been made to the product description:
<?php // code to document content changes to products (underlines additions, strikethrough removals) add_action('pre_post_update', 'content_change_email', 10, 2); function content_change_email($post_ID, $data) { function diff($old, $new) { $matrix = array(); $maxlen = 0; foreach($old as $oindex => $ovalue) { $nkeys = array_keys($new, $ovalue); foreach($nkeys as $nindex) { $matrix[$oindex][$nindex] = isset($matrix[$oindex - 1][$nindex - 1]) ? $matrix[$oindex - 1][$nindex - 1] + 1 : 1; if($matrix[$oindex][$nindex] > $maxlen) { $maxlen = $matrix[$oindex][$nindex]; $omax = $oindex + 1 - $maxlen; $nmax = $nindex + 1 - $maxlen; } /* close if($matrix */ } /* close foreach($nkeys */ } /* close foreach($old */ if($maxlen == 0) return array(array('d'=>$old, 'i'=>$new)); return array_merge( diff(array_slice($old, 0, $omax), array_slice($new, 0, $nmax)), array_slice($new, $nmax, $maxlen), diff(array_slice($old, $omax + $maxlen), array_slice($new, $nmax + $maxlen))); } /*close function diff */ function htmlDiff($old, $new) { $ret = ''; $diff = diff(preg_split("/[\s]+/", $old), preg_split("/[\s]+/", $new)); foreach($diff as $k) { if(is_array($k)) $ret .= (!empty($k['d'])?"<del>".implode(' ',$k['d'])."</del> ":'').(!empty($k['i'])?"<ins>".implode(' ',$k['i'])."</ins> ":''); else $ret .= $k . ' '; } /* close foreach($diff */ return $ret; } /* close function htmlDiff */ $type = get_post_type( $post_ID ); // get post type if ( $type == 'product' ) { // if post type is product $content = $_POST['content']; $product = wc_get_product( $post_ID ); $oldcontent = $product->post->post_content; $status = $product->get_status(); if ( $status == "publish" ) { if ( $content != "" ) { if ( $oldcontent != $content ) { $msg = htmlDiff($oldcontent, $content); $memail = " <style type='text/css'> ins { background-color:#ccffcc; } del { background-color:#ffcccc; } </style> <table> <tr> <td>".$msg."</td> </tr> </table>"; //$file = file_put_contents("changes.html", $email); *** send email not write to file $headers = array('Content-Type: text/html; charset=UTF-8'); global $current_user; wp_get_current_user(); $email = $current_user->user_email; $lastuser = $current_user->user_firstname; if ( $product->get_sku() != "" ) { $memail = "USER: " . $lastuser . " made the following changes: Additions will have underlining. Subtractions will be strikethrough. " . $memail; $subject = "CONTENT CHANGED - SKU: " . $product->get_sku(); $to = ""; wp_mail( $to, $subject, $memail, $headers ); } /* close if ( $product->get_sku() */ } /* close if ( $oldcontent */ } /* close if ( $content */ /* check if image ids have changed $images = $_POST['image_ids']; $oldImages = $product->get_gallery_image_ids(); if ( $images != "" ) { if ( $oldImages != $images ) { } }*/ } /* close if ( $status */ } /* close if ( $type */ remove_action('pre_post_update', 'content_change_email', 10, 2); } ?>
You can store the product image and product gallery before the post is saved and check it again after. These two hooks enable you to compare them.
add_action('pre_post_update', 'wpsf_pre_post_update'); function wpsf_pre_post_update($post_ID) { if (get_post_type($post_ID) == 'product') { error_log('old product image: ' . get_post_thumbnail_id($post_ID)); error_log('old product gallery: ' . print_r(get_post_meta($post_ID, '_product_image_gallery'), true)); } } add_action('wp_after_insert_post', 'wpsf_wp_after_insert_post', 10, 3); function wpsf_wp_after_insert_post($post_id, $post, $update) { if (get_post_type($post_id) == 'product' && $update) { error_log('new product image: ' . get_post_thumbnail_id($post_id)); error_log('new product gallery: ' . print_r(get_post_meta($post_id, '_product_image_gallery'), true)); } }
To view the debugging output, place these two lines in your wp-config.php file and watch for changes to
/wp-content/debug.log
when you update a product.define('WP_DEBUG', true); define('WP_DEBUG_LOG', true);
I solved my own problem by using something similar to what rickymccallum87 suggested…
I used a custom meta field to record the IDs for all the images, including the featured image then compared that to the current gallery images to see if they had changed, if they did then update the custom meta value with the new numbers and send an email to let people know the pictures changed.code is as follows:
// send email when images change for a product add_action( 'woocommerce_update_product', 'imagecheck_on_product_save', 10, 1 ); function imagecheck_on_product_save( $product_id ) { $product = wc_get_product( $product_id ); // do something with this product $metaImages = get_post_meta( $product_id, '_gallery_image_ids', true ); $currentImages = $product->get_gallery_image_ids(); $imageIDs = get_post_thumbnail_id( $product_id ); if ($imagesIDs != "" ) { $imageIDs = $imageIDs . ", "; foreach($currentImages as $value){ $imageIDs = $imageIDs . $value . ", "; } if ( $metaImages != $imageIDs ) { update_post_meta( $product_id, '_gallery_image_ids', esc_attr( $imageIDs ) ); global $current_user; wp_get_current_user(); $email = $current_user->user_email; $lastuser = $current_user->user_firstname; if ( $product->get_sku() != "" ) { $memail = "<pre>USER: " . $lastuser . " made the following changes:</br> Meta gallery images: " . $metaImages . "</br> </br> Actual gallery images: " . $imageIDs . " " . $memail . "</pre>"; $headers = array('Content-Type: text/html; charset=UTF-8'); $subject = "PICTURES CHANGED - SKU: " . $product->get_sku(); $to = "(insert who you want to send to here)"; wp_mail( $to, $subject, $memail, $headers ); } } } }
- This reply was modified 3 years, 4 months ago by jedidiah13.
- This reply was modified 3 years, 4 months ago by jedidiah13.
see above reply
- The topic ‘email when images change’ is closed to new replies.