opencart规格积分无法增加的解决方案

需要的动作:
1:用户加入购物车,有其他的规格,增加其他规格的积分的显示
controller/checkout/cart.php里的index控制器里修改:

$jifennum=0; //重新定义积分 = 0 ;
foreach ($product[‘option’] as $option) {
if ($option[‘type’] != ‘file’) {
$value = $option[‘value’];
} else {
$upload_info = $this->model_tool_upload->getUploadByCode($option[‘value’]);

if ($upload_info) {
$value = $upload_info[‘name’];
} else {
$value = ”;
}
}

$option_data[] = array(
‘name’ => $option[‘name’],
‘value’ => (utf8_strlen($value) > 20 ? utf8_substr($value, 0, 20) . ‘..’ : $value)
);
//新增判断语句,判断积分的增减
if (!empty($option[‘points_prefix’]) && !empty($option[‘points’])){
if ($option[‘points_prefix’]==’+’){
$jifennum = ($jifennum+$option[‘points’]);
} else {
$jifennum = ($jifennum-$option[‘points’]);
}
}
//判断结束
}
$jifennum = ($jifennum*$product_total); //购买的数量,同时增加积分的总值

$jifen=($product[‘reward’]+$jifennum);
#然后,$data[‘products’][]多维数组重新修改显示的值:

// Display prices
if (($this->config->get(‘config_customer_price’) && $this->customer->isLogged()) || !$this->config->get(‘config_customer_price’)) {
$price = $this->currency->format($this->tax->calculate($product[‘price’], $product[‘tax_class_id’], $this->config->get(‘config_tax’)));
} else {
$price = false;
}

// Display prices
if (($this->config->get(‘config_customer_price’) && $this->customer->isLogged()) || !$this->config->get(‘config_customer_price’)) {
$total = $this->currency->format($this->tax->calculate($product[‘price’], $product[‘tax_class_id’], $this->config->get(‘config_tax’)) * $product[‘quantity’]);
} else {
$total = false;
}

$recurring = ”;

if ($product[‘recurring’]) {
$frequencies = array(
‘day’ => $this->language->get(‘text_day’),
‘week’ => $this->language->get(‘text_week’),
‘semi_month’ => $this->language->get(‘text_semi_month’),
‘month’ => $this->language->get(‘text_month’),
‘year’ => $this->language->get(‘text_year’),
);

if ($product[‘recurring’][‘trial’]) {
$recurring = sprintf($this->language->get(‘text_trial_description’), $this->currency->format($this->tax->calculate($product[‘recurring’][‘trial_price’] * $product[‘quantity’], $product[‘tax_class_id’], $this->config->get(‘config_tax’))), $product[‘recurring’][‘trial_cycle’], $frequencies[$product[‘recurring’][‘trial_frequency’]], $product[‘recurring’][‘trial_duration’]) . ‘ ‘;
}

if ($product[‘recurring’][‘duration’]) {
$recurring .= sprintf($this->language->get(‘text_payment_description’), $this->currency->format($this->tax->calculate($product[‘recurring’][‘price’] * $product[‘quantity’], $product[‘tax_class_id’], $this->config->get(‘config_tax’))), $product[‘recurring’][‘cycle’], $frequencies[$product[‘recurring’][‘frequency’]], $product[‘recurring’][‘duration’]);
} else {
$recurring .= sprintf($this->language->get(‘text_payment_cancel’), $this->currency->format($this->tax->calculate($product[‘recurring’][‘price’] * $product[‘quantity’], $product[‘tax_class_id’], $this->config->get(‘config_tax’))), $product[‘recurring’][‘cycle’], $frequencies[$product[‘recurring’][‘frequency’]], $product[‘recurring’][‘duration’]);
}
}

$data[‘products’][] = array(
‘key’ => $product[‘key’],
‘thumb’ => $image,
‘name’ => $product[‘name’],
‘model’ => $product[‘model’],
‘option’ => $option_data,
‘recurring’ => $recurring,
‘quantity’ => $product[‘quantity’],
‘stock’ => $product[‘stock’] ? true : !(!$this->config->get(‘config_stock_checkout’) || $this->config->get(‘config_stock_warning’)),
//’reward’ => ($product[‘reward’] ? sprintf($this->language->get(‘text_points’), $product[‘reward’]) : ”),
‘reward’ => ($product[‘reward’] ? sprintf($this->language->get(‘text_points’), $jifen) : ”),
‘price’ => $price,
‘total’ => $total,
‘href’ => $this->url->link(‘product/product’, ‘product_id=’ . $product[‘product_id’])
);

这样,用户在购物车看到的赠送积分的规则就变了

然后是后台管理员

2: 后台管理员对订单的操作,首先是修改添加的积分值
sale/order/info方法内

$data[‘reward’] = $order_info[‘reward’]; //获取赠送积分,自然已经自动区分了不同的会员等级

然后该方法内还有个foreach ($products as $product) 循环

$product_num = $product[‘quantity’]; //获取产品的数量
$jifen = 0; //订单规格的积分增加
foreach ($options as $option) {
//嵌入开发规格积分
$guige = $this->model_sale_order->getOrderOptionsData($option[‘product_option_id’],$product[‘product_id’],$option[‘product_option_value_id’]);
if (!empty($guige[‘points_prefix’]) && !empty($guige[‘points’])){

if ($guige[‘points_prefix’]==’+’){
$jifen = ($jifen+($guige[‘points’]*$product_num)); //因为是按数量来计算的
} else {
$jifen = ($jifen-($guige[‘points’]*$product_num));
}
}
if ($option[‘type’] != ‘file’) {
$option_data[] = array(
‘name’ => $option[‘name’],
‘value’ => $option[‘value’],
‘type’ => $option[‘type’]
);
} else {
$upload_info = $this->model_tool_upload->getUploadByCode($option[‘value’]);

if ($upload_info) {
$option_data[] = array(
‘name’ => $option[‘name’],
‘value’ => $upload_info[‘name’],
‘type’ => $option[‘type’],
‘href’ => $this->url->link(‘tool/upload/download’, ‘token=’ . $this->session->data[‘token’] . ‘&code=’ . $upload_info[‘code’], ‘SSL’)
);
}
}
}
$data[‘reward’] =($data[‘reward’]+$jifen); //把对应的规格积分进行相加

