如何以编程方式或使用SQL Query创建WooCommerce产品?

时间:2017-12-25 作者:Nitin Pawar

我想在不使用管理面板的情况下向购物车添加有问题的产品。添加到购物车,然后使用php代码签出。那么我如何实现它,我应该把代码放在哪里呢?

我正在插件文件中编写一个php脚本,接受插件中的post数据。我的代码是

    <?php 
    var_dump($_POST);

    $post_id = wp_insert_post( array(
    \'post_title\' => \'Great new product\',
    \'post_content\' => \'Here is content of the post, so this is our great new products 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\', \'\' );
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\', \'\' );

?>
我正在使用wordpress 4.9和woocommerce插件。请帮我解决这个问题,我是wordpress的新手,搜索了很多,但都没有找到适合我的答案。我可以在哪里写这些代码,或者我缺少什么?

1 个回复
SO网友:Lucas Bustamante

这将为您的起步奠定基础:

function generate_simple_product() {
    $name              = \'My Product Name\';
    $will_manage_stock = true;
    $is_virtual        = false;
    $price             = 1000.00;
    $is_on_sale        = true;
    $sale_price        = 999.00;
    $product           = new \\WC_Product();
    $image_id = 0; // Attachment ID
    $gallery  = self::maybe_get_gallery_image_ids();
    $product->set_props( array(
        \'name\'               => $name,
        \'featured\'           => false,
        \'catalog_visibility\' => \'visible\',
        \'description\'        => \'My awesome product description\',
        \'short_description\'  => \'My short description\',
        \'sku\'                => sanitize_title( $name ) . \'-\' . rand(0, 100), // Just an example
        \'regular_price\'      => $price,
        \'sale_price\'         => $sale_price,
        \'date_on_sale_from\'  => \'\',
        \'date_on_sale_to\'    => \'\',
        \'total_sales\'        => 0,
        \'tax_status\'         => \'taxable\',
        \'tax_class\'          => \'\',
        \'manage_stock\'       => $will_manage_stock,
        \'stock_quantity\'     => $will_manage_stock ? 100 : null, // Stock quantity or null
        \'stock_status\'       => \'instock\',
        \'backorders\'         => \'no\',
        \'sold_individually\'  => true,
        \'weight\'             => $is_virtual ? \'\' : 15,
        \'length\'             => $is_virtual ? \'\' : 15,
        \'width\'              => $is_virtual ? \'\' : 15,
        \'height\'             => $is_virtual ? \'\' : 15,
        \'upsell_ids\'         => \'\',
        \'cross_sell_ids\'     => \'\',
        \'parent_id\'          => 0,
        \'reviews_allowed\'    => true,
        \'purchase_note\'      => \'\',
        \'menu_order\'         => 10,
        \'virtual\'            => $is_virtual,
        \'downloadable\'       => false,
        \'category_ids\'       => \'\',
        \'tag_ids\'            => \'\',
        \'shipping_class_id\'  => 0,
        \'image_id\'           => $image_id,
        \'gallery_image_ids\'  => $gallery,
    ) );

    $product->save();

    return $product;
}
上述代码取自WooCommerce Smooth Generator, 由WooCommerce自己制造。它主要用于测试。

https://github.com/woocommerce/wc-smooth-generator

示例:

// Generate WC_Product object and save it to database
// 70% change generated product is simple
// 30% chance generated product is variable
$product = \\WC\\SmoothGenerator\\Generator\\Product::generate();

// Returns WC_Product object of Simple product and don\'t save it  to database
$product = \\WC\\SmoothGenerator\\Generator\\Product::generate_simple_product();

// Returns WC_Product object of Variable Product and saves it to database
$variable_product = \\WC\\SmoothGenerator\\Generator\\Product::generate_variable_product();
Src:https://github.com/woocommerce/wc-smooth-generator/blob/master/includes/Generator/Product.php

如果要以编程方式创建产品,可以根据需要编辑上面的Product generator类。

结束

相关推荐

WordPress删除带有字符串的MySQL行

因此,我正在更新一个旧wordpress插件(从未发布)的代码,我希望在导入新数据之前数据库保持“干净”,否则数据只会累积起来,我希望在每次导入之前都保持干净,这样就不会发生重复。这是我正在使用的代码,但似乎不起作用。global $wpdb; $wpdb->delete( $wpdb->postmeta WHERE meta_key LIKE \'_cb_%\' ) ); $wpdb->delete( $wpdb->posts WHERE post_type LI