当前位置: 首页 > 知识库问答 >
问题:

使用Woocommerce 3中的CRUD方法以编程方式创建产品

公西翼
2023-03-14

我想用PHP代码以编程方式创建一个新的Woocommerce产品,我已经用Woocommerce 3 CRUD方法进行了尝试:

//Woocommerce CRUD
$objProduct = new WC_Product_Simple();

$objProduct->set_name('My Product 1'); //Set product name.
$objProduct->set_status('publish'); //Set product status.
//$objProduct->set_featured(FALSE); //Set if the product is featured.                          | bool
$objProduct->set_catalog_visibility('visible'); //Set catalog visibility.                   | string $visibility Options: 'hidden', 'visible', 'search' and 'catalog'.
$objProduct->set_description('My custom long description'); //Set product description.
//$objProduct->set_short_description('My short description'); //Set product short description.
//$objProduct->set_sku('U-123'); //Set SKU

$objProduct->set_price(5.00); //Set the product's active price.
//$objProduct->set_regular_price(5.00); //Set the product's regular price.
//$objProduct->set_sale_price(); //Set the product's sale price.
//$objProduct->set_date_on_sale_from(); //Set date on sale from.                              | string|integer|null $date UTC timestamp, or ISO 8601 DateTime. If the DateTime string has no timezone or offset, WordPress site timezone will be assumed. Null if their is no date.
//$objProduct->set_date_on_sale_to();//Set date on sale to.                                   | string|integer|null $date UTC timestamp, or ISO 8601 DateTime. If the DateTime string has no timezone or offset, WordPress site timezone will be assumed. Null if their is no date.

//$objProduct->set_manage_stock(TRUE); //Set if product manage stock.                         | bool
//$objProduct->set_stock_quantity(10); //Set number of items available for sale.
//$objProduct->set_stock_status('instock'); //Set stock status.                               | string $status 'instock', 'outofstock' and 'onbackorder'
//$objProduct->set_backorders('no'); //Set backorders.                                        | string $backorders Options: 'yes', 'no' or 'notify'.
//$objProduct->set_sold_individually(FALSE); //Set if should be sold individually.            | bool

//$objProduct->set_weight(); //Set the product's weight.
//$objProduct->set_length(); //Set the product length.
//$objProduct->set_width(); //Set the product width.
//$objProduct->set_height(); //Set the product height.

//$objProduct->set_upsell_ids($upsell_ids); //Set upsell IDs.                               | array $upsell_ids IDs from the up-sell products.
//$objProduct->set_cross_sell_ids($cross_sell_ids); //Set crosssell IDs.                    | array $cross_sell_ids IDs from the cross-sell products.

$objProduct->set_reviews_allowed(TRUE); //Set if reviews is allowed.                        | bool

//$objProduct->set_purchase_note($purchase_note); //Set purchase note.                      | string $purchase_note Purchase note.


$attribute = new WC_Product_Attribute();
$attribute->set_id(wc_attribute_taxonomy_id_by_name('pa_color')); //if passing the attribute name to get the ID
$attribute->set_name('pa_color'); //attribute name
$attribute->set_options('red'); // attribute value
$attribute->set_position(1); //attribute display order
$attribute->set_visible(1); //attribute visiblity
$attribute->set_variation(0);//to use this attribute as varint or not

$raw_attributes[] = $attribute; //<--- storing the attribute in an array

$attribute = new WC_Product_Attribute();
$attribute->set_id(25);
$attribute->set_name('pa_size');
$attribute->set_options('XL');
$attribute->set_position(2);
$attribute->set_visible(1);
$attribute->set_variation(0);

$raw_attributes[] = $attribute; //<--- storing the attribute in an array

$objProduct->set_attributes($raw_attributes); //Set product attributes.                   | array $raw_attributes Array of WC_Product_Attribute objects.

