Advertisement
Guest User

Untitled

a guest
Mar 1st, 2015
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.17 KB | None | 0 0
  1. function add_product_to_cart() {
  2.     if (!is_admin()) {
  3.         global $woocommerce;
  4.         $items = $woocommerce->cart->get_cart();
  5.         $live_product_ids = array(10305, 10302, 10300, 10303, 10301);
  6.         $live_upsell_id = 10311;
  7.         $suff_found = false;
  8.         $upsell_found = false;
  9.         $coupon_code = 'nutreincesufficiencyquiznakedcalories';
  10.         //check if nut suff is already in cart
  11.         if (sizeof($items) > 0) {
  12.             foreach ($items as $cart_item_key => $values) {
  13.                 $_product = $values['data'];
  14.                 if (in_array($_product->id, $live_product_ids))
  15.                     $suff_found = true;
  16.                 if ($_product->id == $live_upsell_id)
  17.                     $upsell_found = true;
  18.             }
  19.             // if upsell product not found, add it
  20.             if ($suff_found) {
  21.                 if (!$upsell_found)
  22.                     $woocommerce->cart->add_to_cart($live_upsell_id);
  23.                 if (!$woocommerce->cart->has_discount($coupon_code))
  24.                     $woocommerce->cart->add_discount($coupon_code);
  25.             }
  26.         }
  27.     }
  28. }
  29. add_action('wp_loaded', 'add_product_to_cart');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement