Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // add item to cart on visit
- add_action( 'template_redirect', 'add_product_to_cart' );
- function add_product_to_cart() {
- if ( ! is_admin() ) {
- $product_ids = [385, 386];
- //check if product already in cart
- if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
- foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
- unset( $product_ids[array_search( $_product->id, $product_ids )] );
- }
- }
- if ( count($product_ids) > 0 ) {
- foreach($product_ids as $i => $$product_id) {
- WC()->cart->add_to_cart( $product_id );
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement