• Resolved gianni333

    (@gianni333)


    I’m trying to make a button which I can add to the product template in woocommerce as a “quick buy” button (in addition to the standard “add to cart” button), that adds the current product to the cart and redirects the customer to the checkout page. I’m using this maxbutton shortcode, but somehow it won’t get the product id.

    [maxbutton id=”9″ url=”https://thecreativecrops.com/checkout/?add-to-cart=.$product_id”%5D

    Did I make a mistake in the shortcode?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Bas Schuiling

    (@basszje)

    Hi,

    Yes that won’t work that way. If I’m correct you are editing a PHP file right?

    <?php echo do_shortcode('[maxbutton id="9" url="https://thecreativecrops.com/checkout/?add-to-cart=' . $product_id . '"]'); ?>

    Also note that you can use the name attribute for buttons, instead of the id ( like [maxbutton name=”buy-now”] ) . In this way if you ever want to change the button (to another one) you can change the name in the button editor without having to edit your templates.

    Thread Starter gianni333

    (@gianni333)

    I’ve been trying to implement the function described here (https://www.presstigers.com/how-to-add-a-quick-buy-link-in-woocommerce-without-a-plugin/) with a maxbutton.

    Thanks for the tip with the name attribute I’m already using this feature on other maxbuttons, will definitely use it on this one too.

    Plugin Author Bas Schuiling

    (@basszje)

    I see. The part described there is just the URL you need.

    I would solve it like this:

    
    <?php 
    $url =  home_url('/checkout/?add-to-cart='.$product_id);
    echo do_shortcode('[maxbutton name="buy-button" url="' . $url . '"]'); ?>
    
    

    Keeps everything readable.

    Thread Starter gianni333

    (@gianni333)

    Cool thanks for the help! Much appreciated!

    To make it possible to add the button into the products template I tried to make a shortcode out of it by adding this code snipped to my template via the code snippet plugin:

    function buy_now_button_shortcode() {
    $url = home_url(‘/checkout/?add-to-cart=’.$product_id);
    echo do_shortcode(‘[maxbutton id=”9″ url=”‘ .$url . ‘”]’);
    }
    add_shortcode(‘buynowbutton’, ‘buy_now_button_shortcode’);

    It seems to work, the button is there and it’s linking to the cart but it does not add the product to the cart, I think it doesnt get the current product id.

    Did I make a mistake with the code?

    Plugin Author Bas Schuiling

    (@basszje)

    Yes, add_shortcode is a widly different function and not needed.

    You need to put the code where the button should be in the template. If you want to make some central function, pass the $product_id to the function, otherwise it will be out of scope.

    Your code should give enough errors to think again ?? . I’d recommend to put your development site on full error output and use a plugin like Query Monitor to make life a lot easier.

    Thread Starter gianni333

    (@gianni333)

    Thanks for the indetail answer, I’m new to coding but I think I just learned some new basics about it thanks to you. Thanks for that. I’ll probably be best of using the code snippet plugin to safely add this code:) Since I can not choose where the code is added I’ll have to do what you suggested and pass the $product_id to the function.

    Thread Starter gianni333

    (@gianni333)

    Quick update: Since I designed my product template with the Anywhere Elementor plugin (and since I’m not too much into php and didnt want to mess up the site by editing the php files directly^^), I couldn’t manage to place the button inside the anywhere elementor product template in another way than by using as the shortcode widget, so I tried again to make the shortcode work. The result, which seems to work now is:

    function buy_now_button_shortcode() {

    global $product;
    $id = $product->get_id();
    $url = home_url(‘/checkout/?add-to-cart=’.$id);
    echo do_shortcode(‘[maxbutton id=”9″ url=”‘ .$url . ‘”]’);
    }
    add_shortcode(‘buynowbutton’, ‘buy_now_button_shortcode’);

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Trying to make a dynamic button link’ is closed to new replies.