I want to add this "add to cart" shortcode using a custom field. how do i get it to display? Have tried filters but to no use.
Shortcode to add:
[add_to_cart item="7818-9" showprice="no" quantity="user:1"]
I've tried with the short code filter and this, to no success:
<?php echo apply_filters( 'the_content', '[add_to_cart item="7818-9" showprice="no" quantity="user:1"]'); ?>
<strong>Updated!</strong>
Ok, so I hope everybody gets this reply. I am using the Cart66 ecommerce plugin, so the shortcode exists. I just need to make it print, using a custom field. Of course I can add the shortcode in the_content(); but due to design this is not an option.
1. I do not want to register a new short code, it exists via the plugin
2. I want it to print outside the_content(); using a custom field
Julio Potier answers:
Hello
Does the "<strong>add_to_cart</strong>" shortcode already exists or you want us to create/handle it ?
1) If it exists add this to your <em>functions.php</em> from theme folder :
function my_shortcode_in_content( $content )
{
global $post;
$button = get_post_meta( $post->ID, 'add_to_my_cart', true);
return $content . $button;
}
add_filter( 'the_content', 'my_shortcode_in_content' );
Then in you post, add a custom meta field named "<strong>add_to_my_cart</strong>", now you can paste this in the field : "<strong>[add_to_cart item="7818-9" showprice="no" quantity="user:1"]</strong>"
Logically, the content will be shortcode_parsed to output the shortcode button html code.
2) Is the shortcode does not exists, which plugin do you use as ecommerce stuff ? Can you give me a correct URL where i can see this button ?
Thank you
Julio Potier comments:
Also you can add this button via shortcode directly in your single.php template using :
<?php
echo do_shortcode( get_post_meta( get_the_ID(), 'add_to_my_cart', true) );
?>
Under the "the_content()".
LeTune comments:
Ok, so I hope everybody gets this reply. I am using the Cart66 ecommerce plugin, so the shortcode exists. I just need to make it print, using a custom field. Of course I can add the shortcode in the_content(); but due to design this is not an option.
1. I do not want to register a new short code, it exists via the plugin
2. I want it to print outside the_content(); using a custom field
Julio Potier comments:
1. Ok great
2. No problem, with my code (1) i think this is good, or as i said, directly in your theme template, single.php file.
If you want, i can do this for you ;) Just PM me an admin access or/and FTP account. You can trust us, here on WPQ ;)
<em>ps : i'm also Web Security Consultant</em>
LeTune comments:
this is the actual short code:
[add_to_cart item="7818-9" showprice="no" quantity="user:1" ]
i want to add it using the custom field "download", the code looks like this right now:
<div class="take-look">
<a href="<?php echo get_post_meta($post->ID, 'download', true); ?>">DOWNLOAD THIS ITEM </a>
</div>
LeTune comments:
pm you with login details
Julio Potier comments:
Ok, can you come on skype (julio.boiteaweb) or gtalk ([email protected]) ? Because i'm editing the theme with cache disabled and i can not se my modifications...
Julio Potier comments:
Here comes my solution (job done)
Directly in the template :
<?php // OLD WAY DISPLAYING THE DOWNLOAD LINK - [email protected]
/*
if((get_post_meta($post->ID, "download", true))) { ?>
<div class="take-look"><a href="http://premiumpsd.com/thankyou/index.php?file=<?php echo get_post_meta($post->ID, 'download', true); ?>" onClick="_gaq.push(['_trackEvent', 'Download', 'Download', '<?php echo get_post_meta($post->ID, 'download', true); ?>.zip']);">DOWNLOAD THIS ITEM (FREE)</a></div>
<?php
}
*/ // NEW WAY TO DISPLAY ADD TO CART - [email protected]
if( $button = get_post_meta($post->ID, "download", true) ) {
echo do_shortcode( $button );
}
?>
If you need to display both, you have to create a new field named "download1" for example. Then, decomment the old code.
See you and thank you !
Milan Petrovic answers:
To add a shortcode, you need a plugin to handle it. What you have installed that can work with add_to_cart shortcode?
Arnav Joy answers:
define a custom field
say
add_to_cart
then give its value as
[add_to_cart item="7818-9" showprice="no" quantity="user:1"]
at front end
show it as
$add_to_card = get_post_meta($post->ID,'add_to_cart',true);
echo do_shortcode($add_to_cart);
Arnav Joy comments:
i think you should explain your question as we are not getting what do you want actually?
Arnav Joy comments:
so try my code add a custom filed in post at admin panel ,
if you do not know how to add then use this
http://codex.wordpress.org/Custom_Fields
Arnav Joy comments:
if you want to get it done by me then send me a pm with ftp details and db details also the link of front end and backend
also mention here that you have send me a pm.
Thanks
Hai Bui answers:
You should use do_shortcode to do that.
<?php echo do_shortcode('[add_to_cart item="7818-9" showprice="no" quantity="user:1"]') ?>
But I'm not sure what you want. Do you want to add the shortcode into the custom field?
Kannan C answers:
if you want to use it with content filter,
add_filter( 'the_content', 'my_shortcode_filter');
function my_shortcode_filter( $content ) {
if ( is_single() ) //restrict for single pages
$shortcode_content = do_shortcode('[add_to_cart item="7818-9" showprice="no" quantity="user:1"]');
// Returns the content.
return $shortcode_content.$content;
}
Updated to prevent the overwriting the default content.