Пример #1
0
 /**
  * Returns boolean true if user can review product
  * otherwise, returns string error message  
  * 
  * @param \Users\Models\Users $user
  * @param \Shop\Models\Products $product
  * @return string|boolean
  */
 public static function canUserReview(\Users\Models\Users $user, \Shop\Models\Products $product)
 {
     if (empty($user->id)) {
         return false;
     }
     $settings = \Shop\Models\Settings::fetch();
     switch ($settings->{'reviews.eligibile'}) {
         case "identified":
             // has the user already reviewed it?
             $has_reviewed = static::collection()->count(array('product_id' => $product->id, 'user_id' => $user->id));
             if (!$has_reviewed) {
                 return true;
             }
             break;
         case "purchasers":
         default:
             // has the user purchased this item?
             if (\Shop\Models\Customers::hasUserPurchasedProduct($user, $product)) {
                 // has the user already reviewed it?
                 $has_reviewed = static::collection()->count(array('product_id' => $product->id, 'user_id' => $user->id));
                 if (!$has_reviewed) {
                     return true;
                 }
             }
             break;
     }
     return false;
 }
Пример #2
0
 public function forSelection()
 {
     $field = $this->input->get('value', '_id', 'default');
     $term = $this->input->get('q', null, 'default');
     $key = new \MongoRegex('/' . $term . '/i');
     $where = array();
     $where[] = array('username' => $key);
     $where[] = array('email' => $key);
     $where[] = array('first_name' => $key);
     $where[] = array('last_name' => $key);
     $results = \Shop\Models\Customers::forSelection(array('$or' => $where), $field);
     $response = new \stdClass();
     $response->more = false;
     $response->term = $term;
     $response->results = $results;
     return $this->outputJson($response);
 }
Пример #3
0
 public function productReviews()
 {
     $user = $this->getIdentity();
     $model = new \Shop\Models\Orders();
     $state = $model->populateState()->getState();
     $is_reviewed = null;
     if (strlen($state->get('filter.is_reviewed'))) {
         if ($state->get('filter.is_reviewed')) {
             $is_reviewed = true;
         } else {
             $is_reviewed = false;
         }
     }
     try {
         $paginated = \Shop\Models\Customers::purchasedProducts($user, array('offset' => $state->get('list.offset'), 'keyword' => $state->get('filter.keyword'), 'is_reviewed' => $is_reviewed));
     } catch (\Exception $e) {
     }
     $this->app->set('meta.title', 'My Reviews');
     $this->app->set('paginated', $paginated);
     $this->app->set('state', $state);
     echo $this->theme->render('Shop/Site/Views::account/product_reviews.php');
 }
Пример #4
0
                                dataType: 'json',
                                data: function (term, page) {
                                    return {
                                        q: term
                                    };
                                },
                                results: function (data, page) {
                                    return {results: data.results};
                                }
                            }
                            <?php 
if ($state->get('filter.user')) {
    ?>
                            , initSelection : function (element, callback) {
                                var data = <?php 
    echo json_encode(\Shop\Models\Customers::forSelection(array('_id' => new \MongoId($state->get('filter.user')))));
    ?>
;
                                callback(data);            
                            }
                            <?php 
}
?>
                        
                        });
                    
                    });
                    </script>
                                    
                    <div class="row">
                        <div class="col-md-2">
