K
K
Konstantin Timosheno2020-11-25 00:23:58
WordPress
Konstantin Timosheno, 2020-11-25 00:23:58

Add extra "Add to cart" Woocommerce button?

Hello. It would seem that the hackneyed, but the situation is a bit different.

You need to add another button that will redirect to the main one, while adding the selected product to the cart. A redirect to the Checkout Button was made through the hook.

There are additional fields (hidden) implemented with WooCommerce TM Extra Product Options that are included in the order.
When I add via a link like this /?add_to_cart=61, then additional fields do not get into the order and cart.
Tried through other hooks, but the behavior remains the same as on a regular button.
I've been googling for the second day and I'm struggling to solve this issue.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Timosheno, 2020-11-30
@kastembay

I decided! Made a hidden field near the buttons

<div class="button-submit" style="text-align:right;">
      <input type="text" name="custom_value" value="" hidden id="custom">
      <button type="submit"
              name="add-to-cart"
              value="<?php echo esc_attr( $product->get_id() ); ?>"
              onclick="jQuery('#custom').val(555)"
              class="button alt single_add_to_cart_button">Заказать еще</button>
      <button type="submit" name="add-to-cart"
              value="<?php echo esc_attr( $product->get_id() ); ?>"
              class="single_add_to_cart_button button alt">Оформить заказ</button>
    </div>

and in functions.php added action
add_filter( 'woocommerce_add_to_cart_redirect', 'stamp_skip_cart' );

function stamp_skip_cart( $redirect ) {

  $cart_items = WC()->cart->get_cart();

  foreach ($cart_items as $cart_item ) {
    if ($cart_item['tmpost_data']['custom_value'] == 555){
      return get_home_url();
    }
    else{
      return wc_get_checkout_url();

    }
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question