WordPress.org

Ready to get started?Download WordPress

Forums

Shortcode for price converts to PayPal button (4 posts)

  1. sigmaweb
    Member
    Posted 1 year ago #

    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="https://www.paypal.com/cgi-bin/webscr" method="post">
        		<p style="TEXT-ALIGN: left">
        		<input type="hidden" name="cmd" value="_cart" />
       	 		<input type="hidden" name="business" value="bob@bob.com" />
        		<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="https://www.paypal.com/en_US/i/btn/x-click-but22.gif" alt="Make payments with PayPal - it\'s fast, free and secure!" /></p>
                </form>';
    	}
    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.

  2. bcworkz
    Member
    Posted 1 year ago #

    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.

  3. sigmaweb
    Member
    Posted 1 year ago #

    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.

  4. bcworkz
    Member
    Posted 1 year ago #

    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 ;)

Topic Closed

This topic has been closed to new replies.

About this Topic