Php 在WooCommerce 4中以编程方式从自定义产品类型添加新产品
我想在woocommerce中创建一个从前端直接到分组产品的产品。 现在它正在“简单产品”中创建一个产品 截图: 当前代码:Php 在WooCommerce 4中以编程方式从自定义产品类型添加新产品,php,wordpress,woocommerce,frontend,product,Php,Wordpress,Woocommerce,Frontend,Product,我想在woocommerce中创建一个从前端直接到分组产品的产品。 现在它正在“简单产品”中创建一个产品 截图: 当前代码: $auction_image = $_POST['auction_image_url']?: ''; $auction_title = $_POST['auction_title'] ?: ''; $auction_category = $_POST['auction_category'] ?: 'auction'; $author_id = $_POST['a
$auction_image = $_POST['auction_image_url']?: '';
$auction_title = $_POST['auction_title'] ?: '';
$auction_category = $_POST['auction_category'] ?: 'auction';
$author_id = $_POST['author_id'] ?: '';
$auction = array(
'post_author' => $author_id,
'post_content' => 'Description',
'post_status' => "publish",
'post_title' => $auction_title,
'post_parent' => '',
'post_type' => "product",
);
//Create post
$auction_id = wp_insert_post($auction, $wp_error);
if ($auction_id) {
save_featured_image($auction_image, $auction_id);
wp_set_object_terms($auction_id, $auction_category, 'product_cat');
update_post_meta($auction_id, '_author_id', $author_id);
wp_send_json_success(array('auction_id' => $auction_id,'message' => 'Auction Added Successfully!!'), 200);
} else {
wp_send_json_error($product_id->get_error_message());
}
1) 。从WooCommerce 3开始,使用WC\u Order
方法代替WordPress的旧方法
重要提示:
- 产品图像:通常,图像是从附件id设置的,而不是从URL设置的。另外,
不是WordPress函数保存图像()
- 产品名称(或标题):为必填项
- 您提供的代码中有很多错误
- 在下面的代码中,您必须定义产品类型
- 自定义产品类型:还需要定义为自定义产品类型源代码类定义的类名
$auction_title = isset($_POST['auction_title']) ? sanitize_text_field($_POST['auction_title']) : '';
$auction_category = isset($_POST['auction_category']) ? esc_attr($_POST['auction_category']) : 'auction';
$product_type = 'grouped'; // <== Here define your product type slug
$class_name = WC_Product_Factory::get_classname_from_product_type($product_type); // Get the product Class name
// If the product class exist for the defined product type
if( ! empty($class_name) && class_exists( $class_name ) ) {
$product = new $class_name(); // Get an empty instance of a grouped product Object
}
// For a custom product class
else {
$class_name = 'WC_Product_custom'; // <== Here define the Class name of your custom product type
if( class_exists( $class_name ) ) {
$product = new $class_name(); // Get an empty instance of a custom class product Object
} else {
wp_send_json_error( array( 'message' =>__('Wrong product class') ), 409 );
return; // or exit;
}
}
$product->set_name($auction_title);
$product->set_description('Description');
$product->set_short_description('Short_description');
$product->set_status('publish');
// $product-> set_image_id( $image_id ); // ???
$category_term = get_term_by( 'slug', $auction_category, 'product_cat' ); // Get the term from its slug
if( is_a($category_term, 'WP_Term') ) {
$product->set_category_ids( array($category_term->term_id) );
}
$product_id = $product->save(); // Save product to database
if ( $product_id ) {
// Set the post author
if( isset($_POST['author_id']) ) {
wp_update_post('ID' => $product_id, 'post_author' => esc_attr($_POST['author_id']) );
}
wp_send_json_success(array('auction_id' => $product_id,'message' => __('Auction Added Successfully!!') ), 200);
} else {
wp_send_json_error( array( 'auction_id' => $product_id, 'message' =>__('Auction failed :(') ), 400 );
}
$auction\u title=isset($\u POST['auction\u title'])?清理文本字段($\u POST['auction\u title']):'';
$auction\u category=isset($\u POST['auction\u category'])?esc_attr($_POST[‘拍卖’类别]):‘拍卖’;
$product_type=‘分组’;//设置名称(拍卖名称);
$product->set_说明(“说明”);
$product->set_short_description('short_description');
$product->set_状态(“发布”);
//$product->set_image_id($image_id);/???
$category_term=get_term_by('slug',$auction_category,'product_cat');//从它的鼻涕虫那里得到这个词
如果(是一个($category_term,'WP_term')){
$product->set_category_id(数组($category_term->term_id));
}
$product_id=$product->save();//将产品保存到数据库
如果($product_id){
//设置帖子作者
如果(isset($\u POST['author\u id'])){
wp_update_post($ID'=>$product_ID,$post_author'=>esc_attr($u post['author_ID']));
}
wp_send_json_success(数组('auction_id'=>$product_id,'message'=>\uu('auction Added Successfully!!')),200);
}否则{
wp_send_json_错误(数组('auction_id'=>$product_id,'message'=>json('auction failed:(')),400);
}
这段代码应该能更好地工作
2) 。或者您仍然可以使用Wordpress旧方式组合:
$auction_data = array(
'post_author' => isset($_POST['author_id']) ? esc_attr($_POST['author_id']) : '',
'post_content' => 'Description',
'post_status' => "publish",
'post_title' => isset($_POST['auction_title']) ? sanitize_text_field($_POST['auction_title']) : __('Empty title'),
'post_parent' => '',
'post_type' => "product",
);
$auction_id = wp_insert_post($auction_data, $wp_error);
if ( $auction_id ) {
$auction_category = isset($_POST['auction_category']) ? esc_attr($_POST['auction_category']) : 'auction';
wp_set_object_terms( $auction_id, $auction_category, 'product_cat' );
if( isset($_POST['auction_image_url']) && function_exists('save_featured_image') ) {
save_featured_image($auction_image, ecs_attr($_POST['auction_image_url']));
}
update_post_meta( $auction_id, '_author_id', $author_id );
$product_type = 'grouped'; // <== Here define your product type slug
$class_name = WC_Product_Factory::get_product_classname( $product_id, $new_product_type );
// If the product class exist for the defined product type
if( ! empty($class_name) && class_exists( $class_name ) ) {
$product = new $class_name($auction_id); // Get an empty instance of a grouped product Object
}
// For a custom product class (you may have to define the custom class name)
else {
$class_name = 'WC_Product_custom'; // <== Here define the Class name of your custom product type
if( class_exists( $class_name ) ) {
$product = new $class_name($auction_id); // Get an empty instance of a custom class product Object
} else {
wp_send_json_error( array( 'message' =>__('Wrong product class') ), 409 );
return; // or exit;
}
}
$auction_id = $product->save(); // Save to database
wp_send_json_success( array('auction_id' => $auction_id, 'message' => __('Auction Added Successfully!!') ), 200 );
} else {
wp_send_json_error( array('message' => __('Auction Failed') 400 );
}
$auction\u data=array(
'post_author'=>isset($_post['author_id'])?esc_attr($_post['author_id']):“”,
“发布内容”=>“说明”,
“发布状态”=>“发布”,
'post_title'=>isset($_post['auction_title'])?清理文本字段($_post['auction_title']):$(空标题),
“post_parent'=>”,
“发布类型”=>“产品”,
);
$auction\u id=wp\u insert\u post($auction\u data,$wp\u error);
如果($拍卖id){
$auction_category=isset($_POST['auction_category'])?esc_attr($_POST['auction_category']):'auction';
wp_set_object_terms($auction_id,$auction_category,'product_cat');
如果(isset($\u POST['auction\u image\u url'])和函数存在('save\u characterized\u image')){
保存特色图片($拍卖图片,ecs属性($发布['拍卖图片\ url');
}
更新文章元($author\u id、$author\u id);
$product_type='GROUBLED';//保存();//保存到数据库
wp_send_json_success(数组('auction_id'=>$auction_id,'message'=>auction('auction Added Successfully!!')),200);
}否则{
wp_发送_json_错误(数组('message'=>uu('Auction Failed')400);
}
这也应该起作用
相关的:
- 和的源代码