Add Product to Cart Programmatically

This snippet demonstrates how to add a product to the cart programmatically, which can be useful for creating custom "Add to Cart" functionality.

PHP

function add_product_to_cart( $product_id, $quantity = 1 ) {
    WC()->cart->add_to_cart( $product_id, $quantity );
}
// Usage: add_product_to_cart( 123, 2 );

Snippet Feedback

Did this snippet work for you? Do you have any questions about this snippet? Leave some feedback below.

2 Views

Tags

Language

Did it work?

Save Snippet

Embed Snippet

To embed this snippet on your site, copy this html code and paste into your webpage. Learn more
By embedding snippets on your site, you are agreeing to our terms and conditions.

Embed Snippet

To embed this snippet on your site, copy this html code and paste into your webpage. Learn more
By embedding snippets on your site, you are agreeing to our terms and conditions.

Embed Snippet

To embed this snippet on your site, copy this html code and paste into your webpage. Learn more
By embedding snippets on your site, you are agreeing to our terms and conditions.