十一年专注,只做WordPress定制开发一件事

WordPress更新文章时获取更新前后的数据

王超
2023-12-13
主题开发
303 次

最近在给客户开发WordPress与Shopify互通插件的过程中,我们需要在产品价格改变的时候发送邮件给用户邮箱,这就需要我们在更新产品的时候,能够获取到更新前的价格和更新后的价格,然后进行对比,如果不同就发送邮件,如果相同则不发送。

但是查询了WordPress所有跟内容更新相关的钩子,发现没有一个钩子可以让我们能够获取到更新之前的信息,但经过多次测试,有一个钩子引起了我的注意,那就是save_post这个钩子,其实这个钩子也是大家首先能想到的钩子,但是默认的这个钩子传递的三个参数$post_id,$post,$update是没有办法直接获取更新前后的数据进行对比的。

但是,在我测试的时候发现,这个钩子对应的回调函数,每次当保存文章或产品的时候会被执行两次,经过继续测试,发现执行的两次,第一次执行的时候,通过post_id获取到的数据是更改前的,第二次执行的时候就是更改后的了,到此,我们的需求就可以得到解决了,而且实现的方法不止一种,我们选择了一种,当第一次执行的时候,获取的产品的价格,然后保存到一个自定义字段中,比如old_sale_price,当然,保存之前先判断改产品是否已经有这个字段及字段儿值了,如果没有才保存,接下来,我们再去获取一下当前产品的价格,然后和old_sale_price进行对比就可以了,如果已经有old_sale_price的值,且更新前后的价格不同,我们执行发送邮件的动作,且在邮件发送完成后,清空old_sale_price,这样就可以确保下次修改的时候同样生效

下面是我们的示例代码:

// 添加一个钩子来监视产品价格更新
            add_action('save_post', array( $this, 'wkwooshopify_send_price_change_email' ), 10, 3);
//当产品价格更新后给导出过这个产品的用户发送邮件
		function wkwooshopify_send_price_change_email($post_id, $post, $update) {
			// 确保只在产品编辑页面触发
			if (get_post_type($post_id) !== 'product') {
				return;
			}
			// 确保只在编辑已发布的产品时触发
			if ('publish' !== $post->post_status || !$update) {
				return;
			}
			$product = wc_get_product($post_id);
			// 获取产品的不同价格
			$old_regular_price = get_post_meta($post_id, '_regular_price', true);
			$old_sale_price = get_post_meta($post_id, '_sale_price', true);
			if(!get_post_meta($post_id,'old_regular_price',true)){
				update_post_meta($post_id,'old_regular_price',$old_regular_price);
			}
			if(!get_post_meta($post_id,'old_sale_price',true)){
				update_post_meta($post_id,'old_sale_price',$old_sale_price);
			}
			// 获取旧价格
			$regular_price = get_post_meta($post_id, '_regular_price', true);
			$sale_price = get_post_meta($post_id, '_sale_price', true);
			//如果任何一个价格发生变化,发送邮件
			if ((get_post_meta($post_id,'old_regular_price',true) && $regular_price !== get_post_meta($post_id,'old_regular_price',true)) || (get_post_meta($post_id,'old_sale_price',true) && $sale_price !== get_post_meta($post_id,'old_sale_price',true))) {
				$product_object = new Helper\WkWooShopify_Product_Handler();
				if ( 'variable' === $product->get_type() ) {
					$variations       = $product->get_children();
					$child_id         = end( $variations );
					$store_product_id = $child_id;
				} else {
					$store_product_id = $post_id;
				}
				$check_for_product = $product_object->wkwooshopify_check_product_exists_on_shopify( $store_product_id );
				if ( $check_for_product ) {
					foreach($check_for_product['data'] as $check_product){
						$store_id = $check_product->store_id;
						$obj      = new Helper\WkWooShopify_account();
						$metadata = $obj->wkwooshopify_get_account_meta_details( $store_id );
						$acc_gen_config = $obj->wkwooshopify_get_account_genrl_setting( $store_id);
						if ( isset( $metadata['shopify-acct-auto-sync-opt'] ) && 3 === intval( $metadata['shopify-acct-auto-sync-opt'] ) || isset( $metadata['shopify-acct-auto-sync-opt'] ) && 4 === intval( $metadata['shopify-acct-auto-sync-opt'] ) ) {
							if(!isset( $metadata['shopify_acct_product_price_sync'] ) || (isset( $metadata['shopify_acct_product_price_sync']) && $metadata['shopify_acct_product_price_sync'] == '1')){
                                $to_email = $metadata['email'];
								if(!empty($to_email)){
									$subject = get_bloginfo('name') . '-' . __( 'Product Price Change Notification!', 'wk-woo-shopify' );
									$message = 'The price of product "' . get_the_title($post_id) . '" has changed and has been synced to your product in the Shopify store.';
									$headers = array(
										'Content-Type: text/html; charset=UTF-8',
									);
									wp_mail( $to_email, $subject, $message, $headers );
								}
							}
						}
					}
				}
				update_post_meta($post_id,'old_regular_price','');
				update_post_meta($post_id,'old_sale_price','');
			}
		}

需要注意的是,以上只是示例代码,而且我们使用了类函数,大家可以参考以上思路,根据自己的需求自行写出自己的代码,希望对大家有帮助。

文章标签:

WordPress日记主要承接WordPress主题定制开发PSD转WordPressWordPress仿站以及以WordPress为管理后端的小程序、APP,我们一直秉持“做一个项目,交一个朋友”的理念,希望您是我们下一个朋友。如果您有WordPress主题开发需求,可随时联系QQ:919985494 微信:18539976310

搜索

嘿,有问题找我来帮您!