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

黎兹旺·谢赫(Rizwan Shaikh)

我想在woocommerce中直接从前端创建产品到分组产品。现在,它正在“简单产品”中创建产品。

屏幕截图:

在此处输入图片说明

当前代码:

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

1)。从WooCommerce 3开始,WC_Order不再使用WordPress的旧方法,而应使用方法。

重要笔记:

  • 产品图片:通常,图片是通过附件ID设置的,而不是通过URL设置的。另外save_featured_image()是不是一个WordPress的功能。
  • 产品名称(或标题):是必填项。
  • 您提供的代码中有很多错误和错误。

产品类型和定制产品类型

  • 在下面的代码中,您将必须定义产品类型。
  • 定制产品类型:还必须定义为定制产品类型源代码Class定义的Class名称。

例如,对于“分组”产品类型:

$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 Old Way:

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

这也应该起作用。


有关:

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在Woocommerce 3中添加新产品类型

如何在 Woocommerce 的自定义产品类型中添加变体选项卡?

在WooCommerce中以编程方式添加自定义设置选项卡以管理产品数据

如何以编程方式在WooCommerce中添加新的自定义产品属性?

WooCommerce自定义产品类型-多个添加到购物车部分的问题

在WooCommerce中将自定义标签添加到特定产品类型的单个产品页面

如何在WooCommerce中为自定义产品类型启用价格和库存

如何检查WooCommerce中的自定义产品类型选项?

在WooCommerce中更改自定义产品类型的订单运送地址

在Woocommerce中为特定产品类别添加自定义按钮

根据Woocommerce中的产品类别添加自定义结帐字段

在Woocommerce 3中以编程方式更新产品库存

在Woocommerce中以编程方式创建新产品属性

在Woocommerce中以编程方式设置或更新产品运输类别

在自定义家庭和产品类别档案中显示WooCommerce产品属性

产品类别的WooCommerce产品价格中的自定义小数

根据产品类型,WooCommerce的“添加到购物车”按钮旁边的“自定义”按钮

使用Woocommerce中的方法以编程方式设置自定义产品字段

以编程方式更新WooCommerce产品中设置的自定义属性值

根据 WooCommerce 电子邮件通知中的产品类别添加自定义文本“每个项目”

在WooCommerce中为特定产品类别自定义“添加到购物车”按钮

在WooCommerce自定义产品类型上显示库存状态

使用ACF编号字段作为WooCommerce自定义产品类型的价格

在WooCommerce中根据产品类型隐藏付款方式

在 WooCommerce 中添加、更新或删除产品自定义字段

woocommerce中的自定义产品

仅在 WooCommerce 中自定义类型的管理产品上添加字段

从Woocommerce档案库中的自定义显示按钮中排除产品类别

获取 WooCommerce 中顶级产品类别的自定义数组