Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Problem with do_shortcode() / Cart66 WordPress

  • SOLVED

Using Cart66, we created a catalog page to display thumbnails of products. We wanted to include the price using custom fields and the cart66 shortcode, but I've run into a problem. For some reason, when passing the custom field value through do_shortcode() we are not getting the correct value. In fact, it is returning the value from the first item in the loop. When we just output the custom field, it is returning the right value. I'm sure it is something with my code, so I have pasted it below:



$args = array(
'post_type' => 'product',
'post_parent' => 86,
'orderby' => 'menu_order',
'order' => 'ASC'
);

$query = new WP_Query( $args ); ?>

<div class="hentry">
<div class="entry-content">

<?php while($query->have_posts()) : $query->the_post(); ?>

<?php $my_meta = get_post_meta(get_the_ID(), '66_price', TRUE); ?>

<div class="img-wrap left">
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('grid-thumbnail');?></a>
<h3 class="prod-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<p><?php echo $my_meta ?></p>
<?php echo do_shortcode('[add_to_cart item="'.$my_meta.'" showprice="only"]'); ?>
</div>

<?php endwhile; wp_reset_postdata(); ?>

</div><!-- end .entry-content -->
</div><!-- end .hentry -->



Also, note the screen shot attached which shows that when the custom field is outputted as text it works fine, but when outputted through do_shortcode it returns $38 both times. The value should be $38 and $48. I am only outputting the first instance of $my_meta to show that it is correct when not passed through do_shortcode()

Answers (4)

2011-11-04

Romel Apuya answers:

have you tried resetting the post data after the function mens_custom_loop() ?

like this


<?php
/*
Template Name: Men's Products
*/
remove_action('genesis_loop', 'genesis_do_loop');
add_action('genesis_loop', 'mens_custom_loop');
function mens_custom_loop() {
$args = array(
'post_type' => 'product',
'post_parent' => 86,
'orderby' => 'menu_order',
'order' => 'ASC'
);
?>
<?php wp_reset_postdata(); ?>
$query = new WP_Query( $args ); ?>
<div class="hentry">
<div class="entry-content">
<?php while($query->have_posts()) : $query->the_post(); ?>
<?php $my_meta = get_post_meta(get_the_ID(), '66_price', TRUE); ?>
<div class="img-wrap left">
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('grid-thumbnail');?></a>
<h3 class="prod-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<p><?php echo $my_meta ?></p>
<?php echo do_shortcode('[add_to_cart item="'. $my_meta .'" showprice="only"]'); ?>
</div>
<?php endwhile; wp_reset_postdata(); ?>
</div><!-- end .entry-content -->
</div><!-- end .hentry -->
<script type="text/javascript">
(function($){
$(document).ready(function(){
var replace_str = $(".Cart66Price").html().replace("Price:","");
$(".Cart66Price").html(replace_str);
})
})(jQuery);
</script>
<?php }
genesis(); ?>


Rich Staats comments:

UPDATE: As it turns out, this also not the genesis custom loop causing the problem. The problem stems from that bit of jQuery that removes a bit of copy that Cart66 prepends to the price: "Price:" -- once I remove that it works in all cases.

So the question now is...why is that happening?


Romel Apuya comments:

(function($){
$(document).ready(function(){
var replace_str = $(".Cart66Price").html().replace("Price:","");
$(".Cart66Price").html(replace_str);
})
})(jQuery);


because it replaces the value of .Cart66Price to blank.
what you need to do is make it that the value of previous .Cart66Price is added to
the new value.

2011-11-02

Jens Filipsson answers:

Maybe you shouldn't reset the post data, and just keep it like this instead:

<?php endwhile; ?>


Rich Staats comments:

yeah i tried that but to no avail. What's weird is that it works when not passed through do_shortcode(). It wouldn't work in that instance if reset postdata was the problem. I think it must be something else


Jens Filipsson comments:

How about changing this line:

<?php $my_meta = get_post_meta(get_the_ID(), '66_price', TRUE); ?>

To this:

<?php $my_meta = get_post_meta($post->ID, '66_price', true); ?>


Rich Staats comments:

oddly enough nothing is returned when I use $post->ID.

BTW thanks for the help.


Jens Filipsson comments:

Going to bed soon, but I noticed you didn't close this one, maybe that would help?

Changing this:

<p><?php echo $my_meta ?></p>

To this:

<p><?php echo $my_meta; ?></p>


Rich Staats comments:

nope that creates a server error. thanks for all the suggestions though.

2011-11-03

Francisco Javier Carazo Gil answers:

I'm not sure that I understand it. You say that:
echo $my_meta

