1

我正在尝试以编程方式为可变产品添加 2 个产品变体。基于this answer thread,我正在使用以下缩短功能:

function create_product_variation( $product_id, $variation_data ){

    $product = wc_get_product($product_id);
    
    $variation_post = array(
        'post_title'  => $product->get_name(),
        'post_name'   => 'product-'.$product_id.'-variation',
        'post_status' => 'publish',
        'post_parent' => $product_id,
        'post_type'   => 'product_variation',
        'guid'        => $product->get_permalink()
    );
    
    $variation_id = wp_insert_post( $variation_post );
    $variation = new WC_Product_Variation( $variation_id );
    
    foreach($variation_data as $_variation_data){
        foreach($_variation_data['attributes'] as $attribute => $term_name){
            $taxonomy = 'pa_'.$attribute;

            if( !taxonomy_exists( $taxonomy ) ){
                register_taxonomy(
                    $taxonomy,
                   'product_variation',
                    array(
                        'hierarchical' => false,
                        'label' => ucfirst($attribute),
                        'query_var' => true,
                        'rewrite' => array( 'slug' => sanitize_title($attribute)),
                    )
                );
            }

            if( ! term_exists( $term_name, $taxonomy ) )
                wp_insert_term( $term_name, $taxonomy );

            $term_slug = get_term_by('name', $term_name, $taxonomy )->slug;

            $post_term_names =  wp_get_post_terms( $product_id, $taxonomy, array('fields' => 'names') );

            if( ! in_array( $term_name, $post_term_names ) )
                wp_set_post_terms( $product_id, $term_name, $taxonomy, true );
            
            update_post_meta( $variation_id, 'attribute_'.$taxonomy, $term_slug );
        }       
    }
    
    
    $variation->save();
}

我使用以下数据数组:

$variations_data = array( 
    array( 
        'attributes' => array( 
            'color' => 'Blue',
            'warranty-and-insurance' => 'W1',
         ),
        'sku' => ,
        'regular_price' => 2000,
        'sale_price' => 1800,
        'stock_qty' => 5,
    ),
    array( 
        'attributes' => array( 
            'color' => 'Red',
            'warranty-and-insurance' => 'W1',
        ),
        'sku' => ,
        'regular_price' => 3000,
        'sale_price' => 2800,
        'stock_qty' => 3,
    )
);

然后我使用以下命令运行该函数:

create_product_variation( $variable_product_id, $variations_data ); 
    

$variable_product_id我想对其进行更改的变量产品的 id 和$variations_data上面定义的数据数组在哪里。

我的问题是该update_post_meta()函数只是在函数中插入来自 foreach 的最后一个数据。

即在woocommerce的产品变化中有:

红W1

红W1

但我想要:

蓝色 W1

红W1

4

1 回答 1

1

在您的代码中,第一个 foreach 循环必须位于以下代码行之前:

$variation_id = wp_insert_post( $variation_post );

喜欢:

function create_product_variations( $product_id, $variations_data ){
    $product = wc_get_product($product_id);
    
    $variation_post = array(
        'post_title'  => $product->get_name(),
        'post_name'   => 'product-'.$product_id.'-variation',
        'post_status' => 'publish',
        'post_parent' => $product_id,
        'post_type'   => 'product_variation',
        'guid'        => $product->get_permalink()
    );

    foreach( $variations_data as $variation_data ){
        
        $variation_id = wp_insert_post( $variation_post );
        
        $variation = new WC_Product_Variation( $variation_id );

        foreach($_variation_data['attributes'] as $attribute => $term_name){
            $taxonomy = 'pa_'.$attribute;

            if( ! taxonomy_exists( $taxonomy ) ){
                register_taxonomy(
                    $taxonomy,
                   'product_variation',
                    array(
                        'hierarchical' => false,
                        'label' => ucfirst($attribute),
                        'query_var' => true,
                        'rewrite' => array( 'slug' => sanitize_title($attribute)),
                    )
                );
            }

            if( ! term_exists( $term_name, $taxonomy ) )
                wp_insert_term( $term_name, $taxonomy );

            $term_slug = get_term_by('name', $term_name, $taxonomy )->slug;

            $post_term_names =  wp_get_post_terms( $product_id, $taxonomy, array('fields' => 'names') );

            if( ! in_array( $term_name, $post_term_names ) )
                wp_set_post_terms( $product_id, $term_name, $taxonomy, true );
            
            update_post_meta( $variation_id, 'attribute_'.$taxonomy, $term_slug );      
        }
        $variation->save();
    }
}

由于wp_insert_post()在数据库中创建具有唯一变体 ID 的产品变体,因此需要为数据数组中的每个变体执行此操作。

这应该可以解决您的主要相关问题。


有关的:

于 2020-06-25T10:04:47.367 回答