//$objProduct->set_category_ids($term_ids); //Set the product categories.                   | array $term_ids List of terms IDs.
//$objProduct->set_tag_ids($term_ids); //Set the product tags.                              | array $term_ids List of terms IDs.

//$objProduct->set_image_id(); //Set main image ID.                                         | int|string $image_id Product image id.
//$objProduct->set_gallery_image_ids(); //Set gallery attachment ids.                       | array $image_ids List of image ids.

$new_product_id = $objProduct->save(); //Saving the data to create new product, it will return product ID.

return;
}

但我得到了这个错误:

22-Oct-2018 20:25:19UTC]PHP致命错误:未捕获错误:类'WC_Product_Simple'未找到 /home/u1771p590/domains/famyfuerza.com/public_html/store/wp-content/plugins/ff-dropship-data-scraper/ff-dropship-data-scraper.php: 165堆栈跟踪:
-#0 /home/u1771p590/domains/famyfuerza.com/public_html/store/wp-content/plugins/ff-dropship-data-scraper/ff-dropship-data-scraper.php(233):curl_download('<-
-#1 /home/u1771p590/domains/famyfuerza.com/public_html/store/wp-settings.php(305):include_once('/home/u1771p590...')
-#2 /home/u1771p590/domains/famyfuerza.com/public_html/store/wp-config.php(112):require_once('/home/u1771p590...')
-#3 /home/u1771p590/domains/famyfuerza.com/public_html/store/wp-load.php(37):require_once('/home/u1771p590...')
-#4 /home/u1771p590/domains/famyfuerza.com/public_html/store/wp-admin/admin.php(31):require_once('/home/u1771p590...')
-#5 /home/u1771p590/domains/famyfuerza.com/public_html/store/wp-admin/index.php(10):require_once('/home/u1771p590...')
-#6{main}
抛入 /home/u1771p590/domains/famyfuerza.com/public_html/store/wp-content/plugins/ff-dropship-data-scraper/ff-dropship-data-scraper.php第165行

我试图包含简单的产品类,但这产生了另一个错误,即另一个类丢失了。

有人知道怎么解决这个问题吗?或者有其他方法通过代码创建woocommerce产品?

共有2个答案

壤驷俊逸
2023-03-14

另一种方式:https://wordpress.stackexchange.com/a/137578/74284,使用wp_insert_post函数。

https://lukasznowicki.info/insert-new-woocommerce-product-programmatically/

$item = array(
    'Name' => 'Product A',
    'Description' => 'This is a product A',
    'SKU' => '10020030A',
);
$user_id = get_current_user(); // this has NO SENSE AT ALL, because wp_insert_post uses current user as default value
// $user_id = $some_user_id_we_need_to_use; // So, user is selected..
$post_id = wp_insert_post( array(
    'post_author' => $user_id,
    'post_title' => $item['Name'],
    'post_content' => $item['Description'],
    'post_status' => 'publish',
    'post_type' => "product",
) );
wp_set_object_terms( $post_id, 'simple', 'product_type' );
update_post_meta( $post_id, '_visibility', 'visible' );
update_post_meta( $post_id, '_stock_status', 'instock');
update_post_meta( $post_id, 'total_sales', '0' );
update_post_meta( $post_id, '_downloadable', 'no' );
update_post_meta( $post_id, '_virtual', 'yes' );
update_post_meta( $post_id, '_regular_price', '' );
update_post_meta( $post_id, '_sale_price', '' );
update_post_meta( $post_id, '_purchase_note', '' );
update_post_meta( $post_id, '_featured', 'no' );
update_post_meta( $post_id, '_weight', '' );
update_post_meta( $post_id, '_length', '' );
update_post_meta( $post_id, '_width', '' );
update_post_meta( $post_id, '_height', '' );
update_post_meta( $post_id, '_sku', $item['SKU'] );
update_post_meta( $post_id, '_product_attributes', array() );
update_post_meta( $post_id, '_sale_price_dates_from', '' );
update_post_meta( $post_id, '_sale_price_dates_to', '' );
update_post_meta( $post_id, '_price', '' );
update_post_meta( $post_id, '_sold_individually', '' );
update_post_meta( $post_id, '_manage_stock', 'no' );
update_post_meta( $post_id, '_backorders', 'no' );
update_post_meta( $post_id, '_stock', '' );
齐浩淼
2023-03-14

