Post create/update performance speedup

  • Unknown's avatar

    Hello, I need to speedup my import products process.

    Currently Im adding products from CSV/XML files. What I have now is create:

    `php
    $postId = wp_insert_post( array(
    ‘post_title’ => $data[‘title’],
    ‘post_type’ => ‘product’,
    ‘post_status’ => ‘publish’,
    ‘post_content’ => $data[‘description’],
    ));

    $product = wc_get_product( $postId );
    $product->set_description( $data[‘description’]);
    $product->set_short_description( $data[‘shortDescription’]);
    $product->set_regular_price($data[‘regularPrice’]);
    $product->set_sale_price($data[‘salePrice’]);
    $product->set_price($data[‘regularPrice’]);
    $product->set_sku( $data[‘sku’]);
    $categories = [];
    if ( ! empty($data[‘categories’]) ){
    $parent = null;
    foreach ($data[‘categories’] as $categoryName) {
    if ($categoryName && !isset($parent->errors)) {
    $existingCategory = is_exists_term($categoryName, self::TAXONOMY, $parent);
    if (!$existingCategory) {
    $category = wp_insert_term(
    $categoryName,
    self::TAXONOMY,
    [
    ‘description’ => ”,
    ‘slug’ => $categoryName,
    ‘parent’ => is_array($parent) ? $parent[‘term_id’] : null
    ]
    );
    $parent = $category;
    } else {
    $parent = $existingCategory;
    }

    if (!isset($parent->errors)) {
    $categories [] = $parent[‘term_id’];
    }
    }
    }
    }
    $product->set_category_ids([end($categories)]);

    echo “Created product ID: ” . $postId . “rn”;
    $this->log(true, $postId, $data[‘sku’]);
    $product->save();
    `

    product update is very similar – Im only using the
    `php
    $product = wc_get_product( $postId );
    `
    method to get product by postID

    Is there any possibility to update speed of it ?

  • Hello there,

    Many thanks for reaching out.

    Are you able to confirm the URL of the website that you need assistance with please?

  • Unknown's avatar

    Nope i can’t because of the GDPR stuff.

    maybe the main question is:

    1) does the wc_get_product is the fastest way to fetch the product object that will be updated
    2) does the wp_insert_post can be handled once with all required data because now Im creating post with wp_insert_post and then with wc_get_product im updating the rest of the post ?

  • Hello there,

    I can’t see a WordPress.com site on this account.

    If the site in question is a self-hosted WordPress site, we are unable to assist.

    Please see the differences between WordPress.com and WordPress.org here: https://en.support.wordpress.com/com-vs-org/

  • The topic ‘Post create/update performance speedup’ is closed to new replies.