Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Woocommerce库存通知程序Api_Php_Wordpress_Api_Woocommerce - Fatal编程技术网

Php Woocommerce库存通知程序Api

Php Woocommerce库存通知程序Api,php,wordpress,api,woocommerce,Php,Wordpress,Api,Woocommerce,我正在使用库存通知插件 此插件中未给出rest api或rest api的插件 我正在制作自定义RESTAPI插件,并在表中插入数据,但在订阅电子邮件id期间电子邮件不会出现 在产品库存期间。 我的instock api自定义代码 <?php /** * Plugin Name: Very First Plugin * Plugin URI: https://www.yourwebsiteurl.com/ * Description: This is the very first plu

我正在使用库存通知插件 此插件中未给出rest api或rest api的插件 我正在制作自定义RESTAPI插件,并在表中插入数据,但在订阅电子邮件id期间电子邮件不会出现 在产品库存期间。 我的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'
      ) );
    } );

我也想做同样的事情。我用你的代码作为开始的基础,但做了一些小改动,现在可以使用了

//himanshu-swamiitechs-co-in__trashed
    $ok =  str_replace(".","-",$data['cwginstock_subscriber_email']);
    $post_name =  str_replace("@","",$ok);
str_替换需要不同。代替通过-并删除@

$data1 = array(
        'cwginstock_product_id' ,
        'cwginstock_variation_id',
         'cwginstock_subscriber_email',
        'cwginstock_user_id',
        'cwginstock_language',
           'cwginstock_pid' ,
    );
其次,这是正确的顺序和变量

下面你可以看到我使用的全部代码

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;
    }
}
在库存邮件中返回功能($data){
//获取请求参数
全球$wpdb;
如果($data['cwginstock\u user\u id']=''){
$students\u arr=数组(
“响应代码”=>400,
“responseMessage”=>“请输入用户id.”,);
echo json_encode($students_arr);
}elseif($data['cwginstock_订户_email']=''){
$students\u arr=数组(
“响应代码”=>400,
“responseMessage”=>“请输入电子邮件id.”,);
echo json_encode($students_arr);
}elseif($data['cwginstock_pid']=''){
$students\u arr=数组(
“响应代码”=>400,
“responseMessage”=>“请输入产品id.”,);
echo json_encode($students_arr);
}否则{
$tablename2=$wpdb->prefix.“posts”;
//himanshu-swamiitechs-co-in___垃圾
$ok=str_replace(“.”,“-”,$data['cwginstock_订户_email']);
$post_name=str_replace(“@”,“确定”);
$res=$wpdb->insert(
$tablename2,
排列(
'post_author'=>“0”,
“发布内容”=>“”,
“post_title”=>$data['cwginstock_订户_email'],
“post_摘录”=>“”,
“post\u name”=>$post\u name,
“发布状态”=>“已订阅cwg”,
“评论状态”=>“已关闭”,
“ping_状态”=>“已关闭”,
“post_密码”=>“”,
“to_ping”=>“”,
“ping”=>“”,
'已过滤的内容后'=>'',
“post_parent'=>”,
'guid'=>'/cwgstocknotifier/'.$post_name.'/',
“菜单顺序”=>“0”,
“post_type”=>“CWG链接器”,
“post\u mime\u type”=>“”,
“注释计数”=>“0”,
“发布日期”=>日期('Y-m-d H:i:s'),
“发布日期”=>日期('Y-m-d H:i:s'),
“修改后的gmt”=>日期('Y-m-d H:i:s'),
“修改后”=>日期('Y-m-d H:i:s')
)
);
$lastid=$wpdb->insert_id;
echo$lastid;
$data1=数组(
“cwginstock_product_id”,
“cwginstock_变体_id”,
“cwginstock_订户_电子邮件”,
“cwginstock_用户_id”,
“cwginstock_语言”,
“cwginstock_pid”,
);
$data3=数组($data['cwginstock\u pid'],$data['cwginstock\u variation\u id'],
$data['cwginstock_订户_email']、“0”、“en_US”、$data['cwginstock_pid'];
$tablename=$wpdb->prefix.“postmeta”;
foreach($data1作为$key=>$value){
$res=$wpdb->insert(
$tablename,
排列(
“post_id”=>$lastid,
“meta_key”=>$value,
“meta_值”=>$data3[$key]
)
);
}
如果($res){
回声“插入”;
}否则{
回声“未插入”;
}
返回$wpdb;
}
}

您能否突出显示您所做的更改,并帮助指导OP解决方案。仅代码答案无助于OP和其他人了解如何解决问题。我现在清楚了吗?