沃梦达 / 编程问答 / php问题 / 正文

在 WooCommerce 4 中以编程方式从自定义产品类型添加新产品

Add a new product from a custom product type programmatically in WooCommerce 4(在 WooCommerce 4 中以编程方式从自定义产品类型添加新产品)

本文介绍了在 WooCommerce 4 中以编程方式从自定义产品类型添加新产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从前端直接创建产品到 woocommerce 中的分组产品.现在它正在简单产品"中创建产品.

I want to create a product from frontend directly to a grouped product in woocommerce. Right now it is creating a product in "Simple Product".

截图:

当前代码:

$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 开始,不要使用 WordPress 的旧方式,而是使用 WC_Order 方法.

1). Instead of using the WordPress old way, since WooCommerce 3, Use WC_Order methods instead.

  • 产品图片: 通常图片是从附件 id 设置的,而不是从 URL 设置的.save_featured_image() 也不是 WordPress 函数.
  • 产品名称(或标题):必填项.
  • 您提供的代码中有很多错误和错误.
  • Product Image: Normally the image is set from an attachment id, but not from an URL. Also save_featured_image() is not a WordPress function.
  • Product name (or title): Is mandatory.
  • There are a lot of mistakes and errors in your provided code.
  • 您必须在下面的代码中定义产品类型.
  • 自定义产品类型:还需要定义为自定义产品类型源代码类定义的类名称.
  • In the code below you will have to define the product type.
  • Custom product type: It will be necessary to define also the Class name as defined for the custom product type source code Class.

例如,对于分组";产品类型:

For example for a "grouped" product type:

$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 );
}

这段代码应该更好用.

2).或者您仍然可以结合使用 Wordpress 旧方式:

2). Or you can still use the Wordpress Old Way combined:

$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 );
}

这也应该有效.

相关:

  • 使用以下方式以编程方式创建产品Woocommerce 3 中的 CRUD 方法
  • 更改产品WooCommerce 3+ 中现有产品的类型
  • WC_Product 类的源代码(方法) 和 WC_Product_Grouped 类(方法)

这篇关于在 WooCommerce 4 中以编程方式从自定义产品类型添加新产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 WooCommerce 4 中以编程方式从自定义产品类型添加新产品

基础教程推荐