-
-
Save JustinSainton/5400735 to your computer and use it in GitHub Desktop.
<?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; | |
} |
Also, FWIW, checked the output of remove_filter() to ensure it was successfully removing the filter, and it is.
What is $check
? I'm not familiar with the get_post_metadata
filter.
Couple of things:
- The
has_filter()
check is unnecessary - If you're only running on
'_wpsc_product_metadata' === $meta_key
you should hard-code that in theget_post_meta
check. - The way you're trying to set
$value['no_shipping']
implies$single
will be set totrue
... I'd hard-code that as well for clarity - Since the priority is the default (10) there's no reason to specify that or number of args for
remove_filter
I would do a check after grabbing $value
to make sure it's an array. When $single
is set to true, and the meta key doesn't exist, you get back an empty string. Trying to set an array key on a string will err.
If the function is returning null
, that means the conditional is failing. There should be no way the return
inside the conditional would return null - the get_post_meta
check will return either the value or an empty string ...
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()
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 ...
@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.
Hmm, actually, I think your point about the associative array is probably the issue...
Freaking @ericmann. Works beautifully. Hugs are on the way.
The get_post_meta() call on Line 11 will also invoke this filter - it would be an infinite loop without removing the filter. The has_filter() call may indeed be unnecessary (added it after continued hair pulling), but removing it doesn't affect the output.