php - Woocommerce in stock Notifier Api
i am using in stock notifier plugin https://wordpress.org/plugins/back-in-stock-notifier-for-woocommerce/ in this plugin rest api or plugin of rest api not given i am making custom rest api plugin and insert data in a table but email not coming during subscription of email id and during in stock of product. my custom code for instock api
<?php
/**
* Plugin Name: Very First Plugin
* Plugin URI: https://www.yourwebsiteurl.com/
* Description: This is the very first plugin I ever created.
* Version: 1.0
* Author: Your Name Here
* Author URI: http://yourwebsiteurl.com/
**/
/**
* Grab latest post title by an author!
*
* @param array $data Options for the function.
* @return string|null Post title for the latest,??? * or null if none.
*/
/*
function my_awesome_func( $data ) {
$posts = get_posts( array(
'author' => $data['id'],
) );
if ( empty( $posts ) ) {
return null;
}
return $posts[0]->post_title;
}
add_action( 'rest_api_init', function () {
register_rest_route( 'myplugin/v1', '/authorsss/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'my_awesome_func',
) );
} );
*/
function my_awesome_func( $data ) {
global $wpdb;
if($data['cwginstock_user_id']==''){
$students_arr = array(
"responseCode" => 400,
"responseMessage" => "Please enter user id.",);
echo json_encode($students_arr);
}elseif($data['cwginstock_subscriber_email'] == ''){
$students_arr = array(
"responseCode" =>400,
"responseMessage" => "Please enter email id.",);
echo json_encode($students_arr);
}elseif($data['cwginstock_pid'] == ''){
$students_arr = array(
"responseCode" =>400,
"responseMessage" => "Please enter product id.",);
echo json_encode($students_arr);
}else{
$tablename2 = $wpdb->prefix . "posts";
//himanshu-swamiitechs-co-in__trashed
$ok = str_replace(".","",$data['cwginstock_subscriber_email']);
$post_name = str_replace("@","-",$ok);
$res = $wpdb->insert(
$tablename2,
array(
'post_author' => $data['cwginstock_user_id'],
'post_content' => "",
'post_title' => $data['cwginstock_subscriber_email'],
'post_excerpt' => "",
'post_name'=>$post_name,
'post_status' =>'cwg_subscribed',
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_password' => "",
'to_ping' => "",
'pinged' => "",
'post_content_filtered' =>'',
'post_parent' =>'',
//'guid' => 'http://localhost/ecommerces4/cwginstocknotifier/himanshu-swamiitechs-co-in/',
'guid' => 'http://localhost/ecommerces4/cwginstocknotifier/'.$post_name.'/',
"menu_order"=> 0,
"post_type"=> "cwginstocknotifier",
"post_mime_type" =>"",
"comment_count"=> 0,
"post_date"=>Date('Y-m-d H:i:s'),
"post_date_gmt"=>Date('Y-m-d H:i:s'),
"post_modified_gmt" =>Date('Y-m-d H:i:s'),
"post_modified"=>Date('Y-m-d H:i:s')
)
);
$lastid = $wpdb->insert_id;
$data1 = array(
'cwginstock_variation_id',
'cwginstock_subscriber_email',
'cwginstock_user_id',
'cwginstock_language',
'cwginstock_pid'
);
$data3 = array(0, $data['cwginstock_subscriber_email'],
$data['cwginstock_user_id'], "en_US",$data['cwginstock_pid']
);
$tablename = $wpdb->prefix . "postmeta";
foreach ($data1 as $key => $value) {
$res = $wpdb->insert(
$tablename,
array(
'post_id' => $lastid,
'meta_key' => $value,
'meta_value' => $data3[$key]
)
);
}
if($res){
echo 'inserted';
}else{
echo 'not inserted';
}
return $wpdb;
}
}
add_action( 'rest_api_init', function () {
$namespace = 'myplugin/v1';
$endpoint = '/authorsss/';
register_rest_route( $namespace, $endpoint, array(
'methods' => 'GET',
'callback' => 'my_awesome_func'
) );
} );
i want to proper insert data in database, email alert is not coming and in admin side in plugin product name not showing even i am sending product id
Answer
Solution:
I wanted to do the same thing. I used your code as a base to start but changed some small things and it works now.
//himanshu-swamiitechs-co-in__trashed
$ok = str_replace(".","-",$data['cwginstock_subscriber_email']);
$post_name = str_replace("@","",$ok);
the str_replace needed to be different. replace . by - and remove @
$data1 = array(
'cwginstock_product_id' ,
'cwginstock_variation_id',
'cwginstock_subscriber_email',
'cwginstock_user_id',
'cwginstock_language',
'cwginstock_pid' ,
);
Secondly this is the right order and variables
Underneath you can see the whole code I used
function back_in_stock_email ( $data) {
// Get request params
global $wpdb;
if ($data['cwginstock_user_id']=='') {
$students_arr = array(
"responseCode" => 400,
"responseMessage" => "Please enter user id.",);
echo json_encode($students_arr);
} elseif ($data['cwginstock_subscriber_email'] == '') {
$students_arr = array(
"responseCode" =>400,
"responseMessage" => "Please enter email id.",);
echo json_encode($students_arr);
} elseif($data['cwginstock_pid'] == '') {
$students_arr = array(
"responseCode" =>400,
"responseMessage" => "Please enter product id.",);
echo json_encode($students_arr);
} else {
$tablename2 = $wpdb->prefix . "posts";
//himanshu-swamiitechs-co-in__trashed
$ok = str_replace(".","-",$data['cwginstock_subscriber_email']);
$post_name = str_replace("@","",$ok);
$res = $wpdb->insert(
$tablename2,
array(
'post_author' => "0",
'post_content' => "",
'post_title' => $data['cwginstock_subscriber_email'],
'post_excerpt' => "",
'post_name'=>$post_name,
'post_status' =>'cwg_subscribed',
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_password' => "",
'to_ping' => "",
'pinged' => "",
'post_content_filtered' =>'',
'post_parent' =>'',
'guid' => '<YOUR BASE URL>/cwgstocknotifier/'.$post_name.'/',
"menu_order"=> "0",
"post_type"=> "cwginstocknotifier",
"post_mime_type" =>"",
"comment_count"=> "0",
"post_date"=>Date('Y-m-d H:i:s'),
"post_date_gmt"=>Date('Y-m-d H:i:s'),
"post_modified_gmt" =>Date('Y-m-d H:i:s'),
"post_modified"=>Date('Y-m-d H:i:s')
)
);
$lastid = $wpdb->insert_id;
echo $lastid;
$data1 = array(
'cwginstock_product_id' ,
'cwginstock_variation_id',
'cwginstock_subscriber_email',
'cwginstock_user_id',
'cwginstock_language',
'cwginstock_pid' ,
);
$data3 = array($data['cwginstock_pid'], $data['cwginstock_variation_id'],
$data['cwginstock_subscriber_email'], "0","en_US",$data['cwginstock_pid']);
$tablename = $wpdb->prefix . "postmeta";
foreach ($data1 as $key => $value) {
$res = $wpdb->insert(
$tablename,
array(
'post_id' => $lastid,
'meta_key' => $value,
'meta_value' => $data3[$key]
)
);
}
if ($res) {
echo 'inserted';
} else {
echo 'not inserted';
}
return $wpdb;
}
}
Source