Skip to content

Instantly share code, notes, and snippets.

@eapdob
Last active May 23, 2021 18:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save eapdob/40def55b18df4158eb2ddaabe72e746e to your computer and use it in GitHub Desktop.
Save eapdob/40def55b18df4158eb2ddaabe72e746e to your computer and use it in GitHub Desktop.
discount interactive
<?xml version="1.0" encoding="utf-8"?>
<modification>
<name>Product discount options</name>
<version>1.0.0</version>
<link>https://e-m.com.ua</link>
<author>em</author>
<code>product_discount_options</code>
<file path="catalog/model/catalog/product.php">
<operation error="skip">
<search><![CDATA[$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_discount WHERE product_id = '" . (int)$product_id . "' AND customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "' AND quantity > 1 AND ((date_start = '0000-00-00' OR date_start < NOW()) AND (date_end = '0000-00-00' OR date_end > NOW())) ORDER BY quantity ASC, priority ASC, price ASC");]]></search>
<add position="replace"><![CDATA[
/* Product discount options */
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_discount WHERE product_id = '" . (int)$product_id . "' AND customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "' AND quantity > 0 AND ((date_start = '0000-00-00' OR date_start < NOW()) AND (date_end = '0000-00-00' OR date_end > NOW())) ORDER BY quantity ASC, priority ASC, price ASC");
/* END Product discount options */
]]></add>
</operation>
</file>
<file path="catalog/controller/product/product.php">
<operation error="skip">
<search><![CDATA[$data['text_loading'] = $this->language->get('text_loading');]]></search>
<add position="after"><![CDATA[
/* Product discount options */
$data['text_discount'] = $this->language->get('text_discount');
$data['text_discount_percent'] = $this->language->get('text_discount_percent');
/* END Product discount options */
]]></add>
</operation>
<operation error="skip">
<search><![CDATA[$this->response->setOutput($this->load->view('product/product', $data));]]></search>
<add position="before"><![CDATA[
/* animate prices, summ, quantity */
$var_currency['value'] = $this->currency->getValue($this->session->data['currency']);
$var_currency['symbol_left'] = $this->currency->getSymbolLeft($this->session->data['currency']);
$var_currency['symbol_right'] = $this->currency->getSymbolRight($this->session->data['currency']);
$var_currency['decimals'] = $this->currency->getDecimalPlace($this->session->data['currency']);
$var_currency['decimal_point'] = $this->language->get('decimal_point');
$var_currency['thousand_point'] = $this->language->get('thousand_point');
$data['currency'] = $var_currency;
$data['tax_class_id'] = $product_info['tax_class_id'];
$data['tax_rates'] = $this->tax->getRates(0, $product_info['tax_class_id']);
$data['dicounts_unf'] = $discounts;
/* animate prices, summ, quantity */
]]></add>
</operation>
</file>
<file path="catalog/language/ru-ru/product/product.php">
<operation error="skip">
<search><![CDATA[$_['text_year'] = 'год';]]></search>
<add position="after"><![CDATA[
/* Product discount options */
$_['text_discount'] = '%s %s или более - ';
$_['text_discount_percent'] = ' скидка %s&#37;';
/* END Product discount options */
]]></add>
</operation>
</file>
<file path="catalog/language/uk-ua/product/product.php">
<operation error="skip">
<search><![CDATA[$_['text_year'] = 'рік';]]></search>
<add position="after"><![CDATA[
/* Product discount options */
$_['text_discount'] = '%s %s або більш - ';
$_['text_discount_percent'] = ' знижка %s&#37;';
/* END Product discount options */
]]></add>
</operation>
</file>
<!-- FRONT -->
<file path="catalog/view/theme/oct_techstore/template/product/product.tpl">
<operation error="skip">
<search><![CDATA[<?php if ($discounts) { ?>]]></search>
<add position="replace" offset="4"><![CDATA[
<!-- Product discount options -->
<?php if ($discounts && $disable_buy === 0) { ?>
<?php
$unit_for = '';
if (isset($isbn)) {
$unit_for = $isbn;
} else {
$unit_for = 'шт';
}
$for_label_id = 0;
function tofloat($num) {
$dotPos = strrpos($num, '.');
$commaPos = strrpos($num, ',');
$sep = (($dotPos > $commaPos) && $dotPos) ? $dotPos : ((($commaPos > $dotPos) && $commaPos) ? $commaPos : false);
if (!$sep) {
return floatval(preg_replace("/[^0-9]/", "", $num) );
}
return floatval(preg_replace("/[^0-9]/", "", substr($num, 0, $sep)) . '.' . preg_replace("/[^0-9]/", "", substr($num, $sep+1, strlen($num) ) ) );
}
?>
<div class="product-discount">
<?php foreach ($discounts as $discount) { ?>
<?php
$price_in_db = tofloat($price);
$discount_price = tofloat($discount['price']);
$percent = (100 * $discount_price)/$price_in_db;
$percent = round($percent, 0, PHP_ROUND_HALF_DOWN);
$percent = 100 - $percent;
$percent = (string)($percent);
?>
<div class="product-discount-item">
<input class="product-discount-radio js-discount-radio" type="radio"
value="<?php echo $discount['quantity']; ?>" name="discount-radio-input">
<div class="product-discount-btns">
<div class="product-discount-btns-block">
<input class="product-discount-input product-discount-input-minus" type="button"
value="<?php echo $discount['quantity']; ?>"
id="product-discount-btn-minus<?php echo $for_label_id; ?>" disabled>
<label class="product-discount-label-minus"
for="product-discount-btn-minus<?php echo $for_label_id; $for_label_id++; ?>"></label>
</div>
<div class="product-discount-btns-block">
<input class="product-discount-input product-discount-input-plus" type="button"
value="<?php echo $discount['quantity']; ?>"
id="product-discount-btn-plus<?php echo $for_label_id; ?>" disabled>
<label class="product-discount-label-plus"
for="product-discount-btn-plus<?php echo $for_label_id; $for_label_id++; ?>"></label>
</div>
</div>
<span class="product-discount-text"><?php echo sprintf($text_discount, $discount['quantity'], $unit_for); ?></span>
<span class="product-discount-price"><?php echo $discount['price']; echo '/'.$unit_for; ?></span>
<span class="product-discount-percent"><?php echo sprintf($text_discount_percent, $percent); ?></span>
</div>
<?php } ?>
</div>
<script>
$(function () {
// get first value
let firstDiscountRadio = $(".product-discount-radio").first();
let firstDiscountValue = Number(firstDiscountRadio.val());
// init counting variables
let standartValue = firstDiscountValue;
let stage = false;
if (firstDiscountValue === 1) {
let secondDiscountValue = Number($(".product-discount-radio:eq(1)").val());
standartValue = secondDiscountValue;
}
//if (firstDiscountValue === 2) {
// standartValue = 1;
//}
// input type click
$(".js-discount-radio").on("click", function () {
if (stage) {
stage = false;
} else {
// update quantity
let discountQuantity = $(this).attr("value");
// input click - display quantity
setQuantity(discountQuantity);
oct_update_product_quantity('<?php echo $product_id; ?>');
}
// 1. disabled all input button - and +
let allDiscountBtnsInput = $(".product-discount-input");
allDiscountBtnsInput.prop("disabled", true);
$('.product-discount-btns-block label').removeClass('active');
// 2. unbind click event on input button - and +
allDiscountBtnsInput.unbind("click");
// 3. find discount block, enabled find input button - and +
let discountBtns = $(this).next();
discountBtns.find(".product-discount-input").prop("disabled", false);
// 4. get input button
let discountBtnsMinus = discountBtns.find(".product-discount-input-minus");
let discountBtnsPlus = discountBtns.find(".product-discount-input-plus");
let discountBtnsMinusLabel = discountBtnsMinus.next();
let discountBtnsPlusLabel = discountBtnsPlus.next();
discountBtnsMinusLabel.addClass('active');
discountBtnsPlusLabel.addClass('active');
// 5. get value for counting
let currentValue = Number(discountBtnsMinus.val());
// 6. stagging
let nextRadioButton = $(this).parent().next().find(".js-discount-radio");
let prevRadioButton = $(this).parent().prev().find(".js-discount-radio");
let nextRadioButtonValue = 0;
if (nextRadioButton.length > 0) {
nextRadioButtonValue = Number(nextRadioButton.val());
}
// 7. handle click minus input
discountBtnsMinus.on("click", function () {
// 7.1 get value of existed quantity
let quantityValue = getQuantity();
// 7.2 sub from existed quantity
let result;
if (firstDiscountValue === 1 && currentValue === 1) {
result = countDiscount(firstDiscountValue, '-', quantityValue);
} else {
if (standartValue === quantityValue) {
result = countDiscount(firstDiscountValue, '-', quantityValue);
} else {
result = countDiscount(standartValue, '-', quantityValue);
}
}
// 7.3 check value
if (result < standartValue) {
result = standartValue;
}
// 7.4 check for stagging
if (result < currentValue) {
stage = true;
prevRadioButton.trigger("click");
}
// 7.5 display quantity
setQuantity(result);
oct_update_product_quantity('<?php echo $product_id; ?>');
});
// 8. handle click plus input
discountBtnsPlus.on("click", function () {
// 8.1 get value of existed quantity
let quantityValue = getQuantity();
// 8.2 add to existed quantity
let result;
if (firstDiscountValue === 1 && currentValue === 1) {
result = countDiscount(firstDiscountValue, '+', quantityValue);
} else {
result = countDiscount(standartValue, '+', quantityValue);
}
// 8.3 check for stagging
if (result >= nextRadioButtonValue) {
stage = true;
nextRadioButton.trigger("click");
}
// 8.4 display quantity
setQuantity(result);
oct_update_product_quantity('<?php echo $product_id; ?>');
});
});
// count discount
function countDiscount(n, type, v) {
let q = 0;
n = parseInt(n);
v = parseInt(v);
if (type === '-') {
q = v - n;
}
if (type === '+') {
q = v + n;
}
if (q < 0) {
q = 0;
}
return q;
}
// quantity
function setQuantity(q) {
let quantity = $("#input-quantity");
// animate prices, summ, quantity
animateQuantity(q);
// END animate prices, summ, quantity
quantity.val(q);
}
function getQuantity() {
let quantity = $("#input-quantity");
return Number(quantity.val());
}
// animate prices, summ, quantity
// animation quantity
function animateQuantity(mainQuantity) {
let animate_quantity_delay = 20;
let main_quantity_final = ( Number( $("#input-quantity").val() ) );
let main_quantity_start = ( Number( $("#input-quantity").val() ) );
let main_quantity_step = 0;
let main_quantity_timeout_id = 0;
function animateMainQuantityCallback() {
//main_quantity_start += main_quantity_step;
main_quantity_start = (main_quantity_start * 10 + main_quantity_step * 10) / 10;
if ((main_quantity_step > 0) && (main_quantity_start > main_quantity_final)){
main_quantity_start = main_quantity_final;
} else if ((main_quantity_step < 0) && (main_quantity_start < main_quantity_final)) {
main_quantity_start = main_quantity_final;
} else if (main_quantity_step == 0) {
main_quantity_start = main_quantity_final;
}
$('.number .plus-minus').html( (main_quantity_start) );
if (main_quantity_start != main_quantity_final) {
main_quantity_timeout_id = setTimeout(animateMainQuantityCallback, animate_quantity_delay);
}
}
function animateMainQuantity(quantity) {
main_quantity_start = main_quantity_final;
main_quantity_final = quantity;
main_quantity_step = (main_quantity_final - main_quantity_start) / 10;
clearTimeout(main_quantity_timeout_id);
main_quantity_timeout_id = setTimeout(animateMainQuantityCallback, animate_quantity_delay);
}
animateMainQuantity(mainQuantity);
}
// animate prices, summ, quantity
});
</script>
<?php } ?>
<!-- END Product discount options -->
]]></add>
</operation>
</file>
</modification>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment