Buy It Now / Add To Cart - same form

I am creating a custom theme / plugin for Marketpress. I need to have a choice of Buy It Now / Add To Cart the same way eBay does (see attached).

Result: redirected me to the cart when I:
* tried just using two buttons with two different names / values.
* tried to different 'form' tags with the same action etc...
Result: Added to the cart via Ajax when I :
* created an onSubmit function captured which button was pressed and dynamically changed the action variable on the form to: http://compumatter.biz/store/cart/ and http://compumatter.biz/store/shopping-cart/shipping

Upon closer observation I see that when submitted, admin-ajax.php is called with action=mp_update_cart so that is superseding any coding I put in place.

I've tried everything and anything and can't get those 2 different buttons to do the following:

Buy Now - add the item to the cart and go the cart directly (or even to shipping)
Add To Cart - add the item to the cart via ajax and go nowhere (which is default behavior it seems)

What sayeth the group.

Jay
CompuMatter

  • Rupok

    Hi Jay

    Hope you had a wonderful day.

    Upon closer observation I see that when submitted, admin-ajax.php is called with action=mp_update_cart so that is superseding any coding I put in place.

    Our plugin developer can give you best idea regarding this. So I'm flagging our developer here. Though our developers are busy, still I believe, he will come up with a suggestion very soon.

    Have a nice day. Cheers!
    Rupok

  • Rupok

    Hi Jay

    Hope you had a wonderful day.

    Our developer was working on this. As you said "tried just using two buttons with two different names / values", so I assume you already have got two buttons showing already. It would be best for us if you could tell us in details how you did that would be awesome and would help us to determine next required step.

    He gave me code to paste in a MU Plugin. For creating a mu plugin, create a folder named "mu-plugins" inside your "wp-content" folder. Then inside that "mu-plugins" folder, create a file with any name and with a .php extension. Now paste the code inside that file and save.

    function my_theme_mp_buy_button_script() {
        ?>
        <script>
        jQuery(document).ready(function($) {
            $( '#mp-products, .mp-multiple-products' ).on( 'mp_cart/after_add_item', '.mp_form-buy-product', function( e, resp, item, qty ) {
                    var $btn = $(document.activeElement);
                     if( $btn.is('.mp_button-buynow') ){
                        // buy now button - redirect to cart
                        window.location.href = $( this ).attr( 'action' );
                     }
            })
        });
        </script>;
        <?php
    }
    add_action( 'wp_footer', 'my_theme_mp_buy_button_script', 999);

    For getting more idea on mu plugins, you can go through this article: https://premium.wpmudev.org/manuals/wpmu-manual-2/using-mu-plugins/

    And if it doesn't work, please let us know. Our developer is working on this seriously and he said he will give us full code to add both buttons in the native way.

    I'm looking forward to hear from you and resolve this issue as soon as possible.

    Have a nice day. Cheers!
    Rupok

  • Jay

    Hello Rupok, Thanks for your reply.

    OK I have followed the instructions but nothing new happens

    This video shows the result: https://youtu.be/fYJW-L42gP4

    Here you can see these things
    * In firebug console we see 'got here 0' which confirms for us the mu-plugin code got loaded.
    * We see the shopping cart in the top right corner started empty.
    * Upon pressing the buy it now button we see the cart did get increased. However, it did not redirect us as hoped for.
    * We also notice that got here 1 and got here 2 were never shown in the console. This proof that the code did not fire

    I have also attached two images:
    * The firebug image shows the code used on the buttons when they are rendered.
    * The mu-plugin file shows the console.log code which has been inserted so we can debug this

    I hope this helps. I look forward to your suggestion.

    Jay
    CompuMatter

  • Kasia Swiderska

    Hello Jay,

    Thanks for additional information - I'm passing them for developer.
    Could you confirm one thing for me? Are you developing this code for old MarketPress version - not the 3.X? I can see that you have installed old version on multisite, but I cant access your site from your original question so I want to be sure we are providing code for right MarketPress.

    kind regards,
    Kasia

  • Jay

    Hello. NO this is not an old version. It is version 3.0.0.7 as this attached photo shows. I am curious why you believe it is an old version.

    It is important to note that through code I have set the shopping cart to the top bar. That is our development on the theme. The floating window will be used at some point but for now we are using the bar.

    So, back to the problem at hand now that you know we are using the up to date version...

    Your thoughts about the Buy It Now button?

    I am sure you must understand the need for this solution. The choices between buying a product now, or adding to a cart are standard in most carts. If they buy it now, they should be transported to shipping. If they add to cart they remain where they are.

    Your thoughts ?

    Jay
    CompuMatter

  • Rupok

    Hi Jay

    Hope you had a wonderful day.

    I've talked with our developer regarding this and he gave me another code block which is a complete solution. I mean, this should add both buttons and both will be functional. You have to create a MU Plugin for using this. For creating a mu plugin, create a folder named "mu-plugins" inside your "wp-content" folder. Then inside that "mu-plugins" folder, create a file with any name and with a .php extension. Now paste the code inside that file and save.

    <?php
    add_filter( 'mp_buy_button_tag', 'my_theme_mp_buy_button_tag', 10, 5 );
    function my_theme_mp_buy_button_tag( $button, $id, $context, $selected_atts, $no_single ){
    
            $product = new MP_Product( $id );
            $mp_buy_button = false;
    
            $button = '';
            if ( $product->get_meta( 'product_type' ) == 'external' && ( $url = $product->get_meta( 'external_url' ) ) ) {
                $button = '<a class="mp_link-buynow" href="' . esc_url( $url ) . '">' . __( 'Buy Now &raquo;', 'mp' ) . '</a>';
            } elseif ( ! mp_get_setting( 'disable_cart' ) ) {
                $button = '<form id="mp-buy-product-' . $product->ID . '-form" class="mp_form mp_form-buy-product ' . ( $no_single ? 'mp_no_single' : '' ) . ' ' . ( $mp_buy_button ? 'mp_buy_button' : '' ) . '" method="post" data-ajax-url="' . mp_get_ajax_url( 'admin-ajax.php?action=mp_update_cart' ) . '" action="' . mp_cart_link( false, true ) . '">';
    
                if ( ! $product->in_stock() ) {
                    $button .= '<span class="mp_no_stock">' . __( 'Out of Stock', 'mp' ) . '</span>';
                } else {
                    $button .= '<input type="hidden" name="product_id" value="' . $product->ID . '">';
    
                    if ( $context == 'list' ) {
                        if ( $product->has_variations() ) {
                            $button .= '<a class="mp_button mp_link-buynow mp_button-has_variations" data-href="' . admin_url( 'admin-ajax.php?action=mp_product_get_variations_lightbox&product_id=' . $product->ID ) . '" href="' . $product->url( false ) . '">' . __( 'Choose Options', 'mp' ) . '</a>';
                        } else {
                            $button .= '<button class="mp_button mp_button-addcart" type="submit" name="addcart">' . __( 'Add To Cart', 'mp' ) . '</button>';
                            $button .= '<button class="mp_button mp_button-buynow" type="submit" name="buynow">' . __( 'Buy Now', 'mp' ) . '</button>';
                        }
                    } else {
                        $button .= $product->attribute_fields( false, $selected_atts );
                            $button .= '<button class="mp_button mp_button-addcart" type="submit" name="addcart">' . __( 'Add To Cart', 'mp' ) . '</button>';
                            $button .= '<button class="mp_button mp_button-buynow" type="submit" name="buynow">' . __( 'Buy Now', 'mp' ) . '</button>';
                    }
                }
    
                $button .= '</form><!-- end mp-buy-product-form -->';
            }
            return $button;
    }
    
    function my_theme_mp_buy_button_script() {
        ?>
        <script>
        jQuery(document).ready(function($) {
            $( '#mp-products, .mp-multiple-products' ).on( 'mp_cart/after_add_item', '.mp_form-buy-product', function( e, resp, item, qty ) {
                    var $btn = $(document.activeElement);
                     if( $btn.is('.mp_button-buynow') ){
                        // buy now button - redirect to cart
                        window.location.href = $( this ).attr( 'action' );
                     }
            })
        });
        </script>;
        <?php
    }
    add_action( 'wp_footer', 'my_theme_mp_buy_button_script', 999);

    For getting more idea on mu plugins, you can go through this article: https://premium.wpmudev.org/manuals/wpmu-manual-2/using-mu-plugins/

    Can you please try this and confirm?

    Have a nice day. Cheers!
    Rupok

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.