Give the correct value and:

echo do_shortcode('[add_to_cart item="'.$my_meta.'" showprice="only"]');

Presents the problem.

Is that correct?


Rich Staats comments:

yes
echo $my_meta
gives the correct value.

The following:
echo do_shortcode('[add_to_cart item="'.$my_meta.'" showprice="only"]');
does not.

But what makes this weird is that the previous finds the first instance of $my_meta and displays that for every queried post in the loop. So, from the screen shot you can see that the first item is $38 and the second is $38. It should be $38 for the first and $48 for the second. For whatever reason (an error in my code I'm sure) it reusing the value of the custom field from the first queried post.

But because it works without using do_shortcode() it makes me think something is wrong with that syntax but i cannot find it.


Francisco Javier Carazo Gil comments:

This is really stange.

Try the next:
$myarg = '[add_to_cart item="'.$my_meta.'" showprice="only"]';
echo do_shortcode($myarg);


The syntax seems to be correct.


Rich Staats comments:

so. as it turns out, the implementation is correct, but since I am running this through the Genesis Frameworks "custom loop" function the problem is occurring there. when i do a normal WP template it works fine. I will post back what I find out, but maybe someone here can explain why the Genesis Custom Loop is causing a problem, or maybe it's me using their custom loop incorrectly.

template page with Genesis Custom Loop that is causing the problem:

<?php
/*
Template Name: Men's Products

*/


remove_action('genesis_loop', 'genesis_do_loop');
add_action('genesis_loop', 'mens_custom_loop');
function mens_custom_loop() {


$args = array(
'post_type' => 'product',
'post_parent' => 86,
'orderby' => 'menu_order',
'order' => 'ASC'
);

$query = new WP_Query( $args ); ?>

<div class="hentry">
<div class="entry-content">

<?php while($query->have_posts()) : $query->the_post(); ?>

<?php $my_meta = get_post_meta(get_the_ID(), '66_price', TRUE); ?>

<div class="img-wrap left">
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('grid-thumbnail');?></a>
<h3 class="prod-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<p><?php echo $my_meta ?></p>
<?php echo do_shortcode('[add_to_cart item="'. $my_meta .'" showprice="only"]'); ?>
</div>

<?php endwhile; wp_reset_postdata(); ?>

</div><!-- end .entry-content -->
</div><!-- end .hentry -->

<script type="text/javascript">
(function($){
$(document).ready(function(){
var replace_str = $(".Cart66Price").html().replace("Price:","");
$(".Cart66Price").html(replace_str);
})
})(jQuery);
</script>

<?php }



genesis();



Normal WP template which displays the code correctly:

<?php
/*
Template Name: Men's Products

*/

get_header();

$args = array(
'post_type' => 'product',
'post_parent' => 86,
'orderby' => 'menu_order',
'order' => 'ASC'
);

$query = new WP_Query( $args ); ?>
<div id="content-sidebar-wrap">
<div id="content">
<div class="hentry">
<div class="entry-content">

<?php while($query->have_posts()) : $query->the_post(); ?>

<?php $my_meta = get_post_meta(get_the_ID(), '66_price', TRUE); ?>

<div class="img-wrap left">
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('grid-thumbnail');?></a>
<h3 class="prod-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<p><?php echo $my_meta ?></p>
<?php echo do_shortcode('[add_to_cart item="'. $my_meta .'" showprice="only"]'); ?>
</div>

<?php endwhile; wp_reset_postdata(); ?>

</div><!-- end .entry-content -->
</div><!-- end .hentry -->
</div><!-- end .content -->
<?php get_sidebar(); ?>
</div><!-- end #content-sidebar-wrap -->
<?php get_footer();


Francisco Javier Carazo Gil comments:

Rich,

It should be a Genesis Framework problem, it's not your error. Have you contacted with them to report the bug? http://www.studiopress.com/contact

I'm sure they will solve it quickly.

2011-11-04

Denzel Chia answers:

Hi,

I suspect get_the_ID() is returning the same id for all entries.

Try replacing the following


<?php $my_meta = get_post_meta(get_the_ID(), '66_price', TRUE); ?>


with this


<?php

global $post;
$post_id = $post->ID;
$my_meta = get_post_meta($post_id, '66_price',true);

?>


$post->ID works only if you declare global $post.

Hope this helps!

Thanks!
Denzel


Rich Staats comments:

UPDATE: As it turns out, this also not the genesis custom loop causing the problem. The problem stems from that bit of jQuery that removes a bit of copy that Cart66 prepends to the price: "Price:" -- once I remove that it works in all cases.

So the question now is...why is that happening?