Prepare cart with products
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class studiowp_add_products_to_cart { | |
private $products = array(); | |
private $id = ''; | |
public function __construct( $id = 1, $products = array() ) { | |
$this->products = $products; | |
$this->id = $id; | |
add_action( 'init', array( $this, 'init' ) ); | |
} | |
public function init() { | |
if ( isset( $_GET['studiowp_add_to_cart'] ) && $_GET['studiowp_add_to_cart'] == $this->id ) { | |
$cart = WC()->cart; | |
$cart->empty_cart(); | |
foreach ( $this->products as $product_id ) { | |
$cart->add_to_cart( $product_id, 1 ); | |
} | |
wp_safe_redirect( wc_get_cart_url() ); | |
exit; | |
} | |
} | |
} | |
new studiowp_add_products_to_cart( | |
$id = 'my_products', | |
$products = array( 35, 31, 31 ) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment