Answer the question
In order to leave comments, you need to log in
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
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 questionAsk a Question
731 491 924 answers to any question