103 lines
4.7 KiB
PHP
103 lines
4.7 KiB
PHP
<?php
|
|
/**
|
|
* Start Checking subscribed customer and alert about stock
|
|
*
|
|
*/
|
|
class WOO_Product_Stock_Alert_Action {
|
|
|
|
public function __construct() {
|
|
// Call to cron action
|
|
add_action('dc_start_stock_alert', array($this, 'stock_alert_action'));
|
|
}
|
|
|
|
function stock_alert_action() {
|
|
global $WC;
|
|
$all_products = array();
|
|
$all_products = get_posts(
|
|
array(
|
|
'post_type' => 'product',
|
|
'post_status' => 'publish',
|
|
'numberposts' => -1
|
|
)
|
|
);
|
|
$all_product_ids = array();
|
|
if (!empty($all_products) && is_array($all_products)) {
|
|
foreach ($all_products as $products_each) {
|
|
$child_ids = $product_obj = array();
|
|
$product_obj = wc_get_product($products_each->ID);
|
|
if ($product_obj && $product_obj->is_type('variable')) {
|
|
if ($product_obj->has_child()) {
|
|
$child_ids = $product_obj->get_children();
|
|
if (isset($child_ids) && !empty($child_ids)) {
|
|
foreach ($child_ids as $child_id) {
|
|
$all_product_ids[] = $child_id;
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
$all_product_ids[] = $products_each->ID;
|
|
}
|
|
}
|
|
}
|
|
|
|
$get_subscribed_user = array();
|
|
if (!empty($all_product_ids) && is_array($all_product_ids)) {
|
|
foreach ($all_product_ids as $product_id) {
|
|
$subscribers_email = get_product_subscribers_email($product_id);
|
|
if ($subscribers_email && !empty($subscribers_email)) {
|
|
$get_subscribed_user[$product_id] = $subscribers_email;
|
|
}
|
|
}
|
|
}
|
|
if (!empty($get_subscribed_user) && is_array($get_subscribed_user)) {
|
|
foreach ($get_subscribed_user as $p_id => $subscriber) {
|
|
$product = wc_get_product($p_id);
|
|
$product_availability_stock = $product->get_stock_quantity();
|
|
$manage_stock = $product->get_manage_stock();
|
|
$managing_stock = $product->managing_stock();
|
|
$stock_status = $product->get_stock_status();
|
|
if ( $managing_stock ) {
|
|
if ($product->backorders_allowed() && get_mvx_product_alert_plugin_settings('is_enable_backorders')) {
|
|
$email = WC()->mailer()->emails['WC_Email_Stock_Alert'];
|
|
foreach ($subscriber as $post_id => $to) {
|
|
$email->trigger($to, $p_id);
|
|
|
|
update_subscriber($post_id, 'woo_mailsent');
|
|
delete_post_meta($p_id, 'no_of_subscribers');
|
|
}
|
|
|
|
} else {
|
|
if ($product_availability_stock > (int) get_option('woocommerce_notify_no_stock_amount')) {
|
|
$email = WC()->mailer()->emails['WC_Email_Stock_Alert'];
|
|
foreach ($subscriber as $post_id => $to) {
|
|
$email->trigger($to, $p_id);
|
|
|
|
update_subscriber($post_id, 'woo_mailsent');
|
|
delete_post_meta($p_id, 'no_of_subscribers');
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
if ($stock_status == 'onbackorder' && get_mvx_product_alert_plugin_settings('is_enable_backorders')) {
|
|
if ($stock_status != 'outofstock' || $product_availability_stock > (int) get_option('woocommerce_notify_no_stock_amount')) {
|
|
$email = WC()->mailer()->emails['WC_Email_Stock_Alert'];
|
|
foreach ($subscriber as $post_id => $to) {
|
|
$email->trigger($to, $p_id);
|
|
|
|
update_subscriber($post_id, 'woo_mailsent');
|
|
delete_post_meta($p_id, 'no_of_subscribers');
|
|
}
|
|
}
|
|
} elseif ($stock_status == 'instock' ) {
|
|
$email = WC()->mailer()->emails['WC_Email_Stock_Alert'];
|
|
foreach ($subscriber as $post_id => $to) {
|
|
$email->trigger($to, $p_id);
|
|
update_subscriber($post_id, 'woo_mailsent');
|
|
delete_post_meta($p_id, 'no_of_subscribers');
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |