2017-02-06 2 views
1

WooCommerce에서 대량 생성이 가능합니까? wp-cli Product 명령을 사용하고 있지만 하나씩 작성해야합니다.WooCommerce 제품을 프로그래밍 방식으로 대량으로 생성

<?php 

$products = array(
    array('title' => 'My product 1'), 
    array('title' => 'My product 2'), 
    // ... 
    // 10.000 more products 
); 

foreach ($products as $product) { 
    $cliProduct = new WC_CLI_Product(); 
    $cliProduct->create(array(), $product); 
    WP_CLI::success("Added Product #{$product['title']}"); 
} 

가 더 악화 각 제품에 대한 쿼리를 만들려고 때문에 시간이 많이 걸립니다, 정기적으로 실행됩니다 cron 작업을 될 것입니다. 또한 제품이 존재하는지 확인해야하며,이 경우 새 제품을 만드는 대신 업데이트해야합니다.

그래서 쿼리 수는 2

제품이 존재 곱한 것? 업데이트, 그렇지 않으면 새로 만들기

더 좋은 방법이 있나요? 어쩌면 데이터베이스를 직접 쿼리해야하지만 더러워 보일 수 있습니다.

내 자신의 DB 연결을 만들 필요없이 데이터베이스를 쿼리하는 WP 기능이 있습니까?

+1

'wp-cli'를 사용해서 만 만들고 싶습니까? 또는 사용자 정의 함수가 수행 할 것인가? 그렇다면 내 [이 답변] (http://stackoverflow.com/a/41223249/5019802)을 작성하여 질문에 답변 할 수 있습니다. –

+0

예. 제품을 만드는 스크립트는 유효합니다. WooCommerce REST API조차도 유효합니다 (지금 시도). 나는 당신의 대답을 점검 할 것입니다. –

답변

1

이와 같은 배열이 있고 제품을 식별하기 위해 고유 한 SKU이 있다고 가정합니다.

$products = [ 
    0 => [ 
     'title' => 'My Simple product 1', 
     'sku' => 'sku1', 
     'product_cat' => 'My cat' 
    //... 
    //... 
    ], 
    1 => [ 
     'title' => 'My Simple product 1', 
     'sku' => 'sku1' 
    //... 
    //... 
    ] 
]; 

메서드를 통해 위의 배열을 전달하십시오.

function myCustomProduct($product_array) 
{ 
    if (!empty($product_array)): 
     foreach ($product_array as $product): 
      $product_id = wc_get_product_id_by_sku($product['sku']); 
      //no product exist with the given SKU so create one 
      if (!$product_id): 
       $post = [ 
        'post_author' => '', 
        'post_content' => $product['content'], 
        'post_status' => "publish", 
        'post_title' => wp_strip_all_tags($product['title']), 
        'post_name' => $product['title'], 
        'post_parent' => '', 
        'post_type' => "product", 
       ]; 
       //Create Post 
       $product_id = wp_insert_post($post, $wp_error); 

       //set Product Category 
       wp_set_object_terms($product_id, $product['product_cat'], 'product_cat'); 

       //set product type 
       wp_set_object_terms($product_id, 'simple', 'product_type'); 

       update_post_meta($product_id, '_sku', $product['sku']); 
       update_post_meta($product_id, 'total_sales', '0'); 

      //product found 
      else: 
       $post = [ 
        'ID' => $product_id, 
        'post_title' => $product['title'], 
        'post_content' => $product['content'], 
       ]; 
       $post_id = wp_update_post($post, true); 
//    if (is_wp_error($post_id)) 
//    { 
//     $errors = $post_id->get_error_messages(); 
//     foreach ($errors as $error) 
//     { 
//      echo $error; 
//     } 
//    } 
      endif; 

      update_post_meta($product_id, '_visibility', 'visible'); 
      update_post_meta($product_id, '_stock_status', 'instock'); 
      update_post_meta($product_id, '_product_attributes', array()); 
      update_post_meta($product_id, '_manage_stock', "yes"); 
      update_post_meta($product_id, '_backorders', "no"); 
      update_post_meta($product_id, '_stock', $product['qty']); 
      update_post_meta($product_id, '_price', $product['price']); 
      //update_post_meta($product_id, '_downloadable', 'yes'); 
      //update_post_meta($product_id, '_virtual', 'yes'); 
      //update_post_meta($product_id, '_regular_price', "1"); 
      //update_post_meta($product_id, '_sale_price', "1"); 
      //update_post_meta($product_id, '_purchase_note', ""); 
      //update_post_meta($product_id, '_featured', "no"); 
      //update_post_meta($product_id, '_weight', ""); 
      //update_post_meta($product_id, '_length', ""); 
      //update_post_meta($product_id, '_width', ""); 
      //update_post_meta($product_id, '_height', ""); 
      //update_post_meta($product_id, '_sale_price_dates_from', ""); 
      //update_post_meta($product_id, '_sale_price_dates_to', ""); 
      //update_post_meta($product_id, '_price', "1"); 
      //update_post_meta($product_id, '_sold_individually', ""); 
     endforeach; 
    endif; 
} 

이것은 제품 작성/업데이트 방법에 대한 간단한 아이디어를 제공합니다. 추가 지원이 필요한 경우 4-5 배열 요소를 공유해야하므로 어떤 유형의 제품을 만들고 싶은지, 어떤 필드/메타인지 알 수 있습니다.

희망이 도움이됩니다.

+0

도움을 주셔서 감사합니다. 그러나 이것은 정말로 느릴 것입니다. 검색어가 너무 많습니다. 이미 단일 제품을 만들 수 있지만 문제는 청크로 제품을 만드는 방법입니다. 제품을 하나씩 추가하지 않고도 +5000 제품을 빠르게 가져올 수 있습니다. 그렇지 않으면 너무 많은 시간이 걸릴 것입니다. –

+0

WooCommerce가 단일 플랫 테이블에 데이터를 저장하지 않기 때문에 느려질 수 있습니다. "** 처음 확인한 후 업데이트 또는 작성 **"을 원하면 시간이 걸립니다. 하지만 몇 가지 샘플 데이터를 제공 할 수 있다면 코드를 최적화 할 수 있습니다. 배열을 보지 않고 저장하고 싶은 필드가 무엇인지 말하는 것은 정말 어렵습니다. –

+0

예,하지만 WooCommerce에 제가 체크 아웃 한 "제품/배치"엔드 포인트가 있음을 발견했습니다. 해당 끝점에 대한 소스 코드를 확인하고 모든 항목을 반복하고 각 항목에 대해 create_item을 호출하는 것처럼 보입니다. 일괄 적으로 만들 수있는 최적화가 없습니다. 그래서 나는 당신의 대답에 충실 할 것이라고 생각합니다. –

관련 문제