0
function initial_product_data ( $post_id ) {
// lets get out of here if this is not a new product
if ( 'product' != $_POST['post_type'] )
    return;

// make sure the post isn't automagically featured
// WTF WooCommerce??!!
update_post_meta( $post_id, '_featured', 'no' );

// automatically set the swatch
update_post_meta( $post_id, '_swatch_type', 'pickers' );    
update_post_meta( $post_id, '_swatch_type_options', unserialize('a:1:{s:7:"pa_size";a:4:{s:4:"type";s:12:"term_options";s:6:"layout";s:7:"default";s:4:"size";s:19:"swatches_image_size";s:10:"attributes";a:3:{s:7:"x-large";a:3:{s:4:"type";s:5:"color";s:5:"color";s:7:"#FFFFFF";s:5:"image";s:1:"0";}s:23:"x-large-toplarge-bottom";a:3:{s:4:"type";s:5:"color";s:5:"color";s:7:"#FFFFFF";s:5:"image";s:1:"0";}s:23:"x-small-topsmall-bottom";a:3:{s:4:"type";s:5:"color";s:5:"color";s:7:"#FFFFFF";s:5:"image";s:1:"0";}}}}') );
}

add_action( 'wp_insert_post', 'initial_product_data' );

The code works fine and almost does what its supposed to do. However, it seems like it fires every time a product is saved and the user can't alter the values set on the initial product creation.

Any suggestions? Or is this a WordPress bug?

  • wp_insert_post() inserts updates too. This code does exactly what it should. – fuxia Jan 14 '14 at 18:14
  • The code does not do exactly what it should do. According to the WP Codex, when using wp_insert_post as an ACTION, it should only fire when a post is CREATED and not on every save/edit. codex.wordpress.org/Plugin_API/Action_Reference/wp_insert_post – Christopher Carvache Jan 17 '14 at 14:53
  • The codex is wrong. See the source code of wp_insert_post(). – fuxia Jan 17 '14 at 14:57
0

My suggestion would be to add in a simple flag that checks whether this is the initial time we've set our defaults, something such as below:

function initial_product_data ( $post_id ) {
// lets get out of here if this is not a new product
if ( 'product' != $_POST['post_type'] )
    return;

//Grab our initial meta value, if set will return true else wordpress will return and empty string(falsey)
$initial = get_post_meta($post_id, 'initial', true);

if(!$initial){
    // make sure the post isn't automagically featured
    // WTF WooCommerce??!!
    update_post_meta( $post_id, '_featured', 'no' );

    // automatically set the swatch
    update_post_meta( $post_id, '_swatch_type', 'pickers' );    
    update_post_meta( $post_id, '_swatch_type_options', unserialize('a:1:{s:7:"pa_size";a:4:{s:4:"type";s:12:"term_options";s:6:"layout";s:7:"default";s:4:"size";s:19:"swatches_image_size";s:10:"attributes";a:3:{s:7:"x-large";a:3:{s:4:"type";s:5:"color";s:5:"color";s:7:"#FFFFFF";s:5:"image";s:1:"0";}s:23:"x-large-toplarge-bottom";a:3:{s:4:"type";s:5:"color";s:5:"color";s:7:"#FFFFFF";s:5:"image";s:1:"0";}s:23:"x-small-topsmall-bottom";a:3:{s:4:"type";s:5:"color";s:5:"color";s:7:"#FFFFFF";s:5:"image";s:1:"0";}}}}') );

    //Set our initial check value to true, to prevent the above code running again
    update_post_meta($post_id, 'initial', true);
    }
}
add_action( 'wp_insert_post', 'initial_product_data' );

That should do the trick and only set the default upon the initial creation of the product.

  • This is an interesting solution. It works but I hate that its a work around. According to the codex this seems like its a bug. – Christopher Carvache Jan 17 '14 at 14:56
  • I know. But at this time and place it is the correct thing to do, plus the code is sound. – MichaelJames Jan 17 '14 at 18:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.