I am building a wordpress ecommerce template with Cart66 plugin. Using a Cart66 shortcode inside a php template page, I would like to generate the Post ID inside the shortcode. Can someone please help and tell me if this is possible. Here is the code that I am using.

<?php echo do_shortcode("[add_to_cart item=\". the_ID() .\" quantity=\"user:1\"]"); ?>

This code will lay inside loop-single.php and above

Thank you!

share|improve this question
feedback

3 Answers

The right line should be:

<?php echo do_shortcode("[add_to_cart item=\"". the_ID() ."\" quantity=\"user:1\"]"); ?>
share|improve this answer
Thank you! Can you help me with another issue? It seems like that worked but it is now showing the ID in the html webpage vs putting it in the id for the shortcode. – user1227678 Apr 23 at 5:43
ahhh I want to return the id ..not echo it! Thank you for your help! – user1227678 Apr 23 at 5:52
to return the ID, use $post->ID inside the loop – ariefbayu Apr 23 at 7:07
feedback

@silent almost had it, but it should be get_the_ID() and not the_ID() since the later one echos it out, so try:

<?php echo do_shortcode("[add_to_cart item=\"". get_the_ID() ."\" quantity=\"user:1\"]"); ?>
share|improve this answer
@Lenin you should not edit code, feel free to leave a comment with what you think could be better, or to post your own answer explaining why it is better. – jschoen 3 hours ago
feedback

simpler correct answer:

<?php echo do_shortcode('[add_to_cart item="'. get_the_ID() .'" quantity="user:1"]');?>
share|improve this answer
@jschoen I do not have direct comment priviledge. I edited with single quote which not only is simpler but also in PHP it doesn't parse for variables. And within single quote, double quotes can be used. So double quotes do not need to be escaped with ``. – Lenin 2 hours ago
1  
@jschoen also, I edited the post when I had the priviledge. I gave an explanation while editing. Isn't that good enough? – Lenin 2 hours ago
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

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