add_action( 'init', 'add_product_to_cart' );
function add_product_to_cart() {
  if ( isset($_POST['mix']) && !empty($_POST['mix']) && isset($_POST['mix_name']) && !empty($_POST['mix_name']) ) {
        global $woocommerce;
        $mix_name =  $_POST['mix_name'];
        $my_post = array(
      'post_title'    => $mix_name,
      'post_content'  => 'A mix by Customer',
      'post_status'   => 'publish',
      'post_author'   => 1,
      'post_type'     =>'product'
    );

    // Insert the post into the database
    $product_ID = wp_insert_post( $my_post );
    $term = get_term_by('name', 'Mix', 'product_cat');
    wp_set_object_terms($product_ID, $term->term_id, 'product_cat');//set category for this product
    // Set product type as variable
    wp_set_object_terms( $product_ID, 'variable', 'product_type' , false);

    $attributes = array(
        'strain'=>array(
            'name'=>'strain',
            'value'=>'',
            'is_visible' => '1',
            'is_variation' => '1',
            'is_taxonomy' => '1'),

        'level'=>array(
            'name' => 'level',
            'value' => '',
            'is_visible' => '1',
            'is_variation' => '1',
            'is_taxonomy' => '1'
        )   
    );

    update_post_meta( $product_ID, '_product_attributes', $attributes );

    // Assign sizes and colors to the main product
    wp_set_object_terms( $product_ID, array( 'black berry kush', 'la confidential' ), 'strain' );
    wp_set_object_terms( $product_ID, array( 'thc 30%', 'thc 20%' ) , 'level' );



    // Start creating variations
    // The variation is simply a post
    // with the main product set as its parent
    // you might want to put that in a loop or something
    // to create multiple variations with multiple values
    $parent_id = $product_ID;

    $variation = array(
        'post_title'   => 'Product #' . $parent_id . ' Variation',
        'post_content' => '',
        'post_status'  => 'publish',
        'post_parent'  => $parent_id,
        'post_type'    => 'product_variation'
    );

    // The variation id
    $variation_id = wp_insert_post( $variation );



    if ( $product_ID ){
      add_post_meta($product_ID, '_regular_price', 100 );
      add_post_meta($product_ID, '_price', 100 );
      add_post_meta($product_ID, '_stock_status', 'instock' );

      // Assign the size and color of this variation
      update_post_meta( $variation_id, 'attribute_strain', 'black berry kush' );
      update_post_meta( $variation_id, 'attribute_level', 'thc 20%' );
      update_post_meta( $variation_id, '_regular_price', 100 );
      update_post_meta( $variation_id, '_price', 100 );
      update_post_meta($variation_id, '_stock_status', 'instock' );
      update_post_meta($variation_id, '_visibility', 'visible' );


      //Getting error on this line.
      $addcart = $woocommerce->cart->add_to_cart( $product_ID, $variation_id, $quantity=1 );     

        if($addcart) {
            wp_safe_redirect( WC()->cart->get_checkout_url() );
            exit;
        } else {
            wc_print_notices();
            _e('Can\'t add');
        }       
    }
  }
}

我创建了一个函数来创建变体产品,以便在单击按钮时添加到购物车 . 我不知道代码有什么问题,但据说

抱歉,此产品无法购买 .

如果仅使用$ product_ID添加到购物车,则成功但是当它带有$ variation_ID时,它将返回false . 有人可以告诉我如何设置变体产品的属性 . 非常感谢