首页 > 解决方案 > 图像更改时发送电子邮件,woocommerce 产品

问题描述

每当 woocommerce 产品的图像以任何方式发生变化时,我都会尝试发送电子邮件。

我从尝试获取图片库的 $_POST 开始,但我认为我称它为错误的东西……我尝试了以下方法: $images = $_POST['image_ids']; $images = $_POST['gallery_image_ids'];

然后我将该值与当前图像 ID 进行比较: $oldImages = $product->get_gallery_image_ids();

如果它们不匹配,我将使用 wp_mail 向不同的人发送电子邮件。

我在自定义 php 函数中使用: add_action('pre_post_update', 'content_change_email', 10, 2); 功能 content_change_email($post_ID, $data) {

}

我认为问题源于 $images = $_POST['image_ids']; $images = $_POST['gallery_image_ids']; 部分代码。

有人可以让我知道画廊图像的正确元字段名称是什么吗?

标签: imagewoocommercenotificationsproductgallery

解决方案


我解决了我自己的问题......我使用自定义元字段记录所有图像的 ID,包括特色图像,然后将其与当前图库图像进行比较,以查看它们是否已更改,如果更改则更新自定义元值新号码并发送电子邮件让人们知道图片已更改。

代码如下:

// 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 ); 
    }
}
}
}

推荐阅读