Php 为可变产品创建多个产品变体问题

Php 为可变产品创建多个产品变体问题,php,wordpress,woocommerce,product,product-variations,Php,Wordpress,Woocommerce,Product,Product Variations,我试图以编程方式为一个可变产品添加两个产品变体。 基于,我使用以下缩短的函数: function create_product_variation( $product_id, $variation_data ){ $product = wc_get_product($product_id); $variation_post = array( 'post_title' => $product->get_name(), 'po

我试图以编程方式为一个可变产品添加两个产品变体。 基于,我使用以下缩短的函数:

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\u product\u id
是我要对其进行更改的变量产品的id,
$variations\u data
是上面定义的数据数组

我的问题是
update\u post\u meta()
函数只插入foreach in函数中的最后一个数据

i、 e.在商业中的产品变化有:

红色W1

红色W1

但我想:

蓝色W1


红色W1

在您的代码中,第一个foreach循环需要正好位于以下代码行之前:

$variation_id = wp_insert_post( $variation_post );
比如:

正如
wp\u insert\u post()
在数据库中创建具有唯一变体ID的产品变体一样,因此需要为数据数组中的每个变体执行此操作

这将解决您的主要相关问题


相关的:


谢谢。它工作正常。我使用ajax形式的函数。所以它重复了这个过程。i、 e.根据我的问题示例,输出为:蓝色W1红色W1蓝色W1红色W1正如您在所讨论的数据数组中看到的,函数逻辑上创建了两个变量乘积。但输出是重复的。i、 e.每个产品有两套可变产品。我谷歌很多,有人说是因为管理员的角色!我不明白问题出在哪里。我在wordpress仪表板中使用的代码不是frontend@WebDesign你是如何使用它的…最好的事情是问一个新的问题与代码,你正在使用它的后端。
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();
    }
}