AI Verified

Name

Add a Product to Cart on Cart Page

About

Add the Product ID to add a product/upsell to the cart.

Language

PHP

Rating

Voted: 0 by 0 user(s)

Codevault

WebSquadron

Scroll down to see more snippets from this codevault.

Wordpress Compatability

The author has indicated that this snippet is compatable up to wordpress version: 6.4

Our AI bot has checked this snippet is compatable up to wordpress version: 6.1

Code Snippet Plugin Sync

Free & Pro

Download this snippet by clicking the download button, then head over to the Code Snippet Plugin settings in your wordpress admin dashboard, select the import menu then upload this file to import into your wordpress site.

Pro Only (Coming Soon)

You will be able to click a button and sync this snippet to your wordpress site automatically and from your dashboard manage all code snippets across all your wordpress sites that have the Code Snippets Pro plugin installed.

History

Last modified:

03/03/2024

Important Note

This snippet has the following status:

AI Verified

This snippet has been tested by our AI bot, see any comments below.

AI Bot Comments:

Potential vulnerability found : Cross Site Scripting
Found on line : 26
Code : echo('<div id="cart-add"><br><a href="' . esc_url( $add_to_cart_url )
Vulnerable line : 3
Code : $add_to_cart_url = wc_get_cart_url() . '?add-to-cart=' . $product_id); // Construct the add-to-cart URL

Found 1 vulnerabilities

Add a Product to Cart on Cart Page

 
                    
1function cart_add() {
2 $product_id = 380; // Set the product ID you want to add to the cart
3 $add_to_cart_url = wc_get_cart_url() . '?add-to-cart=' . $product_id; // Construct the add-to-cart URL
4 
5 echo '<style>
6 #cart-add a {
7 display: block; /* Stretch to full width */
8 width: 100%; /* Full width */
9 background-color: #222222; /* Background color */
10 color: #FFFFFF; /* Font color */
11 text-align: center; /* Center the text */
12 padding: 10px 0; /* Add some padding */
13 font-size: 16px; /* Font size */
14 font-weight: 500; /* Font weight */
15 text-transform: uppercase; /* Uppercase text */
16 text-decoration: none; /* Remove underline from links */
17 border: none; /* Remove border */
18 cursor: pointer; /* Change cursor to pointer */
19 }
20 /* Optional: Add hover effect */
21 #cart-add a:hover {
22 background-color: #FF0050; /* Darker shade for hover */
23 }
24 </style>';
25 
26 // Change the href attribute to the $add_to_cart_url
27 echo '<div id="cart-add"><br><a href="' . esc_url( $add_to_cart_url ) . '">Add Product to Cart</a></div>';
28}
29add_action('woocommerce_after_cart_totals', 'cart_add', 10, 0);

0

Related Snippets

Please see some snippets below related to this snippet..

WooCommerce

AI Verified

0

WooCommerce Only Allow Number Input For Billing Phone

Added: 1 year ago

Last Updated: 1 year ago

This snippet adds jQuery (run form the footer for performance reasons) which only allows number input in the billing phone field on the checkout. Specifically, it will find and delete anything which i...

WooCommerce

AI Verified

0

WooCommerce Login Redirect

Added: 1 year ago

Last Updated: 1 year ago

By default, when a user logs into their account on your WooCommerce website, they are redirected to their dashboard on the My Account page. This guide shows you how to change this and redirect the use...

WooCommerce

AI Verified

0

WooCommerce Thank You Page Cross-Sells

Added: 1 year ago

Last Updated: 1 year ago

Depending on your website theme, WooCommerce by default will only show product cross-sells in the cart based on products added. These are user defined from the product editor under the Linked Products...

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

Performance

AI Verified

29

Remove Unused Javascript

Added: 1 year ago

Last Updated: 2 weeks ago

Remove Unused Javascript - and improve your Page Speed Insight Score

WooCommerce

Pro Verified

9

Deactivate some WooCommerce Checkout Fields

Added: 1 year ago

Last Updated: 5 months ago

Deactivate some WooCommerce Checkout Fields from showing

Performance

Pro Verified

5

Disable e-icons from Elementor

Added: 1 year ago

Last Updated: 1 year ago

Disable e-icons - and remove the notification from occurring during your Page Speed Insight Check.