0

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!

3 Answers 3

1

@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\"]"); ?>
Sign up to request clarification or add additional context in comments.

3 Comments

@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.
Leaving reason in the edit comment is valid as per the instructions @jschoen
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 ``.
0

The right line should be:

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

3 Comments

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.
ahhh I want to return the id ..not echo it! Thank you for your help!
to return the ID, use $post->ID inside the loop
0

simpler correct answer:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.