Пример #5
0
 /**
  * Determines whether or not a user qualifies for this campaign
  * 
  * @param \Users\Models\Users $user
  * @throws \Exception
  * @return \Shop\Models\Campaigns
  */
 public function customerQualifies(\Shop\Models\Customers $customer)
 {
     // Set $this->__is_validated = true if YES, user qualifies for this campaign.
     // throw an Exception if NO, user does not qualify.
     /**
      * is the campaign published?
      */
     if (!$this->published()) {
         throw new \Exception('This campaign is not valid for today');
     }
     $period_start = null;
     $period_end = null;
     switch ($this->period_type) {
         case "variable":
             $period_start = date('Y-m-d', strtotime('today -' . (int) $this->variable_period_days . ' days'));
             $period_end = date('Y-m-d', strtotime('tomorrow'));
             break;
         case "fixed":
             $period_start = $this->fixed_period_start;
             $period_end = $this->fixed_period_end;
             break;
         default:
             throw new \Exception('Invalid period type');
             break;
     }
     // has the minimum spend amount for the qualification period been met?
     if (!empty($this->rule_min_spent)) {
         // Get the total amount spent by the customer during the qualification period
         $total = $customer->fetchTotalSpent($period_start, $period_end);
         if ($total < $this->rule_min_spent) {
             throw new \Exception('Customer has not spent enough during the qualification period');
         }
     }
     /**
      * evaluate shopper groups against $this->groups
      */
     if (!empty($this->groups)) {
         $groups = array();
         if (empty($customer->id)) {
             // Get the default group
             $group_id = \Shop\Models\Settings::fetch()->{'users.default_group'};
             if (!empty($group_id)) {
                 $groups[] = (new \Users\Models\Groups())->setState('filter.id', (string) $group_id)->getItem();
             }
         } elseif (!empty($customer->id)) {
             $groups = $customer->groups();
         }
         $group_ids = array();
         foreach ($groups as $group) {
             $group_ids[] = (string) $group->id;
         }
         switch ($this->groups_method) {
             case "none":
                 $intersection = array_intersect($this->groups, $group_ids);
                 if (!empty($intersection)) {
                     // TODO Chagne the error messages!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                     throw new \Exception('You do not qualify for this campaign.');
                 }
                 break;
             case "all":
                 // $missing_groups == the ones from $this->groups that are NOT in $group_ids
                 $missing_groups = array_diff($this->groups, $group_ids);
                 if (!empty($missing_groups)) {
                     throw new \Exception('You do not qualify for this campaign.');
                 }
                 break;
             case "one":
             default:
                 $intersection = array_intersect($this->groups, $group_ids);
                 if (empty($intersection)) {
                     throw new \Exception('You do not qualify for this campaign.');
                 }
                 break;
         }
     }
     /**
      * if we made it this far, the user qualifies
      */
     $this->__is_validated = true;
     return $this;
 }
Пример #6
0
 /**
  * User-aware price of the product,
  * accounting for user group, date, specials, etc.
  * Defaults to the product's default price.
  * 
  * @param \Users\Models\Users $user
  * @return unknown
  */
 public function price($variant_id = null, \Users\Models\Users $user = null)
 {
     $price = $this->get('prices.default');
     if (empty($user)) {
         $identity = \Dsc\System::instance()->get('auth')->getIdentity();
         if (!empty($identity->id)) {
             $user = $identity;
         }
     }
     $settings = \Shop\Models\Settings::fetch();
     // find the first user group that has special price override in settings and use it
     $settings_price_override = null;
     $settings_override = false;
     if (!empty($user)) {
         foreach ($user->groups() as $group) {
             $settings_override = $settings->exists('special_group_default_prices.' . $group['slug']);
             if ($settings_override) {
                 // found it, so get the correct number and leave
                 $type_price = 'regular';
                 if (!empty($this->{'prices.list'})) {
                     $type_price = 'sale';
                 }
                 $settings_price_override = 'special_group_default_prices.' . $group['slug'] . '.' . $type_price;
                 break;
             }
         }
     }
     // Get the product price for the user's primary group
     // primaryGroup defaults to the site-wide default user group
     $primaryGroup = \Shop\Models\Customers::primaryGroup($user);
     if ($group_slug = $primaryGroup->{'slug'}) {
         if (!$settings_override && $this->exists('prices.' . $group_slug)) {
             $price = $this->get('prices.' . $group_slug);
         }
     }
     if (!empty($variant_id) && $this->{'policies.variant_pricing.enabled'} && ($variant = $this->variant($variant_id))) {
         $price = $variant['price'];
     }
     // adjust price based on date ranges too
     $using_special_price = false;
     $now = strtotime('now');
     $today = date('Y-m-d', $now);
     foreach ((array) $this->{'prices.special'} as $special_price) {
         if (empty($special_price['group_id']) || $special_price['group_id'] == (string) $primaryGroup->id) {
             if (!empty($special_price['start']['time']) && $special_price['start']['time'] <= $now && (empty($special_price['end']['time']) || $special_price['end']['time'] > $now)) {
                 $price = $special_price['price'];
                 $using_special_price = true;
                 break;
             }
         }
     }
     // don't discount special prices
     if (!$using_special_price && $settings_override) {
         $ratio = 1.0 - (double) $settings->{$settings_price_override} / 100.0;
         $price = $price * $ratio;
     }
     return $price;
 }