• Resolved misstest

    (@misstest)


    I have this code that when a new vendor is created, it automatically publish a new product on his page.

    add_action( 'wcfmmp_new_store_created', function( $member_id, $wcfmmp_settings ) {
    $product_args = array(
    
    'post_title'   => 'Product title',
    'post_status'  => 'publish',
    'post_type'    => 'product',
    'post_excerpt' => 'Description',
    'post_content' => 'Here is the content of the product',
    'post_author'  => $member_id,
    'post_name'    => sanitize_title('Product title')
    );
    $new_product_id = wp_insert_post( $product_args, true );
    update_post_meta( $new_product_id, '_wcfm_product_author', $member_id );
    
    // Set Product Type
    wp_set_object_terms( $new_product_id, 'simple', 'product_type' );
    
    // Set Price
    update_post_meta( $new_product_id, '_regular_price', '0' );
    update_post_meta( $new_product_id, '_price', '0' );
    }, 50, 2 );

    The issue is that it doesn’t set an image. Is there a way to add the image of the product?

    • This topic was modified 5 years, 7 months ago by misstest.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author WC Lovers

    (@wclovers)

    The issue is that it doesn’t set an image. Is there a way to add the image of the product?

    – Possible. Which image do you want to set? Know me that image attachment ID.

    Thread Starter misstest

    (@misstest)

    I want to set the main image of the product. For example: The image with the ID 488

    Plugin Author WC Lovers

    (@wclovers)

    OK, use this revised code –

    add_action( 'wcfmmp_new_store_created', function( $member_id, $wcfmmp_settings ) {
    $product_args = array(
    
    'post_title'   => 'Product title',
    'post_status'  => 'publish',
    'post_type'    => 'product',
    'post_excerpt' => 'Description',
    'post_content' => 'Here is the content of the product',
    'post_author'  => $member_id,
    'post_name'    => sanitize_title('Product title')
    );
    $new_product_id = wp_insert_post( $product_args, true );
    update_post_meta( $new_product_id, '_wcfm_product_author', $member_id );
    
    // Set Product Type
    wp_set_object_terms( $new_product_id, 'simple', 'product_type' );
    
    // Set Price
    update_post_meta( $new_product_id, '_regular_price', '0' );
    update_post_meta( $new_product_id, '_price', '0' );
    
    // Featured Image
    set_post_thumbnail( $new_product_id, 488 );
    }, 50, 2 );
    Thread Starter misstest

    (@misstest)

    Thank you so much!!!!!!!!!

    Plugin Author WC Lovers

    (@wclovers)

    You are always welcome 😊

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Publish product with image’ is closed to new replies.