D
D
Dmitry2019-06-02 19:40:01
WordPress
Dmitry, 2019-06-02 19:40:01

How to give a customer a direct link to buy via WooCommerce?

I have such a task. There is a product in WooCommerce. I need to give such a link to this product to the client so that he gets directly to the checkout page for this product.
How can this be implemented, even with a crutch method? (for example, I give a link to a product, and from it a redirect to the checkout page for this product)
Thanks for the ideas in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor Vorotnev, 2019-06-02
@liggth

Rough:

function prefix_automatically_add_to_cart()
{
  // Сначала решите как будете передавать ID товара - через 
  // GET в ссылке, через кастомный rewrite, POST, куки и тд.
  // Далее, здесь получаете этот ID, в зависимости от решения выше.
  $product_id = 1;

  if ( WC()->cart->get_cart_contents_count() === 0 ) {
    // Програмно добавляем товар в корзину
    WC()->cart->add_to_cart( $product_id );

    // Переадресовываем на корзину (или чекаут)
    wp_redirect( WC()->cart->get_cart_url() );
    // wp_redirect( WC()->cart->get_checkout_url() );
    
    die;
  }
}
add_action( 'template_redirect', 'prefix_automatically_add_to_cart' );

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question