【问题标题】:How to save product attributes programmatically in Woocommerce如何在 Woocommerce 中以编程方式保存产品属性
【发布时间】:2018-12-02 10:09:04
【问题描述】:

我正在使用 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);    

【问题讨论】:

  • 不要像以前那样使用旧的方式,你应该使用new CRUD methods引入Woocommerce 3,就像this related threadthis other one一样。所以当你最后使用save() 方法时,所有的东西都会在各处同步更新(甚至是缓存的数据)。

标签: wordpress woocommerce


【解决方案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();

【讨论】:

  • 缺少第二个 $attribute $attribute->set_id(0);(0 使其成为单个非全局产品属性)。为了设置全局属性,我还必须在名称前加上“pa_”前缀。全局属性称为颜色:$attribute->set_name('pa_color');
猜你喜欢
  • 1970-01-01
  • 2015-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-04
  • 2020-09-01
  • 2018-04-27
相关资源
最近更新 更多