/** * This is the inverse of the showTo shortcode function above. */ public function hideFrom($attrs, $content = 'null') { $isAllowed = true; if (Cart66Common::isLoggedIn()) { $levels = Cart66Common::trimmedExplode(',', $attrs['level']); $account = new Cart66Account(); if (in_array('all_members', $levels)) { $isAllowed = false; } elseif ($account->load(Cart66Session::get('Cart66AccountId'))) { if ($account->isActive() && in_array($account->getFeatureLevel(), $levels)) { $isAllowed = false; } } } $content = $isAllowed ? $content : ''; return do_shortcode($content); }
public function getMembershipPrice() { // Return pricing (if applicable) for different membership levels // Otherwise, just return the default pricing (without options or fancy subscription stuff) $price = $this->price; if (Cart66Common::isLoggedIn()) { $levels = Cart66Common::trimmedExplode(',', $this->priceMembership); foreach ($levels as $level) { list($subscription, $p) = Cart66Common::trimmedExplode(':', $level); $membershipPriceList[$subscription] = $p; } $account = new Cart66Account(); if ($account->load(Cart66Session::get('Cart66AccountId'))) { $userFeatureLevel = $account->getFeatureLevel(); if ($account->isActive() && array_key_exists($userFeatureLevel, $membershipPriceList)) { $price = $membershipPriceList[$userFeatureLevel]; } } } return $price; }
/** * Hide private pages and pages that require a subscription feature level the subscriber does not have */ public function hidePrivatePages($excludes) { global $wpdb; $hidePrivate = true; $mySubItemNums = array(); $activeAccount = false; $featureLevel = false; if (Cart66Common::isLoggedIn()) { $hidePrivate = false; $account = new Cart66Account(Cart66Session::get('Cart66AccountId')); if ($account->isActive()) { $activeAccount = true; $featureLevel = $account->getFeatureLevel(); } // Optionally add the logout link to the end of the navigation if (Cart66Setting::getValue('auto_logout_link')) { add_filter('wp_list_pages', array($this, 'appendLogoutLink')); } // Hide guest only pages $guestOnlyPageIds = Cart66AccessManager::getGuestOnlyPageIds(); $excludes = array_merge($excludes, $guestOnlyPageIds); } // Hide pages requiring a feature level that the subscriber does not have $hiddenPages = Cart66AccessManager::hideSubscriptionPages($featureLevel, $activeAccount); if (count($hiddenPages)) { $excludes = array_merge($excludes, $hiddenPages); } if ($hidePrivate) { // Build list of private page ids $privatePageIds = Cart66AccessManager::getPrivatePageIds(); $excludes = array_merge($excludes, $privatePageIds); } // Merge private page ids with other excluded pages if (is_array(get_option('exclude_pages'))) { $excludes = array_merge(get_option('exclude_pages'), $excludes); } sort($excludes); return $excludes; }
public function getDiscountAmount($cartObject = null, $taxed_products = false) { $p = new Cart66Product(); //Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] Running getDiscountAmount()"); $discount = 0; if (!$cartObject) { $cartObject = Cart66Session::get('Cart66Cart'); } // First, check to see if a membership group is excluded from using this coupon if ($this->membership_eligibility) { Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] This coupon requires a membership to use:" . $this->membership_eligibility); if (Cart66Common::isLoggedIn()) { if ($this->membership_eligibility == "guest") { // The user is logged in as a non-guest and this is a guest-only coupon. Return 0.00 Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] The user is logged in as a non-guest and this is a guest-only coupon."); return number_format($discount, 2, '.', ''); } $account = new Cart66Account(); if ($account->load(Cart66Session::get('Cart66AccountId'))) { $userFeatureLevel = $account->getFeatureLevel(); $allowedLevels = Cart66Common::trimmedExplode(',', $this->membership_eligibility); Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] The user is logged in as " . $userFeatureLevel); if (!in_array($userFeatureLevel, $allowedLevels)) { // The user is logged in, but cannot use this coupon. Return 0.00 Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] The user is logged in as " . $userFeatureLevel . " and is not eligible to use this coupon."); return number_format($discount, 2, '.', ''); } } } else { // The user is not logged in...and this coupon requires a login to work, no discount if ($this->membership_eligibility && $this->membership_eligibility != "guest") { Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] The user is not logged in, cant use this coupon as guest."); return number_format($discount, 2, '.', ''); } } } if ($this->apply_to == "products" && !empty($cartObject)) { // coupon applies to products $products = explode(',', $this->products); $cartItems = $cartObject->getItems(); Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] The number of items in the cart: " . count($cartItems)); $usedThisOrder = 0; if (empty($this->products)) { // all products // apply coupon to every item in the cart $counter = 0; foreach ($cartItems as $item) { $p->load($item->getProductId()); if (!$taxed_products || $taxed_products && $p->taxable == 1) { $basePrice = $item->getBaseProductPrice(); $stayPositivePrice = $this->stayPositive($basePrice, $this->getAmount($basePrice)); $quantity = $item->getQuantity(); for ($i = 1; $i <= $quantity; $i++) { if (empty($this->max_uses_per_order)) { $discount += $stayPositivePrice; Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] Max uses per order is empty. Discount: {$discount}"); } elseif ($counter < $this->max_uses_per_order) { $discount += $stayPositivePrice; Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] Max uses per order is NOT empty. Discount: {$discount} :: Stay Positive: {$stayPositivePrice}"); } $counter++; } } } } else { // coupon applies to specific products foreach ($cartItems as $item) { $p->load($item->getProductId()); if (!$taxed_products || $taxed_products && $p->taxable == 1) { if ($this->exclude_from_products == 0) { if (in_array($item->getProductId(), $products)) { // add up discount $itemQuantity = $item->getQuantity(); if ($this->max_uses_per_order > 0) { $usesRemaining = $this->max_uses_per_order - $usedThisOrder; $allowedQuantity = $usesRemaining <= $itemQuantity ? $usesRemaining : $itemQuantity; } else { $allowedQuantity = $itemQuantity; } $productDiscount = $this->getAmount($item->getBaseProductPrice()); $discount += $allowedQuantity * $this->stayPositive($item->getBaseProductPrice(), $productDiscount); } } elseif ($this->exclude_from_products == 1) { if (!in_array($item->getProductId(), $products)) { // add up discount $itemQuantity = $item->getQuantity(); if ($this->max_uses_per_order > 0) { $usesRemaining = $this->max_uses_per_order - $usedThisOrder; $allowedQuantity = $usesRemaining <= $itemQuantity ? $usesRemaining : $itemQuantity; } else { $allowedQuantity = $itemQuantity; } $productDiscount = $this->getAmount($item->getBaseProductPrice()); $discount += $allowedQuantity * $this->stayPositive($item->getBaseProductPrice(), $productDiscount); } } } } } } if ($this->apply_to == "shipping") { $shipping = $cartObject->getShippingCost(); $discount = $shipping - $this->getAmount($shipping) < 0 ? $shipping : $this->getAmount($shipping); } if ($this->apply_to == "total") { $shipping = $cartObject->getShippingCost(); $products = $cartObject->getSubTotal(); $discount = $this->getAmount($shipping + $products, $shipping + $products); } if ($this->apply_to == "subtotal") { // coupon applies to subtotal $products = explode(',', $this->products); $cartItems = $cartObject->getItems(); if (empty($this->products)) { $products = $cartObject->getSubTotal($taxed_products); $discount = $this->getAmount($products, $products); } else { $itemSubtotal = 0; // coupon applies to specific products foreach ($cartItems as $item) { $p->load($item->getProductId()); if (!$taxed_products || $taxed_products && $p->taxable == 1) { if ($this->exclude_from_products == 0) { if (in_array($item->getProductId(), $products)) { // add up discount $itemSubtotal += $item->getProductPrice() * $item->getQuantity(); } } elseif ($this->exclude_from_products == 1) { if (!in_array($item->getProductId(), $products)) { // add up discount $itemSubtotal += $item->getProductPrice() * $item->getQuantity(); } } } } Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] item subtotal: {$itemSubtotal}"); $discount = $this->getAmount($itemSubtotal, $itemSubtotal); $discount = $this->stayPositive($itemSubtotal, $discount); } } // format $discount = number_format($discount, 2, '.', ''); return $discount; }