|
<?php |
|
/* |
|
* Изменить логику скидки и ID TV / ID Resource под свои нужды |
|
*/ |
|
switch ($modx->event->name) { |
|
case 'msOnBeforeAddToOrder': |
|
if ($key == 'koupon_code' && !empty($value)) { |
|
$check = false; |
|
|
|
$tvr = $modx->getObject('modTemplateVarResource', array( |
|
'tmplvarid' => 80, |
|
'contentid' => 1 |
|
)); |
|
if ($tvr) { |
|
$value_tv = $tvr->get('value'); |
|
} |
|
else{ |
|
$modx->event->output('Неверный код купона!'); |
|
return; |
|
} |
|
if(stristr($value_tv, $value) === FALSE) { |
|
$check = false; |
|
} |
|
else{ |
|
$tv_array = json_decode($value_tv, true); |
|
foreach($tv_array as $tv_val){ |
|
if($tv_val['code'] == $value && !$tv_val['active']){ |
|
$check = true; |
|
break; |
|
} |
|
} |
|
} |
|
if ($check !== true) { |
|
$modx->event->output('Неверный код купона!'); |
|
} |
|
} |
|
break; |
|
case 'msOnGetOrderCost': |
|
if (!empty($with_cart) && !empty($cost)) { |
|
if ($data = $order->get()) { |
|
if (!empty($data['koupon_code'])) { |
|
$tvr = $modx->getObject('modTemplateVarResource', array( |
|
'tmplvarid' => 80, |
|
'contentid' => 1 |
|
)); |
|
if ($tvr) { |
|
$value_tv = $tvr->get('value'); |
|
if(stristr($value_tv, $data['koupon_code']) === FALSE) { |
|
$check = false; |
|
} |
|
else{ |
|
$tv_array = json_decode($value_tv, true); |
|
$discount = 0; |
|
$cost_all = $cost_full = 0; |
|
foreach($tv_array as $tv_val){ |
|
if($tv_val['code'] == $data['koupon_code'] && !$tv_val['active']){ |
|
$check = true; |
|
$discount = $tv_val['percent']; |
|
|
|
$cart_goods = $cart->get(); |
|
foreach($cart_goods as $cart_good){ |
|
if($good = $modx->getObject('msProduct', $cart_good['id'])){ |
|
if($good->get('price') < $good->get('old_price')){ |
|
$cost_full = $cost_full + $cart_good['price'] * $cart_good['count']; |
|
continue; |
|
} |
|
$cost_all = $cost_all + $cart_good['price'] * $cart_good['count']; |
|
} |
|
} |
|
break; |
|
} |
|
} |
|
|
|
if($cost_all){ |
|
$newcost = $cost_all - $discount; |
|
$percent = ($cost_all - $newcost) / $cost_all * 100; |
|
|
|
$newcost = $newcost + $cost_full; |
|
|
|
if ($newcost >= 0 && $percent <= 15) { |
|
$modx->event->returnedValues['cost'] = $newcost; |
|
} |
|
} |
|
} |
|
} |
|
} |
|
} |
|
} |
|
break; |
|
case 'msOnCreateOrder': |
|
if ($data = $order->get()) { |
|
if (!empty($data['koupon_code'])) { |
|
$tvr = $modx->getObject('modTemplateVarResource', array( |
|
'tmplvarid' => 80, |
|
'contentid' => 1 |
|
)); |
|
if ($tvr) { |
|
$value_tv = $tvr->get('value'); |
|
if(stristr($value_tv, $data['koupon_code']) === FALSE) { |
|
$check = false; |
|
} |
|
else{ |
|
$tv_array = json_decode($value_tv, true); |
|
foreach($tv_array as &$tv_val){ |
|
if($tv_val['code'] == $data['koupon_code'] && !$tv_val['active']){ |
|
$check = true; |
|
$tv_val['active'] = 1; |
|
$tv_val['activatedon'] = date('Y-m-d H:i:s'); |
|
$tv_val['order_id'] = $msOrder->get('id'); |
|
break; |
|
} |
|
} |
|
if($check){ |
|
$tv_array = json_encode($tv_array); |
|
$tvr->set('value', $tv_array); |
|
$tvr->save(); |
|
$properties = $msOrder->get('properties'); |
|
if (!is_array($properties)) { |
|
$properties = array(); |
|
} |
|
$properties['koupon_code'] = $data['koupon_code']; |
|
$msOrder->set('properties', $properties); |
|
$msOrder->save(); |
|
} |
|
} |
|
} |
|
} |
|
} |
|
break; |
|
case 'msOnManagerCustomCssJs': |
|
if ($page != 'orders') return; |
|
$modx->controller->addHtml(" |
|
<script type='text/javascript'> |
|
Ext.ComponentMgr.onAvailable('minishop2-window-order-update', function(){ |
|
if (miniShop2.config['order_grid_fields'].in_array('properties')){ |
|
if (this.record.properties){ |
|
var key; |
|
for (key in this.record.properties) { |
|
this.fields.items[0].items[3].items[1].items.push( |
|
{ |
|
xtype: 'displayfield', |
|
name: 'properties_'+key, |
|
fieldLabel: _('ms2_properties_'+key), |
|
anchor: '100%', |
|
style: 'border:1px solid #efefef;width:95%;padding:5px;', |
|
html: this.record.properties[key] |
|
} |
|
); |
|
} |
|
} |
|
} |
|
}); |
|
</script>"); |
|
break; |
|
} |
|
return; |