如何用php代码在WooCommerce中添加产品

时间:2014-03-10 作者:Hossein Hashemi

我想添加带有PHP代码的产品,如下所示:

$post_information = array(
  \'post_title\' => \'new item shop\',
  \'post_content\' => \'this is new item shop\',
  \'post_type\' => \'post\',
  \'post_status\' => \'publish\'
);
$post_id = wp_insert_post($post_information);
但这段代码针对WooCommerce进行了优化,如post类型、guid、元数据和。。。有人能帮忙吗?

1 个回复
最合适的回答,由SO网友:user3361421 整理而成

由于WooCommerce添加了wc_product_meta_lookup 还需要使用一些元值更新的表。

Woo现在提供了一个CRUD接口,所以请改用它。

            $post_id = wp_insert_post( array(
                \'post_title\' => $title,
                \'post_type\' => \'product\',
                \'post_status\' => \'publish\',
                \'post_content\' => $body,
            ));
            $product = wc_get_product( $post_id );
            $product->set_sku( $sku );
            // etc...
            $product->save();
审阅者,请随意编辑您认为合适的内容。

这很简单,你已经计算出了添加到post meta中的数据。我遇到的麻烦是向商店添加可下载的产品。

下面是我正在使用的代码,它列出了woo commerce使用的所有post meta。这将发布产品,但不会附加下载链接。

最初,当我开始使用存储下载链接的数组时,我犯了一个错误,产生了一个坏链接;“b”;然后是第二个正确的下载文件。在修复阵列以匹配手动添加的产品后,任何loner都不会显示文件。如果任何人对此有任何信息,将不胜感激

$post = array(
    \'post_author\' => $user_id,
    \'post_content\' => \'\',
    \'post_status\' => "publish",
    \'post_title\' => $product->part_num,
    \'post_parent\' => \'\',
    \'post_type\' => "product",
);

//Create post
$post_id = wp_insert_post( $post, $wp_error );
if($post_id){
    $attach_id = get_post_meta($product->parent_id, "_thumbnail_id", true);
    add_post_meta($post_id, \'_thumbnail_id\', $attach_id);
}

wp_set_object_terms( $post_id, \'Races\', \'product_cat\' );
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\', \'yes\');
update_post_meta( $post_id, \'_virtual\', \'yes\');
update_post_meta( $post_id, \'_regular_price\', "1" );
update_post_meta( $post_id, \'_sale_price\', "1" );
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\', "1" );
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\', "" );

// file paths will be stored in an array keyed off md5(file path)
$downdloadArray =array(\'name\'=>"Test", \'file\' => $uploadDIR[\'baseurl\']."/video/".$video);
    
$file_path =md5($uploadDIR[\'baseurl\']."/video/".$video);
    
    
$_file_paths[  $file_path  ] = $downdloadArray;
// grant permission to any newly added files on any existing orders for this product
// do_action( \'woocommerce_process_product_file_download_paths\', $post_id, 0, $downdloadArray );
update_post_meta( $post_id, \'_downloadable_files\', $_file_paths);
update_post_meta( $post_id, \'_download_limit\', \'\');
update_post_meta( $post_id, \'_download_expiry\', \'\');
update_post_meta( $post_id, \'_download_type\', \'\');
update_post_meta( $post_id, \'_product_image_gallery\', \'\');
希望符合质量标准:)

结束