如何在Woocommerce中将属性设置为变体

Der*_*rek 2 php wordpress woocommerce

我有一个我正在创建的前端表单,允许用户使用预定义的属性和变体从前端向我的商店发布变量产品.

我找到了这个非常有用的问题:这里 显示了如何将产品类型设置为变量,并在产品数据的属性部分中分配我的预定义属性.

然而,当我在Wordpress/Woocommerce的后端并编辑产品时,我点击变体并且没有设置,我查看属性,我的"分辨率"属性设置为我的3项.

如何将它实际设置为我的表单的变体?我需要使用wp_insert_post吗?查看phpmyadmin,看起来产品变体分配给parent_id(产品ID),帖子类型是product_varition,依此类推.

$new_post = array(
'post_title' => esc_attr(strip_tags($_POST['postTitle'])),
'post_content' => esc_attr(strip_tags($_POST['postContent'])),
'post_status' => 'publish',
'post_type' => 'product',
'tags_input' => array($tags)
);

$skuu = rand();
$post_id = wp_insert_post($new_post);
update_post_meta($post_id, '_sku', $skuu );

//my array for setting the attributes
$avail_attributes = array(
    'high-resolution',
    'medium-resolution',
    'low-resolution'
);

//Sets the attributes up to be used as variations but doesnt actually set them up as variations
wp_set_object_terms ($post_id, 'variable', 'product_type');
wp_set_object_terms( $post_id, $avail_attributes, 'pa_resolution' );


$thedata = array(
'pa_resolution'=> array(
                'name'=>'pa_resolution',
                'value'=>'',
                'is_visible' => '1',
                'is_variation' => '1',
                'is_taxonomy' => '1'
                )
);
update_post_meta( $post_id,'_product_attributes',$thedata);

update_post_meta( $post_id, '_visibility', 'search' );
update_post_meta( $post_id, '_stock_status', 'instock');
Run Code Online (Sandbox Code Playgroud)

所以只是为了清楚(我往往会感到困惑)以上确实从前端创建了我的变量产品,当我在后端看产品时它是一个变量产品,它有分辨率属性设置并且有我的3术语(高分辨率,中等分辨率,低分辨率)作为属性.我只需要更进一步,将它们实际设置为变体,以便人们可以下订单.

已经将属性添加到产品中 需要它来添加变化

Der*_*rek 5

我通过使用update_post_meta和wp_insert_post让它适应我的情况.因为我已经设置了我的属性和术语,所以我需要的是一种添加到上面代码的方法,这样在创建产品时,它不仅会将属性分配给产品,还会将它们作为数据库中的变体插入.

这是我的解决方案:

//insert variations post_type
$i=1;
while ($i<=3) {
$my_post = array(
      'post_title'    => 'Variation #' . $i . ' of ' . esc_attr(strip_tags($_POST['postTitle'])),
      'post_name'     => 'product-' . $post_id . '-variation-' . $i,
      'post_status'   => 'publish',
      'post_parent'   => $post_id,
      'post_type'     => 'product_variation',
      'guid'          =>  home_url() . '/?product_variation=product-' . $post_id . '-variation-' . $i
    );

    // Insert the post into the database
    wp_insert_post( $my_post );

    $variable_id = $post_id + 1;
    $variable_two = $variable_id + 1;
    $variable_three = $variable_two + 1;

    update_post_meta( $variable_id, 'attribute_pa_resolution', 'high-resolution');
    update_post_meta( $variable_id, '_price', 8.50 );
    update_post_meta( $variable_id, '_regular_price', '8.50');

    update_post_meta( $variable_two, 'attribute_pa_resolution', 'medium-resolution');
    update_post_meta( $variable_two, '_price', 5.50 );
    update_post_meta( $variable_two, '_regular_price', '5.50');

    update_post_meta( $variable_three, 'attribute_pa_resolution', 'low-resolution');
    update_post_meta( $variable_three, '_price', 3.50 );
    update_post_meta( $variable_three, '_regular_price', '3.50');

    $i++;
    }
Run Code Online (Sandbox Code Playgroud)