您没有从自定义Dropship数据刮板插件访问WC\u Product\u simple实例对象。

罪犯主要有两种情况:

  1. 您尚未安装Woocommerce
  2. Dropship数据刮板插件已经过时,需要进行更改,以处理电子商务

尝试在插件中包含全局WooCommerce对象并启用WooCommerce支持。

使用WooCommerce 3中介绍的CRUD方法以编程方式创建产品是使其工作的正确方法。

如果您需要处理产品属性创建或产品变化创建,请参阅:

  • 在Woocommerce中以编程方式创建新产品属性
  • 以编程方式创建具有新属性值的WooCommerce产品变体
  • 在WooCommerce中以编程方式创建一个可变产品和两个新属性

所有产品属性及其术语值都需要在之前创建。产品类别和产品标签也是如此。

1)代码功能

// Custom function for product creation (For Woocommerce 3+ only)
function create_product( $args ){

    if( ! function_exists('wc_get_product_object_type') && ! function_exists('wc_prepare_product_attributes') )
        return false;

    // Get an empty instance of the product object (defining it's type)
    $product = wc_get_product_object_type( $args['type'] );
    if( ! $product )
        return false;

    // Product name (Title) and slug
    $product->set_name( $args['name'] ); // Name (title).
    if( isset( $args['slug'] ) )
        $product->set_name( $args['slug'] );

    // Description and short description:
    $product->set_description( $args['description'] );
    $product->set_short_description( $args['short_description'] );

    // Status ('publish', 'pending', 'draft' or 'trash')
    $product->set_status( isset($args['status']) ? $args['status'] : 'publish' );

    // Visibility ('hidden', 'visible', 'search' or 'catalog')
    $product->set_catalog_visibility( isset($args['visibility']) ? $args['visibility'] : 'visible' );

    // Featured (boolean)
    $product->set_featured(  isset($args['featured']) ? $args['featured'] : false );

    // Virtual (boolean)
    $product->set_virtual( isset($args['virtual']) ? $args['virtual'] : false );

    // Prices
    $product->set_regular_price( $args['regular_price'] );
    $product->set_sale_price( isset( $args['sale_price'] ) ? $args['sale_price'] : '' );
    $product->set_price( isset( $args['sale_price'] ) ? $args['sale_price'] :  $args['regular_price'] );
    if( isset( $args['sale_price'] ) ){
        $product->set_date_on_sale_from( isset( $args['sale_from'] ) ? $args['sale_from'] : '' );
        $product->set_date_on_sale_to( isset( $args['sale_to'] ) ? $args['sale_to'] : '' );
    }

    // Downloadable (boolean)
    $product->set_downloadable(  isset($args['downloadable']) ? $args['downloadable'] : false );
    if( isset($args['downloadable']) && $args['downloadable'] ) {
        $product->set_downloads(  isset($args['downloads']) ? $args['downloads'] : array() );
        $product->set_download_limit(  isset($args['download_limit']) ? $args['download_limit'] : '-1' );
        $product->set_download_expiry(  isset($args['download_expiry']) ? $args['download_expiry'] : '-1' );
    }

    // Taxes
    if ( get_option( 'woocommerce_calc_taxes' ) === 'yes' ) {
        $product->set_tax_status(  isset($args['tax_status']) ? $args['tax_status'] : 'taxable' );
        $product->set_tax_class(  isset($args['tax_class']) ? $args['tax_class'] : '' );
    }

    // SKU and Stock (Not a virtual product)
    if( isset($args['virtual']) && ! $args['virtual'] ) {
        $product->set_sku( isset( $args['sku'] ) ? $args['sku'] : '' );
        $product->set_manage_stock( isset( $args['manage_stock'] ) ? $args['manage_stock'] : false );
        $product->set_stock_status( isset( $args['stock_status'] ) ? $args['stock_status'] : 'instock' );
        if( isset( $args['manage_stock'] ) && $args['manage_stock'] ) {
            $product->set_stock_status( $args['stock_qty'] );
            $product->set_backorders( isset( $args['backorders'] ) ? $args['backorders'] : 'no' ); // 'yes', 'no' or 'notify'
        }
    }

    // Sold Individually
    $product->set_sold_individually( isset( $args['sold_individually'] ) ? $args['sold_individually'] : false );

    // Weight, dimensions and shipping class
    $product->set_weight( isset( $args['weight'] ) ? $args['weight'] : '' );
    $product->set_length( isset( $args['length'] ) ? $args['length'] : '' );
    $product->set_width( isset(  $args['width'] ) ?  $args['width']  : '' );
    $product->set_height( isset( $args['height'] ) ? $args['height'] : '' );
    if( isset( $args['shipping_class_id'] ) )
        $product->set_shipping_class_id( $args['shipping_class_id'] );

    // Upsell and Cross sell (IDs)
    $product->set_upsell_ids( isset( $args['upsells'] ) ? $args['upsells'] : '' );
    $product->set_cross_sell_ids( isset( $args['cross_sells'] ) ? $args['upsells'] : '' );

    // Attributes et default attributes
    if( isset( $args['attributes'] ) )
        $product->set_attributes( wc_prepare_product_attributes($args['attributes']) );
    if( isset( $args['default_attributes'] ) )
        $product->set_default_attributes( $args['default_attributes'] ); // Needs a special formatting

    // Reviews, purchase note and menu order
    $product->set_reviews_allowed( isset( $args['reviews'] ) ? $args['reviews'] : false );
    $product->set_purchase_note( isset( $args['note'] ) ? $args['note'] : '' );
    if( isset( $args['menu_order'] ) )
        $product->set_menu_order( $args['menu_order'] );

    // Product categories and Tags
    if( isset( $args['category_ids'] ) )
        $product->set_category_ids( $args['category_ids'] );
    if( isset( $args['tag_ids'] ) )
        $product->set_tag_ids( $args['tag_ids'] );


    // Images and Gallery
    $product->set_image_id( isset( $args['image_id'] ) ? $args['image_id'] : "" );
    $product->set_gallery_image_ids( isset( $args['gallery_ids'] ) ? $args['gallery_ids'] : array() );

    ## --- SAVE PRODUCT --- ##
    $product_id = $product->save();

    return $product_id;
}

