Skip to content
Advertisement

How do I set custom product meta on a new product using WooCommerce Core?

I’m adding new products (car listings) to my WooCommerce shop in bulk in PHP. I’m looping through rows in a custom data table and adding them as WooCommerce products.

I’m able to save each product and add the default meta (ie. description, price, sku, visibility, etc). However, I am not able to set custom product meta. Am I doing this in the wrong order?

Below is the code in my FOREACH loop that saves each row (but the “set_meta_data” is not working).

foreach ( $cars as $car ) {
    set_time_limit(60);

    $product = new WC_Product_Simple();
    $product->set_name( $car->heading );
    $product->set_status( 'publish' );
    $product->set_slug( $car->heading . '-' . uniqid() );
    $product->set_description( $car->seller_comments );
    $product->set_regular_price( $car->price );
    $product->set_sku( $car->vehicle_id );
    $product->set_virtual( true );
    $product->set_meta_data( array(
            'neg_miles' => $car->miles,
            'neg_year' => $car->year,
            'neg_trim' => $car->trim,
            'neg_drivetrain' => $car->drivetrain,
            'neg_fueltype' => $car->fuel_type,
            'neg_transmission' => $car->transmission,
            'neg_doors' => $car->doors,
            'neg_cylinders' => $car->cylinders,
            'neg_color_ext' => $car->color,
            'neg_color_int' => $car->interior_color,
            'neg_is_certified' => $car->is_certified,
            'neg_single_owner' => $car->carfax_1_owner,
            'neg_engine' => $car->engine,
            'neg_latitude' => $car->latitude,
            'neg_longitude' => $car->longitude
    ) );

    $product->save();
    $product_id = $product->get_id();

    if( !term_exists( $car->make, 'product_cat' ) ) {
        $make = wp_insert_term( $car->make, 'product_cat' ); 
        $model = wp_insert_term( $car->model, 'product_cat', array( 'parent' => $make['term_id'] ) );
    } else {
        $make = term_exists( $car->make, 'product_cat' );

        if( !term_exists( $car->model, 'product_cat' ) ) {
            $model = wp_insert_term( $car->model, 'product_cat', array( 'parent' => $make['term_id'] ) );
        } else {
            $model = term_exists( $car->model, 'product_cat' );
        }
        
    }
    wp_set_object_terms( $product_id, array( intval($make['term_id']), intval($model['term_id']) ), 'product_cat' );

    if( !empty( $car->body_type ) ) {
        if( !term_exists( $car->body_type, 'neg_body_style' ) ) {
            $body = wp_insert_term( $car->body_type, 'neg_body_style' ); 
        } else {
            $body = term_exists( $car->body_type, 'neg_body_style' );
        }
        
        wp_set_object_terms( $product_id, array( intval($body['term_id']) ), 'neg_body_style' );
    }


    $last_id = $car->ID;
} 

Advertisement

Answer

set_meta_data method require another piece of meta structure (with id).

I think is better use update_meta_data method in loop:

$product->update_meta_data('neg_miles', $car->miles);
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement