WordPress.org

Ready to get started?Download WordPress

Forums

WooCommerce - excelling eCommerce
programmatically make an attribute a variation (3 posts)

  1. markwaregr
    Member
    Posted 1 year ago #

    hello how can i make an attribute of a product a variation with custom price but programmatically.

    http://wordpress.org/extend/plugins/woocommerce/

  2. jrgd
    Member
    Posted 1 year ago #

    hi mark, it seems i'm following your steps on this topic, on stackoverflow and hereā€”did you manage to get this working?

    i'm stuck with the creation of attributes and their terms
    can you shed some light?

  3. markwaregr
    Member
    Posted 1 year ago #

    hi yes i did, i posted it in wordpress overflow, i really forgot to post it here also!

    Found the solution to make a product attribute, a variation. Lets say we have wp_set_object_terms( $post_id, 'XL', 'pa_size' ); The above is a custom attribute (a size attribute). Making it a variation will look like
    $new_post = array(
    'post_title' => "Custom Variable",
    'post_content' => 'Lorem ipsum dolor sit amet...',
    'post_status' => 'publish',
    'post_type' => 'product'
    );

    $skuu = "random";
    $post_id = wp_insert_post($new_post);
    update_post_meta($post_id, '_sku', $skuu );
    update_post_meta( $post_id, '_price', "25" );

    //made it variable but variations wont be added!
    wp_set_object_terms ($post_id, 'variable', 'product_type');
    wp_set_object_terms( $post_id, 'XL', 'pa_size' );
    /* the solution is here under */
    $thedata = Array('pa_size'=>Array(
    'name'=>'pa_size',
    'value'=>'',
    'is_visible' => '1',
    'is_variation' => '1',
    'is_taxonomy' => '1'
    ));
    update_post_meta( $post_id,'_product_attributes',$thedata);

    /* end of my solution */

    update_post_meta( $post_id, '_visibility', 'search' );
    update_post_meta( $post_id, '_stock_status', 'instock');

    so now you have it pa_size its a variation

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic