mirror of
https://gh.wpcy.net/https://github.com/mainwp/Code-Snippets-Functions.git
synced 2026-05-01 11:52:25 +08:00
21 lines
643 B
Text
21 lines
643 B
Text
add_action( 'template_redirect', 'add_product_to_cart' );
|
|
function add_product_to_cart() {
|
|
if ( ! is_admin() ) {
|
|
$product_id = 64; //replace with your own product id
|
|
$found = false;
|
|
//check if product already in cart
|
|
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
|
|
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
|
|
$_product = $values['data'];
|
|
if ( $_product->get_id() == $product_id )
|
|
$found = true;
|
|
}
|
|
// if product not found, add it
|
|
if ( ! $found )
|
|
WC()->cart->add_to_cart( $product_id );
|
|
} else {
|
|
// if no products in cart, add it
|
|
WC()->cart->add_to_cart( $product_id );
|
|
}
|
|
}
|
|
}
|