Skip to content

Instantly share code, notes, and snippets.

@JustinSainton
Created April 17, 2013 00:13
Show Gist options
  • Save JustinSainton/5400735 to your computer and use it in GitHub Desktop.
Save JustinSainton/5400735 to your computer and use it in GitHub Desktop.
Here's the gist of it (buh dun dun pssssh) - The '_wpsc_product_metadata' meta key is an array of metadata. There's a key in that array that I always want to return false ( '0' in this case). I'd have thought that filtering get_post_metadata, removing the filter, grabbing the value, modifying and returning would do the trick. It doesn't. Always …
<?php
add_filter( 'get_post_metadata', 'zao_edit_per_product_shipping', 10, 4 );
function zao_edit_per_product_shipping( $check, $object_id, $meta_key, $single ) {
if ( '_wpsc_product_metadata' === $meta_key && has_filter( 'get_post_metadata', 'zao_edit_per_product_shipping' ) ) {
remove_filter( 'get_post_metadata', 'zao_edit_per_product_shipping', 10, 4 );
$value = get_post_meta( $object_id, $meta_key, $single );
$value['no_shipping'] = '0';
add_filter( 'get_post_metadata', 'zao_edit_per_product_shipping', 10, 4 );
return $value;
}
return $check;
}
@JustinSainton
Copy link
Author

Basically it's just a null variable - get_metadata checks to see if you've filtered it to be anything but null - if you have, it returns your special value, if it's null, it continues on with the meta cache bucket check in get_metadata()

@ericmann
Copy link

Also, note the special conditions inside get_metadata around the return of $check. From core:

if ( null !== $check ) {
    if ( $single && is_array( $check ) )
        return $check[0];
    else
        return $check;
}

So rather than returning $value, we should be returning array( $value ) otherwise, since $value is an associative array, $check[0] will have issues ...

@JustinSainton
Copy link
Author

@ericmann - thanks for all the points of clarity. All of that is a bit secondary, as it doesn't really affect the output. Even just running the remove_filter() line and then return get_post_meta([...] - that returns null - which seems as if it's not removing the action properly.

@JustinSainton
Copy link
Author

Hmm, actually, I think your point about the associative array is probably the issue...

@JustinSainton
Copy link
Author

Freaking @ericmann. Works beautifully. Hugs are on the way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment