dimanche 1 mars 2015

Adding Item and Coupon Programmically via WooCommerce


Since WooCommerce updated to 2.3, my previous function is not working for adding a coupon and an item, if an item already exists in the cart.



function add_product_to_cart() {
if (!is_admin()) {
global $woocommerce;
$items = $woocommerce->cart->get_cart();
$live_product_ids = array(10305, 10302, 10300, 10303, 10301);
$live_upsell_id = 10311;
$suff_found = false;
$upsell_found = false;
$coupon_code = 'nutreincesufficiencyquiznakedcalories';
//check if nut suff is already in cart
if (sizeof($items) > 0) {
foreach ($items as $cart_item_key => $values) {
$_product = $values['data'];
if (in_array($_product->id, $live_product_ids))
$suff_found = true;
if ($_product->id == $live_upsell_id)
$upsell_found = true;
}
// if upsell product not found, add it
if ($suff_found) {
if (!$upsell_found)
$woocommerce->cart->add_to_cart($live_upsell_id);
if (!$woocommerce->cart->has_discount($coupon_code))
$woocommerce->cart->add_discount($coupon_code);
}
}
}
}
add_action('wp_loaded', 'add_product_to_cart');


Nothing has changed on my end, there is nothing in my error log. If I print_r $items it returns an empty array.





Aucun commentaire:

Enregistrer un commentaire