Php WordPress Woocommerce 3.1.2-以编程方式创建可变产品

Php WordPress Woocommerce 3.1.2-以编程方式创建可变产品,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,在互联网上,我发现了许多创造可变产品的方法,但没有一种能创造可变商品。在数据库中,条目与通过“管理”面板创建的记录不对应。请帮帮我 WC版本:3.1.2 WP版本:4.8.3 来自此示例的示例代码 functions.php <?php function insert_product ($product_data) { $post = array( // Set up the basic post data to insert for our product

在互联网上,我发现了许多创造可变产品的方法,但没有一种能创造可变商品。在数据库中,条目与通过“管理”面板创建的记录不对应。请帮帮我

WC版本
3.1.2

WP版本
4.8.3

来自此示例的示例代码

functions.php

<?php 

function insert_product ($product_data)  
{
    $post = array( // Set up the basic post data to insert for our product

        'post_author'  => 1,
        'post_content' => $product_data['description'],
        'post_status'  => 'publish',
        'post_title'   => $product_data['name'],
        'post_parent'  => '',
        'post_type'    => 'product'
    );

    $post_id = wp_insert_post($post); // Insert the post returning the new post id

    if (!$post_id) // If there is no post id something has gone wrong so don't proceed
    {
        return false;
    }

    update_post_meta($post_id, '_sku', $product_data['sku']); // Set its SKU
    update_post_meta( $post_id,'_visibility','visible'); // Set the product to visible, if not it won't show on the front end

    wp_set_object_terms($post_id, $product_data['categories'], 'product_cat'); // Set up its categories
    wp_set_object_terms($post_id, 'variable', 'product_type'); // Set it to a variable product type

    insert_product_attributes($post_id, $product_data['available_attributes'], $product_data['variations']); // Add attributes passing the new post id, attributes & variations
    insert_product_variations($post_id, $product_data['variations']); // Insert variations passing the new post id & variations   
}

function insert_product_attributes ($post_id, $available_attributes, $variations)  
{
    foreach ($available_attributes as $attribute) // Go through each attribute
    {   
        $values = array(); // Set up an array to store the current attributes values.

        foreach ($variations as $variation) // Loop each variation in the file
        {
            $attribute_keys = array_keys($variation['attributes']); // Get the keys for the current variations attributes

            foreach ($attribute_keys as $key) // Loop through each key
            {
                if ($key === $attribute) // If this attributes key is the top level attribute add the value to the $values array
                {
                    $values[] = $variation['attributes'][$key];
                }
            }
        }

        // Essentially we want to end up with something like this for each attribute:
        // $values would contain: array('small', 'medium', 'medium', 'large');

        $values = array_unique($values); // Filter out duplicate values

        // Store the values to the attribute on the new post, for example without variables:
        // wp_set_object_terms(23, array('small', 'medium', 'large'), 'pa_size');
        wp_set_object_terms($post_id, $values, 'pa_' . $attribute);
    }

    $product_attributes_data = array(); // Setup array to hold our product attributes data

    foreach ($available_attributes as $attribute) // Loop round each attribute
    {
        $product_attributes_data['pa_'.$attribute] = array( // Set this attributes array to a key to using the prefix 'pa'

            'name'         => 'pa_'.$attribute,
            'value'        => '',
            'is_visible'   => '1',
            'is_variation' => '1',
            'is_taxonomy'  => '1'

        );
    }

    update_post_meta($post_id, '_product_attributes', $product_attributes_data); // Attach the above array to the new posts meta data key '_product_attributes'
}

function insert_product_variations ($post_id, $variations)  
{
    foreach ($variations as $index => $variation)
    {
        $variation_post = array( // Setup the post data for the variation

            'post_title'  => 'Variation #'.$index.' of '.count($variations).' for product#'. $post_id,
            'post_name'   => 'product-'.$post_id.'-variation-'.$index,
            'post_status' => 'publish',
            'post_parent' => $post_id,
            'post_type'   => 'product_variation',
            'guid'        => home_url() . '/?product_variation=product-' . $post_id . '-variation-' . $index
        );

        $variation_post_id = wp_insert_post($variation_post); // Insert the variation

        foreach ($variation['attributes'] as $attribute => $value) // Loop through the variations attributes
        {   
            $attribute_term = get_term_by('name', $value, 'pa_'.$attribute); // We need to insert the slug not the name into the variation post meta

            update_post_meta($variation_post_id, 'attribute_pa_'.$attribute, $attribute_term->slug);
          // Again without variables: update_post_meta(25, 'attribute_pa_size', 'small')
        }

        update_post_meta($variation_post_id, '_price', $variation['price']);
        update_post_meta($variation_post_id, '_regular_price', $variation['price']);
    }
}

function insert_products ($products)  
{
    if (!empty($products)) // No point proceeding if there are no products
    {
        array_map('insert_product', $products); // Run 'insert_product' function from above for each product
    }
}

$json = file_get_contents('my-product-data.json'); // Get json from sample file
$products_data = json_decode($json, true); // Decode it into an array

insert_products($products_data);

执行此代码后,创建了一个简单的产品。

我一直在同一个博客上,在使用Woocommerce REST API之前一直感到沮丧。我认为没有其他方法可以在不使用API的情况下创建具有变体的产品。请遵循这一点,避免沮丧,否则你会搜索整个web,找不到任何有效的东西,出于某种原因,许多博客不会告诉你,他们会一直告诉你,你和你的代码是最重要的问题。

看看专业插件WP-ALL-IMPORT
它有一个woocommerce插件。
请在此处查看他们的视频:
它将根据json文件中的变量创建具有属性的可变产品
它将拥有您想要的一切:-)

非常好的拖放功能。。。为我节省了大量时间:-)

这是使其成为可变产品的部分:

wp_set_object_terms($post_id, 'variable', 'product_type');
确保对第四个参数使用
true
,以避免使用其他术语:

wp_set_object_terms($post_id, 'variable', 'product_type', true); 
如果你看一下
insert\u product\u attributes
的代码,你会发现它实际上在使用你的“变量”术语(我想2016年的行为有所不同)

最后,最好清除该产品的瞬态:

delete_transient( 'wc_product_children_' . $post_id );
delete_transient( 'wc_var_prices_' . $post_id );

我刚刚尝试了它,它正确地导入了我的变体。

如果它没有创建任何变量,它应该会给出一些警告。请启用wp_调试并检查您收到的警告。或者检查apache错误日志文件。@Elvin85 nothing,这是我检查的第一件事。在第一个循环(foreach$variations as$index=>variation)之前的insert_product_variations函数中,执行var_dump($variations)并检查传入的数据是否正确。我猜可能存在一些解析问题,这会给最终函数带来一些不正确的数组数据。@Elvin85您可能没有正确理解我,脚本中生成的所有数据都被添加了,只是它们与wc本身创建的模式不匹配。我甚至包括了phpstorm调试和跟踪功能,所有的数据都是创建的。好吧,可能有一些误解,但我在3-4个月前在一个客户端的开发服务器上解决了类似的问题。我为此做了一些“逆向工程”:1。已获取相关mysql表中的最新状态。2.创建了简单的产品和一些变量。3.再次返回mysql表并分析哪些数据在那里被更改。4.然后编写一段代码,以编程方式进行相同的更改。它成功了。
delete_transient( 'wc_product_children_' . $post_id );
delete_transient( 'wc_var_prices_' . $post_id );