在这里引用了一个model
方法原型为:
public function getOrderOptionsData($product_option_id,$product_id,$value,$quantity){
$option_query = $this->db->query(“SELECT po.product_option_id, po.option_id, od.name, o.type FROM ” . DB_PREFIX . “product_option po
LEFT JOIN `” . DB_PREFIX . “option` o ON (po.option_id = o.option_id)
LEFT JOIN ” . DB_PREFIX . “option_description od ON (o.option_id = od.option_id)
WHERE po.product_option_id = ‘” . (int)$product_option_id . “‘
AND po.product_id = ‘” . (int)$product_id . “‘”);
$option_data=array();
if ($option_query->num_rows) {
if ($option_query->row[‘type’] == ‘select’ || $option_query->row[‘type’] == ‘radio’ || $option_query->row[‘type’] == ‘image’) {
$option_value_query = $this->db->query(“SELECT pov.option_value_id, ovd.name, pov.quantity, pov.subtract, pov.price, pov.price_prefix, pov.points, pov.points_prefix, pov.weight, pov.weight_prefix FROM ” . DB_PREFIX . “product_option_value pov LEFT JOIN ” . DB_PREFIX . “option_value ov ON (pov.option_value_id = ov.option_value_id) LEFT JOIN ” . DB_PREFIX . “option_value_description ovd ON (ov.option_value_id = ovd.option_value_id) WHERE pov.product_option_value_id = ‘” . (int)$value . “‘ AND pov.product_option_id = ‘” . (int)$product_option_id . “‘ AND ovd.language_id = ‘” . (int)$this->config->get(‘config_language_id’) . “‘”);

if ($option_value_query->num_rows) {
if ($option_value_query->row[‘price_prefix’] == ‘+’) {
$option_price += $option_value_query->row[‘price’];
} elseif ($option_value_query->row[‘price_prefix’] == ‘-‘) {
$option_price -= $option_value_query->row[‘price’];
}

if ($option_value_query->row[‘points_prefix’] == ‘+’) {
$option_points += $option_value_query->row[‘points’];
} elseif ($option_value_query->row[‘points_prefix’] == ‘-‘) {
$option_points -= $option_value_query->row[‘points’];
}

if ($option_value_query->row[‘weight_prefix’] == ‘+’) {
$option_weight += $option_value_query->row[‘weight’];
} elseif ($option_value_query->row[‘weight_prefix’] == ‘-‘) {
$option_weight -= $option_value_query->row[‘weight’];
}

if ($option_value_query->row[‘subtract’] && (!$option_value_query->row[‘quantity’] || ($option_value_query->row[‘quantity’] < $quantity))) {
$stock = false;
}

$option_data= array(
‘product_option_id’ => $product_option_id,
‘product_option_value_id’ => $value,
‘option_id’ => $option_query->row[‘option_id’],
‘option_value_id’ => $option_value_query->row[‘option_value_id’],
‘name’ => $option_query->row[‘name’],
‘value’ => $option_value_query->row[‘name’],
‘type’ => $option_query->row[‘type’],
‘quantity’ => $option_value_query->row[‘quantity’],
‘subtract’ => $option_value_query->row[‘subtract’],
‘price’ => $option_value_query->row[‘price’],
‘price_prefix’ => $option_value_query->row[‘price_prefix’],
‘points’ => $option_value_query->row[‘points’],
‘points_prefix’ => $option_value_query->row[‘points_prefix’],
‘weight’ => $option_value_query->row[‘weight’],
‘weight_prefix’ => $option_value_query->row[‘weight_prefix’]
);
}
} elseif ($option_query->row[‘type’] == ‘checkbox’ && is_array($value)) {
foreach ($value as $product_option_value_id) {
$option_value_query = $this->db->query(“SELECT pov.option_value_id, ovd.name, pov.quantity, pov.subtract, pov.price, pov.price_prefix, pov.points, pov.points_prefix, pov.weight, pov.weight_prefix FROM ” . DB_PREFIX . “product_option_value pov LEFT JOIN ” . DB_PREFIX . “option_value ov ON (pov.option_value_id = ov.option_value_id) LEFT JOIN ” . DB_PREFIX . “option_value_description ovd ON (ov.option_value_id = ovd.option_value_id) WHERE pov.product_option_value_id = ‘” . (int)$product_option_value_id . “‘ AND pov.product_option_id = ‘” . (int)$product_option_id . “‘ AND ovd.language_id = ‘” . (int)$this->config->get(‘config_language_id’) . “‘”);

if ($option_value_query->num_rows) {
if ($option_value_query->row[‘price_prefix’] == ‘+’) {
$option_price += $option_value_query->row[‘price’];
} elseif ($option_value_query->row[‘price_prefix’] == ‘-‘) {
$option_price -= $option_value_query->row[‘price’];
}

if ($option_value_query->row[‘points_prefix’] == ‘+’) {
$option_points += $option_value_query->row[‘points’];
} elseif ($option_value_query->row[‘points_prefix’] == ‘-‘) {
$option_points -= $option_value_query->row[‘points’];
}

if ($option_value_query->row[‘weight_prefix’] == ‘+’) {
$option_weight += $option_value_query->row[‘weight’];
} elseif ($option_value_query->row[‘weight_prefix’] == ‘-‘) {
$option_weight -= $option_value_query->row[‘weight’];
}

if ($option_value_query->row[‘subtract’] && (!$option_value_query->row[‘quantity’] || ($option_value_query->row[‘quantity’] < $quantity))) {
$stock = false;
}

$option_data= array(
‘product_option_id’ => $product_option_id,
‘product_option_value_id’ => $product_option_value_id,
‘option_id’ => $option_query->row[‘option_id’],
‘option_value_id’ => $option_value_query->row[‘option_value_id’],
‘name’ => $option_query->row[‘name’],
‘value’ => $option_value_query->row[‘name’],
‘type’ => $option_query->row[‘type’],
‘quantity’ => $option_value_query->row[‘quantity’],
‘subtract’ => $option_value_query->row[‘subtract’],
‘price’ => $option_value_query->row[‘price’],
‘price_prefix’ => $option_value_query->row[‘price_prefix’],
‘points’ => $option_value_query->row[‘points’],
‘points_prefix’ => $option_value_query->row[‘points_prefix’],
‘weight’ => $option_value_query->row[‘weight’],
‘weight_prefix’ => $option_value_query->row[‘weight_prefix’]
);
}
}
} elseif ($option_query->row[‘type’] == ‘text’ || $option_query->row[‘type’] == ‘textarea’ || $option_query->row[‘type’] == ‘file’ || $option_query->row[‘type’] == ‘date’ || $option_query->row[‘type’] == ‘datetime’ || $option_query->row[‘type’] == ‘time’) {
$option_data= array(
‘product_option_id’ => $product_option_id,
‘product_option_value_id’ => ”,
‘option_id’ => $option_query->row[‘option_id’],
‘option_value_id’ => ”,
‘name’ => $option_query->row[‘name’],
‘value’ => $value,
‘type’ => $option_query->row[‘type’],
‘quantity’ => ”,
‘subtract’ => ”,
‘price’ => ”,
‘price_prefix’ => ”,
‘points’ => ”,
‘points_prefix’ => ”,
‘weight’ => ”,
‘weight_prefix’ => ”
);
}
}
return $option_data;
}
最后是对积分进行增加和减少的操作
sale/order/addReward()添加积分和removeReward()移除积分

