Skip to content
Advertisement

WooCommerce programmatically created product ignores prices sorting filter

I have following function to create new woocommerce product on the frontend.

parse_str($_POST['form_data'], $form_data);
$insertPost = array(
    //'ID'          => $post_id,
    'post_title' => $form_data['post_title'],
    'post_status' => $form_data['post_status'],
    'post_type' => 'product',
    'post_content' => $form_data['post_content']
);
$insert_post = wp_insert_post( $insertPost, true);
update_post_meta( $insert_post, 'hours', $form_data['hours'] );
update_post_meta( $insert_post, 'year', $form_data['year'] );
update_post_meta( $insert_post, '_weight', $form_data['_weight'] );
update_post_meta( $insert_post, '_price', $form_data['_regular_price'] );
update_post_meta( $insert_post, '_regular_price', $form_data['_regular_price'] );
update_post_meta( $insert_post, '_sale_price', $form_data['_sale_price'] );
update_post_meta( $insert_post, '_stock_status', $form_data['_stock_status'] );
$term_taxonomy_ids = wp_set_object_terms( $insert_post, array(15), 'product_cat' );
wp_set_object_terms( $insert_post, 'simple', 'product_type' );

The code above is creating a new product and adding all data to it as expected. The issue is when using the sort by price filter, the sorting doesn’t really sort the newly product created in proper way, it just ignores it.

After searching i found in WooCommerce > Status > Tools > Product lookup tables (regenerate) once i click the regenerate button, then it regenerate all product data and the price sorting is working as expected.

So what is missing in my function to make the price sorting work without regenerate button?

Advertisement

Answer

There are some custom WooCommerce database tables and cached data that your outdated code doesn’t handle, so use the following (using WC_Product methods) like:

parse_str($_POST['form_data'], $form_data);

// Get an empty instance of the WC_Product Object (with the correct product type)
$product = new WC_Product_Simple(); // "simple" product type

$product->set_name($form_data['post_title']);
$product->set_status($form_data['post_status']);
$product->set_description($form_data['post_content']);

if( isset($form_data['post_excerpt']) ) {
    $product->set_short_description($form_data['post_excerpt']); // Optional
}

$product->add_meta_data('hours', $form_data['hours']);
$product->add_meta_data('year', $form_data['year']);

$product->set_price($form_data['_regular_price']);
$product->set_regular_price($form_data['_regular_price']);
$product->set_sale_price($form_data['_sale_price']);

$product->set_weight($form_data['_weight']);

$product->set_stock_status($form_data['_stock_status']);

$product->set_category_ids( array(15) );

$product->save(); // Save to database

It should work now with “sort by price filter” without regenerating product data.

Related: Create programmatically a product using CRUD methods in Woocommerce 3

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement