php – Woocommerce – 如何从外部脚本添加产品变体

我有一个变量产品,其分类“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');

解决方法:

那么你可以在save_post上运行一个动作,以便在保存/更新帖子时做任何你想做的事情.或者在WooCommerce产品的情况下,您可以在他们的woocommerce_process_product_meta钩子上运行它,该钩子在save_post上运行但已经有一些检查以确保它只适用于产品并且用户具有正确的权限等.使用他们的钩子,我们可以减少条件逻辑以检查产品是否是可变产品.如果是,请运行一些自定义代码.

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;
}
上一篇:从Java中提取Lotus Notes Document的完整ACL


下一篇:php – 获取Woocommerce中特定产品属性值的所有产品变体