我们来从写addReward()
public function addReward() {
$this->load->language(‘sale/order’);

$json = array();

if (!$this->user->hasPermission(‘modify’, ‘sale/order’)) {
$json[‘error’] = $this->language->get(‘error_permission’);
$json[‘error’]= ‘后台API错误4’;
} else {
if (isset($this->request->get[‘order_id’])) {
$order_id = $this->request->get[‘order_id’];
} else {
$order_id = 0;
}

$this->load->model(‘sale/order’);

$order_info = $this->model_sale_order->getOrder($order_id);
//嵌入开发添加积分开始
$data[‘products’] = array(); //订单产品集
$products = $this->model_sale_order->getOrderProducts($order_id); //获取订单的所有产品
foreach ($products as $product) {
$option_data = array(); //产品规格
$options = $this->model_sale_order->getOrderOptions($order_id, $product[‘order_product_id’]); //获取规格
$product_num = $product[‘quantity’]; //获取产品的数量
$jifen = 0; //订单规格的积分增加
foreach ($options as $option) {
$guige = $this->model_sale_order->getOrderOptionsData($option[‘product_option_id’],$product[‘product_id’],$option[‘product_option_value_id’],$product_num);
if (!empty($guige[‘points_prefix’]) && !empty($guige[‘points’])){
if ($guige[‘points_prefix’]==’+’){
$jifen = ($jifen+($guige[‘points’]*$product_num)); //因为是按数量来计算的
} else {
$jifen = ($jifen-($guige[‘points’]*$product_num));
}
}
}
}
$reward =($order_info[‘reward’]+$jifen); //把对应的规格积分进行相加
//嵌入开发添加积分结束
if ($order_info && $order_info[‘customer_id’] && ($reward > 0)) {
$this->load->model(‘sale/customer’);

$reward_total = $this->model_sale_customer->getTotalCustomerRewardsByOrderId($order_id);

if (!$reward_total) {
//$this->model_sale_customer->addReward($order_info[‘customer_id’], $this->language->get(‘text_order_id’) . ‘ #’ . $order_id, $order_info[‘reward’], $order_id);
$this->model_sale_customer->addReward($order_info[‘customer_id’], $this->language->get(‘text_order_id’) . ‘ #’ . $order_id, $reward, $order_id);
}
}
$json[‘success’] = $this->language->get(‘text_reward_added’);
}
$this->response->addHeader(‘Content-Type: application/json’);
$this->response->setOutput(json_encode($json));
}
removeReward()的话,是根据订单的ID来实现直接删除对应ID,这个不需要修改,但为了避免员工在后台进行到底积分赠送,用户购买之后又把积分移除从新生成积分的刷分过程
直接关闭移除功能