无法从安卓应用程序成功将产品添加到 woocommerce。



我想从android应用程序向wooccommerce添加新产品。所以我写了以下代码:

<?php
require_once("wp-load.php");
require_once("wp-load.php");
$image = $_POST['image'];
$name = $_POST['name'];
$info = $_POST['info'];
$price = $_POST['price'];
$saleprice = $_POST['saleprice'];
$id = $_POST['id'];
$imageName = $_POST['imageName'];
$catid = $_POST['catID'];



//add new product 
$args = array(     
'post_author' =>$id, 
'post_content' => $info,
'post_status' => "Publish", // (Draft | Pending | Publish)
'post_title' =>$name,
'post_parent' => '',
'post_type' => "product"
);
// Create a simple WooCommerce product
$postId = wp_insert_post( $args );
//add category to new product
$cat=array($catid);
wp_set_object_terms($postId,$cat, 'product_cat');
// Setting the product type
wp_set_object_terms( $postId, 'simple', 'product_type' );
// Setting the product price
update_post_meta( $postId, '_price',$price );
if($saleprice!=null)
update_post_meta( $postId, '_sale_price', $saleprice );

// current path directory
$dirPath = getcwd();
// full path of image
$IMGFilePath = $dirPath.'/'.$imageName;

$logtxt .= $IMGFilePath. "   IMGFilePathln";
// error message for file not found in directory
$message = $imageName.' is not available or found in directory.';


//prepare upload image to WordPress Media Library
$upload = wp_upload_bits( $imageName, null, base64_decode($image) );

// check and return file type
$imageFile = $upload['file'];
$wpFileType = wp_check_filetype($imageFile, null);
// Attachment attributes for file
$attachment = array(
'post_mime_type' => $wpFileType['type'],  // file type
'post_title' => sanitize_file_name($imageFile),  // sanitize and use image name as file name
'post_content' => '',  // could use the image description here as the content
'post_status' => 'inherit'
);
// insert and return attachment id
$attachmentId = wp_insert_attachment( $attachment, $imageFile, $postId );

// insert and return attachment metadata
$attachmentData = wp_generate_attachment_metadata( $attachmentId, $imageFile);
// update and return attachment metadata
wp_update_attachment_metadata( $attachmentId, $attachmentData );

// finally, associate attachment id to post id
$success = set_post_thumbnail( $postId, $attachmentId );

echo $postId;
?>

我在android中编码了图像,并将其发送到php文件中,对其进行解码并上传到wordpress中。之后我创建了新的文章。然后我将图像附加到新的帖子中。问题是我的产品被添加了两次,并且类别和图像没有正确添加。我该怎么修?

刚刚将其更改为下面的代码。因此它成功地工作了。

<?php
require_once("wp-load.php");
require_once("wp-load.php");
$image = $_POST['image'];
$name = $_POST['name'];
$info = $_POST['info'];
$price = $_POST['price'];
$saleprice = $_POST['saleprice'];
$id = $_POST['id'];
$imageName = $_POST['imageName'];
$catid = $_POST['catID'];



//add new product 
$args = array(     
'post_author' =>$id, 
'post_content' => $info,
'post_status' => "Publish", // (Draft | Pending | Publish)
'post_title' =>$name,
'post_parent' => '',
'post_type' => "product"
);
// Create a simple WooCommerce product
$postId = wp_insert_post( $args );
//add category to new product
$cat=array($catid);
wp_set_object_terms($postId,$cat, 'product_cat');
// Setting the product type
wp_set_object_terms( $postId, 'simple', 'product_type' );
// Setting the product price
update_post_meta( $postId, '_price',$price );
if($saleprice!=null)
update_post_meta( $postId, '_sale_price', $saleprice );

$upload = wp_upload_bits( $imageName, null, base64_decode($image) );    
// check and return file type
$image_url = $upload['url'];


$upload_dir = wp_upload_dir();
$image_data = file_get_contents($image_url);
$filename =$imageName;
if(wp_mkdir_p($upload_dir['path']))     $file = $upload_dir['path'] . '/' . $filename;
else                                    $file = $upload_dir['basedir'] . '/' . $filename;
file_put_contents($file, $image_data);
$wp_filetype = wp_check_filetype($filename, null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name($filename),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $file, $postId );
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );
$res1= wp_update_attachment_metadata( $attach_id, $attach_data );
$res2= set_post_thumbnail( $postId, $attach_id );
var_dump($res2);
?>

最新更新