• Resolved Teratoma

    (@teratoma)


    I’m still pretty noobish when it comes to this stuff but I can’t tell what’s wrong with my code. I’ll try to explain what I’m doing. I put some code in a widget that pulled the_title into the ‘PRODUCT-NAME’ section of the ‘add to cart’ button, and charge $10 for the item. I was pleased with myself. Then I got really crazy and decided I could use get_post_custom_values for the ‘ITEM-PRICE’ code the same way. So I added a custom field called ‘price’, added prices to a couple of items, then tested the button. It displayed this in the sidebar:

    Warning: number_format() expects parameter 1 to be double, string given in XXXXXXXXXX/wordpress-simple-paypal-shopping-cart/wp_shopping_cart.php on line 321

    The following is my code:

    <?php
    $productid = get_the_title();
    $price = get_post_custom_values('Price');
    echo print_wp_cart_button_for_product("$productid", "$price"); ?>

    I’m doing something silly aren’t I? Please tell me I’m close!

    This is one of the posts: https://pins.kevinbyrd.co/vc033008/

    The post titles are the product IDs. Right now everything’s ten bucks anyway so that’s just a text string currently, but I’d like to be able to use that variable.

    https://www.ads-software.com/extend/plugins/wordpress-simple-paypal-shopping-cart/

Viewing 15 replies - 1 through 15 (of 15 total)
  • I had to change:
    <?php echo print_wp_cart_button_for_product("$productid", "$price"); ?>
    to:
    <?php echo print_wp_cart_button_for_product("$productid", "$price[0]"); ?>

    and it worked great.

    Thread Starter Teratoma

    (@teratoma)

    Excellent! Now I just set the prices as I post – or in bulk using any number of plugins – and never have to worry about the button again.

    Thanks for the help!

    Just stumbled across this post – it’s exactly what I’m trying to do: tie a couple of custom fields into the add to cart button.

    I have very little experience with these things, but so far I’ve used the Advanced Custom Fields plugin to generate price and product description.

    I love the way you are getting the product title from the post title, but could really use some help with adding the price bit please.

    I can get the price with acf by using:'<?php the_field(‘price’); ?>’
    But I need help adapting your code to use this??

    Thanks

    Well this sort of works, but it just gets the first number in the price
    eg. 32.00 shows as 3 in the checkout?

    ‘<?php
    $productid = get_the_title();
    $price = get_field(‘price’);
    echo print_wp_cart_button_for_product(“$productid”, “$price”); ?>’

    Thread Starter Teratoma

    (@teratoma)

    Hi speedyp! I’m glad you like the idea. I created a plugin that adds this functionality to a couple of widgets.

    You can see it in action here:

    https://pins.KevinByrd.co/boards/sales

    And download it here:

    https://KevinByrd.co/wp

    I was planning on ironing out the bugs (the shortcodes perform poorly but the widgets work great!) before releasing it, but I figured I’d share it. Let me know if you get any use out of it!

    Cheers!

    seems there’s a possible malware attack on that url..for me anyway!

    but, just wanted to thank the posters for this info, can’t get the price dynamically yet but just getting the title is saving my client a load of time.. thanks!

    Thread Starter Teratoma

    (@teratoma)

    Malware? On my site, or on the Adfly link? I can assure you that my site is safe – a typical WordPress install. If you mean Adfly, I think you should be pretty safe as long as you avoid ALL links on the page except for the ‘skip ad’ in the upper right.

    That said, I’ve been using Github a lot lately so here’s a 100% safe source with no ads or other bs: https://github.com/Teratoma/teratomic-paypal-button

    What issues are you having with the price? The readme assumes you know how to set custom fields, is that the problem?

    I’ll try it, yes I have the fields, at one point I was using a custom field for the title, it was working!.. but I couldn’t get my custom field for the price to output. I tried all of the above and then some. In the process I lost the ability to use the custom field for the item title. Now I keep getting the

    Warning: number_format() expects parameter 1 to be double, string given in XXXXXXXXXX/wordpress-simple-paypal-shopping-cart/wp_shopping_cart.php on line 321

    error.

    I’m checking out different posts and I see there are different ways of calling the post fields and different params for the product settings, none of it is working, I’m getting confused..

    I hope the plugin helps,thanks!

    arrg, my fields are in the attachment post type. is their a way to get it to add the field there instead of the post?

    otherwise, if anyone has a currently working example of using custom fields for the title and price values, I had it for the title, now I can’t remember how I did it and none of the examples are working.

    @teratoma
    the malware wasn’t on your site, but the download link.
    https://adf.ly/BxBWP

    okay, got it working for the product id again,

    <?php
    $productid =  get_post_meta($post->ID,'size_format_1',true);
    $price = '200';
    echo print_wp_cart_button_for_product("$productid", "$price"); ?>

    price is still hardcoded, still seeking a dynamic solution for that..

    FIXED!
    the solution was the error message of course, the numeric param in the field had to be like this(for 200 dollars)

    200,00

    also had to remove[0] from “$price”

    working code:

    <?php
    $productid =  get_post_meta($post->ID,'size_format_1',true); 
    
    $price = get_post_meta($post->ID,'price_1',true); 
    
    $shipping ='15';
    
    echo print_wp_cart_button_for_product("$productid", "$price", "$shipping"); ?>

    shipping is still not dynamic but I’ll settle for now.

    thanks again to the thread authors!

    Shipping is now working too!
    I just used the same get_post_meta but with the custom field value I made for shipping.

    Seems the fields must be “required” in order for this to work, not sure if that’s only because I’m using the “advanced custom fields” plugin to get my fields.. it has a “required” option for the value to be filled, and it must be set as true.

    This may be why I was getting that error, I selected “required” as true for the fields when I changed my numeric values to the “00,00” format, so I have to test to see which was causing the error, maybe both!

    actually, it looks as though it could be neither. I think all I had to do was update the post after adding the default values in “advanced customfields”.. I assumed they would apply since they are default, but looks like the post doesn’t know it has them untill it’s updated.

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘[Plugin: WordPress Simple Paypal Shopping Cart] Custom code question:’ is closed to new replies.