Support » Plugins » Hacks » Shortcode for price converts to PayPal button

  • I have a price shortcode like this:
    [price sku="123EVR"]
    that I want to convert into a PaylPal add-to-cart button.

    The code in my functions.php file looks like this:

    // Add Shortcode
    function price_shortcode( $atts , $content = null ) {
    	$sku = $atts['sku'];
    	// Code
    		$p = $wpdb->get_row("SELECT prodPrice, prodName  FROM products WHERE SKU ='$sku' LIMIT 1");
    		return '<form target="paypal" action="" method="post">
        		<p style="TEXT-ALIGN: left">
        		<input type="hidden" name="cmd" value="_cart" />
       	 		<input type="hidden" name="business" value="" />
        		<input type="hidden" name="add" value="1" />
        		<input type="hidden" name="item_name" value="'.$p->prodName.'" />
        		<input type="hidden" name="item_number" value="'.$sku.'" />
        		<input type="hidden" name="amount" value="'.$p->prodPrice.'" />
        		<input type="hidden" name="no_shipping" value="2" />
        		<input type="hidden" name="currency_code" value="USD" />
        		<input type="image" border="0" name="submit" src="" alt="Make payments with PayPal - it\'s fast, free and secure!" /></p>
    add_shortcode('price', 'price_shortcode');

    It seems to error out on the DB query, probably from not getting a value for $sku.

    What am I missing here? This is not a full plugin, just a work-around for site I’m moving into WordPress.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz


    Everything looks good to me. The only thing I’m not sure about is using the $content parameter if you are not using the enclosing shortcode form, for example [price sku="123EVR"]The content[/price]

    You don’t seem to be using it, try removing the parameter. I wouldn’t think this would have made a difference, but its the only thing even slightly irregular that I can see.

    Thanks, bcworkz. I tried removing the $content=null bit, but it’s still not replacing the shortcode with a PayPal button. In fact, it appears that the shortcode is doing NOTHING at all. It appears on the public page as a shortcode.

    Moderator bcworkz


    The shortcode shows on the public page?! That’s a bit extreme. I did just notice one common, easy to miss error, but I wouldn’t have thought it would cause the shortcode to display.

    Anyway, you need to add global $wpdb; as the first line of your function definition. That should solve the $p having no value issue, and hopefully the raw shortcode will not show any more.

    BTW, if you had temporarily set WP_DEBUG to true in wp-config.php, this sort of error would have been flagged, making the solution apparent. FYI for next time 😉

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Shortcode for price converts to PayPal button’ is closed to new replies.