// Utility function that returns the correct product object instance
function wc_get_product_object_type( $type ) {
    // Get an instance of the WC_Product object (depending on his type)
    if( isset($args['type']) && $args['type'] === 'variable' ){
        $product = new WC_Product_Variable();
    } elseif( isset($args['type']) && $args['type'] === 'grouped' ){
        $product = new WC_Product_Grouped();
    } elseif( isset($args['type']) && $args['type'] === 'external' ){
        $product = new WC_Product_External();
    } else {
        $product = new WC_Product_Simple(); // "simple" By default
    } 
    
    if( ! is_a( $product, 'WC_Product' ) )
        return false;
    else
        return $product;
}

// Utility function that prepare product attributes before saving
function wc_prepare_product_attributes( $attributes ){
    global $woocommerce;

    $data = array();
    $position = 0;

    foreach( $attributes as $taxonomy => $values ){
        if( ! taxonomy_exists( $taxonomy ) )
            continue;

        // Get an instance of the WC_Product_Attribute Object
        $attribute = new WC_Product_Attribute();

        $term_ids = array();

        // Loop through the term names
        foreach( $values['term_names'] as $term_name ){
            if( term_exists( $term_name, $taxonomy ) )
                // Get and set the term ID in the array from the term name
                $term_ids[] = get_term_by( 'name', $term_name, $taxonomy )->term_id;
            else
                continue;
        }

        $taxonomy_id = wc_attribute_taxonomy_id_by_name( $taxonomy ); // Get taxonomy ID

        $attribute->set_id( $taxonomy_id );
        $attribute->set_name( $taxonomy );
        $attribute->set_options( $term_ids );
        $attribute->set_position( $position );
        $attribute->set_visible( $values['is_visible'] );
        $attribute->set_variation( $values['for_variation'] );

        $data[$taxonomy] = $attribute; // Set in an array

        $position++; // Increase position
    }
    return $data;
}

