【问题标题】:Woocommerce - How to add product variation from external scriptWoocommerce - 如何从外部脚本添加产品变体
【发布时间】:2015-01-12 12:52:47
【问题描述】:

我有一个变量产品,其分类“pa_size”属性设置为以下值:S|M|L|XL

如何使用外部 php 脚本为该产品添加变体?

提前致谢。

我使用以下代码:

    $sku = 21333;
    $size = 'S';
    $stock = 2;
    $price_a = 60;
    $price_b = 30;

    $product_parent = get_product_by_sku($sku);
    $product = new WC_Product_Variable($product_parent->id);
    $variations = $product->get_available_variations();

    // First off all delete all variations
    foreach($variations as $prod_variation) {
      $metaid=mysql_query("SELECT meta_id FROM wp_postmeta WHERE post_id = ".$prod_variation['variation_id']);
      while ($row = mysql_fetch_assoc($metaid)) {
        mysql_query("DELETE FROM wp_postmeta WHERE meta_id = ".$row['meta_id']);
      }
      mysql_query("DELETE FROM wp_posts WHERE ID = ".$prod_variation['variation_id']);
    }

    // Now add new variation
    $thevariation = array(
      'post_title'=> '',
      'post_name' => 'product-' . $product_parent->id . '-variation',
      'post_status' => 'publish',
      'post_parent' => $product_parent->id,
      'post_type' => 'product_variation',
      'guid'=>home_url() . '/?product_variation=product-' . $product_parent->id . '-variation'
    );
    $variation_id = wp_insert_post( $thevariation );
    update_post_meta($variation_id, 'post_title', 'Variation #' . $variation_id . ' of '. $product_parent->id);

    wp_set_object_terms( $variation_id, $size, 'pa_size' );
    update_post_meta($variation_id, 'attribute_pa_size', $size);

    update_post_meta($variation_id, '_regular_price', $price_a);
    update_post_meta($variation_id, '_downloadable_files', '');
    update_post_meta($variation_id, '_download_expiry', '');
    update_post_meta($variation_id, '_download_limit', '');
    update_post_meta($variation_id, '_sale_price_dates_to', '');
    update_post_meta($variation_id, '_sale_price_dates_from', '');
    update_post_meta($variation_id, '_backorders', 'no');
    update_post_meta($variation_id, '_stock_status', 'instock');
    update_post_meta($variation_id, '_height', '');
    update_post_meta($variation_id, '_manage_stock', 'yes');
    update_post_meta($variation_id, '_width', '');
    update_post_meta($variation_id, '_sale_price_dates_from', '');
    update_post_meta($variation_id, '_backorders', 'no');
    update_post_meta($variation_id, '_stock_status', 'instock');
    update_post_meta($variation_id, '_manage_stock', 'yes');
    update_post_meta($variation_id, '_height', '');
    update_post_meta($variation_id, '_width', '');
    update_post_meta($variation_id, '_length', '');
    update_post_meta($variation_id, '_weight', '');
    update_post_meta($variation_id, '_downloadable', 'no');
    update_post_meta($variation_id, '_virtual', 'no');
    update_post_meta($variation_id, '_thumbnail_id', '0');
    update_post_meta($variation_id, '_sku', '');

    update_post_meta($variation_id, '_sale_price', $price_b);
    update_post_meta($variation_id, '_price', $price_b);
    update_post_meta($product_parent->id, '_min_variation_price', $price_b);
    update_post_meta($product_parent->id, '_max_variation_price', $price_b);
    update_post_meta($product_parent->id, '_min_price_variation_id', $variation_id);
    update_post_meta($product_parent->id, '_max_price_variation_id', $variation_id);
    update_post_meta($product_parent->id, '_min_variation_regular_price', $price_a);
    update_post_meta($product_parent->id, '_max_variation_regular_price', $price_a);
    update_post_meta($product_parent->id, '_min_regular_price_variation_id', $variation_id);
    update_post_meta($product_parent->id, '_max_regular_price_variation_id', $variation_id);
    update_post_meta($product_parent->id, '_min_variation_sale_price', $price_b);
    update_post_meta($product_parent->id, '_max_variation_sale_price', $price_b);
    update_post_meta($product_parent->id, '_min_sale_price_variation_id', $variation_id);
    update_post_meta($product_parent->id, '_max_sale_price_variation_id', $variation_id);
    update_post_meta($product_parent->id, '_price', $price_b);

    update_post_meta( $variation_id, '_stock', $stock );
    update_post_meta($product_parent->id, 'post_status', 'publish');

【问题讨论】:

  • 您可能需要详细说明。外部脚本中有什么?你为什么需要它?只是一些帮助人们帮助你的事情。
  • 嗨@helgatheviking,我需要使用php为每个属性值创建变体产品。示例:对于尺寸“S”,我想创建价格为 30 美元和 2 个库存的变体。
  • 但是您为什么不能直接进入管理员并创建一个 30 美元且有 2 个库存的“S”变体?如果有帮助,请注意变体是可变父帖子的子帖子。所以你可以使用wp_insert_post()之类的东西。
  • @helgatheviking 我已经编辑了我的第一篇文章,请看一下。

标签: php woocommerce product variation


【解决方案1】:

您可以在save_post 上运行一个操作,以便在保存/更新帖子时做任何您想做的事情。或者对于 WooCommerce 产品,您可以在他们的 woocommerce_process_product_meta 钩子上运行它,该钩子在 save_post 上运行,但已经进行了一些检查以确保它仅适用于产品并且用户具有正确的权限等。使用他们的hook,我们可以将条件逻辑简化为检查产品是否是可变产品。如果是,请运行一些自定义代码。

add_action( 'woocommerce_process_product_meta', 'so_27902470_update_variations', 10, 2 );
function so_27902470_update_variations( $post_id, $post ){
    if( isset( $post['product_type'] ) && 'variable' == $post['product_type'] ){
        // do your stuff
    }
    return $post_id;
}

【讨论】:

    猜你喜欢
    • 2019-07-29
    • 1970-01-01
    • 1970-01-01
    • 2022-10-25
    • 2018-05-10
    • 2015-07-12
    • 2018-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多