如何以编程方式添加Woocommerce产品变体?

时间:2018-02-06 16:42:32

标签: php wordpress woocommerce

首先,我知道已经有一些关于这个主题的回答问题,但是我无法找到适合我特定情况的方法。

我的Wordpress主题使用Woocommerce产品作为活动。您可以在下面看到“事件”菜单的管理页面。

绿色方块是有效的部分。我可以添加新的产品并在表格中显示。

红色方块是我可以在按钮点击上添加表格行的部分,并收集它的AJAX请求的输入值。这些行代表产品差异

Orange强调了产品的变化。如您所见,我可以列出它们,但这些都是通过“产品”菜单添加的,而不是此视图。

Admin page

还有我的问题:

我希望点击顶部的保存按钮后更新产品版本。下面是保存按钮背后的代码,以及我无法继续的一些注释。

注意:我始终只需要一个具有固定名称的属性,并且必须启用所有变体才能使用" stock"。

function empire_ajax_add_post() {
    if(!isset($_POST['date']) || !isset($_POST['title'])) {
        wp_die();
    }

    $post = [
        'post_author' => get_current_user_id(),
        'post_content' => '',
        'post_status' => "publish",
        'post_title' => $_POST['date'] . ' | ' . $_POST['title'],
        'post_parent' => '',
        'post_type' => "product",
    ];

    $post_id = wp_insert_post($post, $wp_error);
    wp_set_object_terms($post_id, 'variable', 'product_type');

    update_post_meta($post_id, '_visibility', 'visible' );
    update_post_meta($post_id, '_stock_status', 'instock');
    update_post_meta($post_id, 'total_sales', '0');
    update_post_meta($post_id, '_downloadable', 'no');
    update_post_meta($post_id, '_virtual', 'no');
    update_post_meta($post_id, '_regular_price', "1" );
    update_post_meta($post_id, '_sale_price', "1" );
    update_post_meta($post_id, '_purchase_note', "" );
    update_post_meta($post_id, '_featured', "no" );
    update_post_meta($post_id, '_weight', "" );
    update_post_meta($post_id, '_length', "" );
    update_post_meta($post_id, '_width', "" );
    update_post_meta($post_id, '_height', "" );
    update_post_meta($post_id, '_sku', "");
    update_post_meta($post_id, '_sale_price_dates_from', "" );
    update_post_meta($post_id, '_sale_price_dates_to', "" );
    update_post_meta($post_id, '_price', "1" );
    update_post_meta($post_id, '_sold_individually', "" );
    update_post_meta($post_id, '_manage_stock', "no" );
    update_post_meta($post_id, '_backorders', "no" );
    update_post_meta($post_id, '_stock', "" );

    wp_set_object_terms($post_id, [], 'pa_idopont');
    update_post_meta($post_id, '_product_attributes', [
        'idopont' => [
            'name' => 'Időpont',
            'value' => '',
            'is_visible' => '1',
            'is_variation' => '1',
            'is_taxonomy' => '1',
        ],
    ]);

    // The array key is always the ID of the parent product ($event)
    foreach($_POST['times'] as $event => $data) {
         // $data contains the values for variation name, price, and stock
         // How to update or insert them here?
    }

    wp_die();
}

修改 我花了大约6个小时才开始工作,最后成功了。链接的答案足够接近,但我需要进行这些更改(为清楚起见,删除了原始注释):

function create_product_variation( $product_id, $variation_data ){

    $product = wc_get_product($product_id);
    $variation_post = array(
        'post_title'  => $product->get_title(),
        '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['attributes'] as $attribute => $term_name )
    {
        // FIRST CHANGE : 'pa_' removed
        $taxonomy = $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') );

        // SECOND CHANGE : The next in_array will fail if it's not an array but an object of WP_Error
        if(is_wp_error($post_term_names)) {
            $post_term_names = [];
        }

        if( ! in_array( $term_name, $post_term_names ) )
            wp_set_post_terms( $product_id, $term_name, $taxonomy, true );

        // THIRD CHANGE: Save the name instead of the generated slug (which is an empty string in all of my cases)
        update_post_meta( $variation_id, 'attribute_'.$taxonomy, $term_name );
    }

    ## Set/save all other data
    // Everything else is correct.
}

0 个答案:

没有答案