我正在使用wp_insert_post()
在woocommerce中以编程方式添加产品
除非我从管理员那里单击更新,否则一切正常,除非产品属性不会显示在产品页面视图中。
我要添加大量产品,因此有什么方法可以使变化显示在产品页面的下拉菜单中。
在这里,我的代码可以正常工作,所有内容都将放置在正确的字段中,而无需单击更新就不会在产品页面上显示选项。
$new_post = array(
'ID' => '',
'post_author' => 1,
'post_title' => 'title of product',
'post_status' => 'publish',
'post_type' => 'product'
);
$post_id = wp_insert_post($new_post);
wp_set_object_terms($post_id, 'variable', 'product_type', false);
$my_post = array(
'post_title' => 'Variation # of ' . esc_attr(strip_tags( $post_title)),
'post_name' => 'product-' . $post_id . '-variation-',
'post_status' => 'publish',
'post_parent' => $post_id,
'post_type' => 'product_variation',
'guid' => home_url() . '/?product_variation=product-' . $post_id . '-variation-'
);
wp_insert_post( $my_post );
$variable_id = $post_id + 1;
update_post_meta( $variable_id, '_price', 8.50 );
update_post_meta( $variable_id, '_regular_price', '8.50');
$product_attributes['type'] = array(
'name' => htmlspecialchars(stripslashes('Options')),
'value' => "black|blue",
'position' => 1,
'is_visible' => 1,
'is_variation' => 1,
'is_taxonomy' => 0
);
update_post_meta( $post_id, '_product_attributes', $product_attributes);
答案 0 :(得分:1)
此代码将创建具有2个属性(重量,品牌)和2种变化的产品。
//Create main product
$product = new WC_Product_Variable();
$att_var = array();
//Create the attribute object with name weight
$attribute = new WC_Product_Attribute();
$attribute->set_id( 0 );
$attribute->set_name( 'weight' );
$attribute->set_options( array(
'50g',
'100g',
'150g'
) );
$attribute->set_position( 0 );
$attribute->set_visible( 1 );
$attribute->set_variation( 1 );
$att_var[] = $attribute;
//Create the attribute object with name brand
$attribute = new WC_Product_Attribute();
$attribute->set_name( 'brand' );
$attribute->set_options( array(
'Parle-G',
'Britania'
) );
$attribute->set_position( 1 );
$attribute->set_visible( 1 );
$attribute->set_variation( 1 );
$att_var[] = $attribute;
$product->set_attributes($att_var);
$product->set_name('Product 3');
$product->set_status('publish');
$product->set_sku(12345);
//Save main product to get its id
$product->set_category_ids([47, 56] );
$id = $product->save();
//variation 1
$variation = new WC_Product_Variation();
$variation->set_regular_price(10);
$variation->set_sale_price(10);
$variation->set_stock_quantity(12);
$variation->set_manage_stock(True);
$variation->set_weight('50g');
$variation->set_parent_id($id);
$variation->set_attributes(array(
'weight' => '50g',
'brand' => 'Parle-G'
));
//Save variation, returns variation id
$variation->save();
//variation 2
$variation_new = new WC_Product_Variation();
$variation_new->set_regular_price(15);
$variation_new->set_sale_price(12);
$variation_new->set_stock_quantity(20);
$variation_new->set_manage_stock(True);
$variation_new->set_weight('100g');
$variation_new->set_parent_id($id);
//Set attributes requires a key/value containing
$variation_new->set_attributes(array(
'weight' => '100g',
'brand' => 'Britania'
));
//Save variation, returns variation id
$variation_new->save();