2) 用法:-具有两个产品属性“颜色”和“大小”的简单产品创建示例:

$product_id = create_product( array(
    'type'               => '', // Simple product by default
    'name'               => __("The product title", "woocommerce"),
    'description'        => __("The product description…", "woocommerce"),
    'short_description'  => __("The product short description…", "woocommerce"),
    // 'sku'                => '',
    'regular_price'      => '5.00', // product price
    // 'sale_price'         => '',
    'reviews_allowed'    => true,
    'attributes'         => array(
        // Taxonomy and term name values
        'pa_color' => array(
            'term_names' => array('Red', 'Blue'),
            'is_visible' => true,
            'for_variation' => false,
        ),
        'pa_size' =>  array(
            'term_names' => array('X Large'),
            'is_visible' => true,
            'for_variation' => false,
        ),
    ),
) );  

// Displaying the created product ID
echo $product_id;

仅使用WooCommerce 3 CRUD方法测试和工作。

 类似资料:
  • 我有一个WS,它返回非常基本的产品数据:代码、价格和图像。我需要用这些基本数据以编程方式创建Hybris产品,然后进行同步,以便在店面上看到这些产品。 创建具有这些基本信息的产品的步骤是什么?有OOTB服务吗?

  • 我需要从php脚本手动创建一个产品(例如,我正在开发一个自定义前端表单以插入产品) 谷歌我看到一些支持论坛手动创建帖子(使用wp\u insert\u post功能)。我认为这个解决方案是不可靠的(随着woocommerce的更新,情况可能会发生变化)。 我需要一个wc函数为我做这件事,一种wc_create_product(像wc_create_order函数)。 我发现似乎是我需要的,但在网上

  • 如何从插件为WooCommerce创建属性?我只发现: 从这个问题 但这种方法需要某些产品的id。我需要生成一些未附加到任何产品的属性。

  • 我试图以编程方式创建一个订单。使用非常简单: 这符合预期,并且为ID为100的简单产品创建了正确数量的订单。 然而,如果我尝试用一个变体来实现这一点,它的行为似乎并不正确。经过多次尝试和错误,我以这种方式完成了工作: 然而,尽管它确实创建了具有正确产品和正确变化ID的订单,订单的总数总是0(巧合的是,这是第一个变化的价格)。 最初我尝试的是

  • 问题内容: 有没有一种方法可以在Windows中创建链接? 我发现使用的样本或必须下载的样本。 我需要一个简单的解决方案。那可能吗? 问题答案: 好了,我不得不使用创建快捷方式的方法,但是实际上我需要的是一个文件夹,但是会创建一个。 最终我用来创建。

  • 问题内容: 我一直在尝试以编程方式重做我的应用程序上的工作。(不使用情节提要) 除了手动制作导航控制器外,我几乎完成了。 我一直在做一些研究,但找不到任何手动实现此方法的文档。(我开始将应用程序制作为单视图应用程序) 目前,我只有1个ViewController。当然是appDelegate 导航控制器将在应用程序的所有页面中使用。 如果有人可以帮助我,或发送指向一些适当文档的链接以编程方式进行此