【问题标题】:SQL queries to create variable products on woocommerce在 woocommerce 上创建可变产品的 SQL 查询
【发布时间】:2018-06-22 18:34:58
【问题描述】:

我正在尝试使用 mysql 和 php 创建可变产品。 我发现 woocommerce 在 wp_posts 表上存储产品但类型为“产品”

变体也保存在同一张表上,但使用 'post_parent' = produt id

我还在 wp_term_relationships 上为可变产品设置了 term_taxonomy_id = 6。

在 wp_postmeta 上,我添加了一些变化数据。

产品已创建,但没有变化。 谁能帮帮我?

【问题讨论】:

    标签: php sql database wordpress woocommerce


    【解决方案1】:

    每个变体都作为自己的产品存储在 wp_posts 表中,并在 post_parent 字段中引用原始产品。

    希望这会有所帮助。

    【讨论】:

    • 感谢您的评论,但我已经在我的问题中指定了它。同样,在我创建其他产品的变体后,它们将不会显示
    【解决方案2】:

    如果您已经在创建可变产品并想为该产品添加变体,下面给出的代码将为您提供帮助。

    1- 假设你有一个属性是 size 有变化,比如 -

    $avail_attributes = array(
    '2xl',
    'xl',
    'lg',
    );
    wp_set_object_terms($post_id, $avail_attributes, 'pa_size');
    $thedata = Array('pa_size'=>Array(
    'name'=>'pa_size',
    'value'=>'',
    'is_visible' => '1',
    'is_variation' => '1',
    'is_taxonomy' => '1'
    ));
    update_post_meta( $new_post_id,'_product_attributes',$thedata);
    update_post_meta( $post_id, '_stock_status', 'instock');
    update_post_meta( $post_id, '_weight', "0.06" );
    update_post_meta( $post_id, '_sku', "skutest1");
    update_post_meta( $post_id, '_stock', "100" );
    update_post_meta( $post_id, '_visibility', 'visible' );
    

    2- 现在你要做的就是像这样向产品添加变体--

    $i=1;
    while ($i<=5) {
    $post = array(
    'post_title'=> 'Variation #' . $i . ' of 5 for prdct#'. $post_id,
    'post_name' => 'product-' . $post_id . '-variation-' . $i,
    'post_status' => 'publish',
    'post_parent' => $post_id,
    'post_type' => 'product_variation',
    'guid'=>home_url() . '/?product_variation=product-' . $post_id . '-variation-' . $i
    );
    $attID = wp_insert_post( $post );
    $logtxt .= "Attribute inserted with ID: $attID\n";
    $variation_id = $post_id + 1;
    $variation_two = $variation_id + 1;
    $variation_three = $variation_two + 1;
    $variation_four = $variation_three + 1;
    $variation_five = $variation_four + 1;
    update_post_meta($variation_id, 'attribute_pa_size', '2xl'); update_post_meta($variation_id, '_price', 21.99);
    update_post_meta($variation_id, '_regular_price', '21.99');
    wp_set_object_terms($variation_id, $avail_attributes, 'pa_size');
    $thedata = Array('pa_size'=>Array(
    'name'=>'2xl',
    'value'=>'',
    'is_visible' => '1',
    'is_variation' => '1',
    'is_taxonomy' => '1'
    ));
    update_post_meta( $variation_id,'_product_attributes',$thedata);
    update_post_meta( $variation_two, 'attribute_pa_size', 'xl');
    update_post_meta( $variation_two, '_price', 20.99 );
    update_post_meta( $variation_two, '_regular_price', '20.99');
    wp_set_object_terms($variation_two, $avail_attributes, 'pa_size');
    $thedata = Array('pa_size'=>Array(
    'name'=>'xl',
    'value'=>'',
    'is_visible' => '1',
    'is_variation' => '1',
    'is_taxonomy' => '1'
    ));
    update_post_meta( $variation_two,'_product_attributes',$thedata);
    update_post_meta( $variation_three, 'attribute_pa_size', 'lg');
    update_post_meta( $variation_three, '_price', 18.99 );
    update_post_meta( $variation_three, '_regular_price', '18.99');
    wp_set_object_terms($variation_three, $avail_attributes, 'pa_size');
    $thedata = Array('pa_size'=>Array(
    'name'=>'lg',
    'value'=>'',
    'is_visible' => '1',
    'is_variation' => '1',
    'is_taxonomy' => '1'
    ));
    update_post_meta( $variation_three,'_product_attributes',$thedata);
    update_post_meta( $variation_four, 'attribute_pa_size', 'md');
    update_post_meta( $variation_four, '_price', 18.99 );
    update_post_meta( $variation_four, '_regular_price', '18.99');
    $i++;
    }
    

    【讨论】:

      猜你喜欢
      • 2015-09-05
      • 2019-05-11
      • 2017-10-30
      • 2017-05-23
      • 1970-01-01
      • 1970-01-01
      • 2018-03-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多