I am using jcart http://conceptlogic.com/jcart/ to create some cart functionality in wordpress.
I am trying to access a jcart function within another function in my functions.php file.
foreach($jcart->get_contents() as $item) {
$psitems[] = $item['id'];
}
This code works fine on functions.php itself or on any template page-- it creates the $psitems array so I can check if that array contains certain values.
Now, within the following function:
function gallery_shortcode_fancybox($attr) { ...
I have the following:
foreach($jcart->get_contents() as $item) {
$psitems[] = $item['id'];
}
foreach ( $attachments as $id => $attachment ) {
$link = isset($attr['link']) && 'file' == $attr['link'] ? wp_get_attachment_image($id, 'full', false, false) : wp_get_attachment_image($id, 'full', true, false);
$output .= "<div class='property'>";
$output .= "$link";
$output .= '
<div class="property-details">
<div class="property-details-inner">
<form method="post" action="" class="jcart">
<fieldset>
<input type="hidden" name="jcartToken" value="' . $_SESSION['jcartToken'] . '" />
<input type="hidden" name="my-item-id" value="' . $id . '" />
<input type="hidden" name="my-item-name" value="Photo #' . $id . '" />
<input type="hidden" name="my-item-url" value="' . wp_get_attachment_url( $id ) . '" />
<input type="hidden" name="my-item-qty" value="1" size="3" />';
if(in_array($id,$psitems)) {
$output .= '<span class="action terminal pull-sheet">Image in Pullsheet</span>'
} else {
$output .= '<input type="submit" name="my-add-button" value="Add to Pull Sheet" class="action terminal pull-sheet" />';
}
$output .= '</fieldset>
</form>
</div> <!-- property-details-inner -->
</div>';
$output .= "</div>";
}
return $output;
I get the following error:
Call to a member function get_contents() on a non-object
If I try placing the original foreach($jcart->get_contents() as $item) { ... code outside of the function, I get the following error:
Warning: in_array() expects parameter 2 to be array, null given in...
How can I access this array inside the function and avoid these errors?
Thanks!