Payment buttons in mini cart
-
I’m sure this isn’t as easy as it sounds, but was wondering if you had any thoughts on how difficult this would be to implement?
-
It really just depends on what mini cart plug-in you are using and if they provide actions that can be hooked in to to expose the Braintree payment buttons.
As a generalization it wouldn’t be that difficult because our plug-in already provides all the abstracted code necessary to render the payment buttons.
Kind regards,
Thanks for the response! I’m pretty excited about using the Gateway. After a full day or looking around its got everything I need except built in mini cart features.
I was able to get this to work, but since I’m pretty new at all of this the only way I can get the scripts to load is by editing the code itself. Can you help me out with how to load the cart scripts without modifying the code? As of right now what I can see is that an is_cart() check in the abstract payment gateway class that loads them but I can’t see how to hook into this.
Thanks!
K
Hi @kminderhout,
You don’t want to edit plugin code directly because the next time you update the plugin the code will be overridden. Our API docs are a very helpful resource and I think reviewing them will help with how our code is structured.
What you want to do, is in your mini-cart there should be some actions that get triggered. Hook into the mini-cart action and do something like the following:
function render_braintree_mini_cart_gateways(){ wc_braintree_cart_checkout_template(); } add_action('mini_cart_action', 'render_braintree_mini_cart_gateways');
The mini_cart_action is a placeholder. You will need to find the appropriate action in the mini cart plugin.
The
wc_braintree_cart_checkout_template
function loops through all the Braintree gateways that support cart checkout and then calls theircart_fields
method.Kind Regards,
Thanks again for the reply! I appreciate the time. That’s the part that I’d figured out. But even with the template without the javascript being loaded I wasn’t seeing any buttons.
I’ve opted for extending the specific classes I want in a plugin that then modifies the
enqueue_frontend_scripts
method to provide the cart scripts on all pages./** * * @param WC_Braintree_Frontend_Scripts $scripts */ public function enqueue_frontend_scripts($scripts) { global $wp; if (is_checkout () && ! is_order_received_page ()) { $this->enqueue_checkout_scripts ( $scripts ); } if (is_add_payment_method_page () && ! isset ( $wp->query_vars[ 'payment-methods' ] )) { $this->enqueue_add_payment_method_scripts ( $scripts ); } if (is_cart ()) { $this->enqueue_cart_scripts ( $scripts ); } if (is_product ()) { $this->enqueue_product_scripts ( $scripts ); } if (wc_braintree_subscriptions_active () && wcs_braintree_is_change_payment_method_request ()) { $this->enqueue_checkout_scripts ( $scripts ); } $this->enqueue_cart_scripts ( $scripts ); }
For right now I’ve added
$this->enqueue_cart_scripts ( $scripts );
to every page, but I’ll get that customized as I get more organized.On top of that I have to make sure the scripts are rerending the buttons after a fragment refresh.
It took me a while to understand it all only because I’m learning, but I’m really impressed and appreciative of how you’ve written everything to so extendable.
Hi @kminderhout,
The reason you aren’t seeing the buttons when you just use the template is because the
is_cart
function is returning false in theenqueue_frontend_scripts
method. In the next version of the plugin, I am going to call the enqueue method in each field rendering method. Here is an example:Today:
/** * Output fields required for cart checkout. */ public function cart_fields() { $this->enqueue_frontend_scripts ( braintree ()->frontend_scripts ); wc_braintree_nonce_field ( $this ); wc_braintree_device_data_field ( $this ); wc_braintree_hidden_field ( $this->id . '_tokenized_response' ); wc_braintree_get_template ( 'cart/' . $this->template, array( 'gateway' => $this ) ); }
Next Version:
/** * Output fields required for cart checkout. */ public function cart_fields() { $this->enqueue_cart_scripts( braintree ()->frontend_scripts ); wc_braintree_nonce_field ( $this ); wc_braintree_device_data_field ( $this ); wc_braintree_hidden_field ( $this->id . '_tokenized_response' ); wc_braintree_get_template ( 'cart/' . $this->template, array( 'gateway' => $this ) ); }
Notice how in the next version, the
cart_fields
method will call theenqueue_cart_scripts
. So just copy and paste this code I provided and directly edit the plugin. In the next version you won’t have to change anything since when you upgrade the code will be the same.Kind Regards,
- The topic ‘Payment buttons in mini cart’ is closed to new replies.