Skip to content

Commit

Permalink
Merge pull request #27 from piatkowski/dev
Browse files Browse the repository at this point in the history
Merging 1.6
  • Loading branch information
piatkowski authored Sep 26, 2022
2 parents b54f472 + 70b5be4 commit 9b9929e
Show file tree
Hide file tree
Showing 22 changed files with 435 additions and 121 deletions.
7 changes: 4 additions & 3 deletions assets/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,7 @@
field.disallow_past_date = input_fdpdisallow_past_date.is(':checked');
break;
case 'imageupload':
case 'fileupload':
//field.max_file_count = $row.find('input.fu-max-file-count').val();
field.max_file_size = $row.find('input.fu-max-file-size').val();
field.allowed_extensions = $row.find('input.fu-allowed-extensions').val();
Expand Down Expand Up @@ -749,10 +750,10 @@
suggest.push(field.name + ":date");
// $("#formula_fields").append('<span class="formula-field">{' + field.name + ':date}</span> ');
$("#wck-parameters .defined-fields").append('<option value="{' + field.name + ':date}">Timestamp of "' + field.title + '" {' + field.name + ':date}' + '</option>');
} else if (field.type === "imageupload") {
} else if (field.type === "imageupload" || field.type === "fileupload") {
suggest.push(field.name + ":size");
//$("#formula_fields").append('<span class="formula-field">{' + field.name + ':size}</span> ');
$("#wck-parameters .defined-fields").append('<option value="{' + field.name + ':size}">File size of "' + field.title + '" {' + field.name + ':size}' + ' [bytes]</option>');
$("#wck-parameters .defined-fields").append('<option value="{' + field.name + ':size}">File size of "' + field.title + '" {' + field.name + ':size}' + ' [MB]</option>');
}
}
};
Expand Down Expand Up @@ -919,7 +920,7 @@
//$("#" + field_id + " .ft-pattern").val(this.pattern);
} else if (this.type === "colorpicker" || this.type === "datepicker" || this.type === "rangedatepicker") {
$("#" + field_id + " .fdp-disallow-past-date").prop("checked", this.disallow_past_date);
} else if (this.type === "imageupload") {
} else if (this.type === "imageupload" || this.type === "fileupload") {
$("#" + field_id + " .fu-max-file-size").val(this.max_file_size);
$("#" + field_id + " .fu-allowed-extensions").val(this.allowed_extensions);
var ext = this.allowed_extensions.split("|");
Expand Down
2 changes: 1 addition & 1 deletion assets/js/admin.min.js

Large diffs are not rendered by default.

66 changes: 53 additions & 13 deletions assets/js/wckalkulator.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,73 @@
(function ($) {
"use strict";
$(document).ready(function ($) {
var userTimeout;
var userTimeout, updateTimeout;
var _form = wck_ajax_object.form;

var shouldCalculatePrice = wck_ajax_object._wck_has_expression === "1";
var CV = {};

if (wck_ajax_object.hasOwnProperty("_wck_visibility_rules") && wck_ajax_object._wck_visibility_rules !== null) {
$.each(wck_ajax_object._wck_visibility_rules, function (fieldName, options) {
$("[name*='wck[" + fieldName + "]']").prop("disabled", true).hide().closest('tr').hide();
findFieldAndToggle(fieldName, false);
CV[fieldName] = options;
});
}

function findFieldAndToggle(fieldName, show) {
var inputField = $("[name*='wck[" + fieldName + "]']");
if(inputField.length > 0) {
if(show) {
inputField.prop("disabled", false).show().closest('tr').show();
} else {
inputField.prop("disabled", true).hide().closest('tr').hide();
}
} else {
var staticField = $("[data-wck-static-name='" + fieldName + "']");
if(staticField.length > 0) {
staticField.toggle(show);
}
}
}

function updateUI() {
//wck-dynamic support, conditional visibility support
var formFields = {};
jQuery(_form + " [name^=wck").each(function () {
var fieldName = $(this).attr("name").replace("wck[", "").replace("]", "").replace("[]", "");
formFields["{" + fieldName + "}"] = $(this).val();
if (CV.hasOwnProperty(fieldName)) {
jQuery(_form + " [data-wck-static-name] , " + _form + " [name^=wck").each(function () {
var fieldName = false;
if($(this)[0].hasAttribute("name")) {
var fieldName = $(this).attr("name").replace("wck[", "").replace("]", "").replace("[]", "");
formFields["{" + fieldName + "}"] = $(this).val();
var type = $(this).prop("type");
switch(type) {
case 'file':
formFields["{" + fieldName + ":size}"] = (($(this)[0].files.length === 1) ? Math.round(($(this)[0].files[0].size / 1000000 + Number.EPSILON) * 100) / 100 : 0 );
break;
}
} else {
fieldName = $(this).data("wckStaticName");
}

if (fieldName && CV.hasOwnProperty(fieldName)) {
toggleField(fieldName, CV[fieldName]);
}
});

$.each(wck_ajax_object._wck_additional_parameters, function (name, value) {
formFields["{" + name + "}"] = value;
});

$("span.wck-dynamic").each(function () {
var expr = $(this).data('expr');
var vars = expr.match(/{[^}]+}/gm);
vars.forEach(function (v, i) {
expr = expr.replaceAll(v, formFields[v]);
});
$(this).text(Math.round(Mexp.eval(expr) * 100) / 100);
try {
$(this).text(Math.round((Mexp.eval(expr) + Number.EPSILON) * 100) / 100);
} catch (error) {
console.log("[Mexp]", error);
}
});
}

Expand All @@ -43,6 +79,8 @@
value = value.substring(0, n);
}
return value;
} else if(field.prop("type") === "file") {
return ((field[0].files.length === 1) ? (Math.round((field[0].files[0].size / 1000000 + Number.EPSILON) * 100) / 100) : 0);
}
return field.val();
}
Expand All @@ -64,12 +102,12 @@
return state !== false;
});
if (state === true) {
$("[name*='wck[" + fieldName + "]']").prop("disabled", false).show().closest('tr').show();
findFieldAndToggle(fieldName, true);
return false;
}
});
if (state !== true) {
$("[name*='wck[" + fieldName + "]']").prop("disabled", true).hide().closest('tr').hide();
findFieldAndToggle(fieldName, false);
return false;
}
}
Expand Down Expand Up @@ -132,19 +170,21 @@
}
});

$(document).on('change', _form + ' input, ' + _form + ' select, ' + _form + ' textarea', function () {
updateUI();
$(document).on('change keyup', _form + ' input, ' + _form + ' select, ' + _form + ' textarea', function () {
clearTimeout(userTimeout);
clearTimeout(updateTimeout);
userTimeout = setTimeout(function () {
$("#wckalkulator-price").html('<img style="display:inline" src="data:image/gif;base64,R0lGODlhEAAQAPUVAHt7e729vf///4R7e+/v762trZSUlKWlpZycnPf39+bm5t7e3tbW1s7OzoSEhMXFxc7FzpSMjJyUlP/397WtraWlnM7FxbW1tb21tebe3tbOzqWcnIyEhHNzc3tzc4yMjK2lpbWttcW9vffv76Wtpa2lra2tpb21vcXFzt7e1qWlrdbe1pScnO/v5gAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH/C05FVFNDQVBFMi4wAwEAAAAh+QQJBwAV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A96IAUFQg0MDZIAQheVBVQQDaMPDwEBF6kcQx8ipaaoBUpyGxemlktBACH5BAUHABAALAEAAQAOAA4AAAaRQAikMCg0CATGxVEQMiIGhgJJSCQUiAhjI1F4p1Vr4rAZLBYKBsLh2CgEggSgcG44QI0GyAFJED4PKQsSIEJCewEBUQwMABqGEBodHwgSDYyOkJIInAF5BoWGe5wIB3kBc3kFHgcHWQAPsQEfAAAcBa0HHhsGsokXBcEHbU8fF78XwMNaQgUACAUBFwcGHk0QQQA7" alt="...">');
calculatePrice();
}, 1000);
}, 500);
updateTimeout = setTimeout(function(){
updateUI();
}, 300);
});

updateUI();
calculatePrice();


$("span.wck-field-tip").tipTip({
attribute: 'title',
defaultPosition: 'left'
Expand Down
2 changes: 1 addition & 1 deletion assets/js/wckalkulator.min.js

Large diffs are not rendered by default.

94 changes: 94 additions & 0 deletions docs/usermanual/code_snippets.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
---
order: -610
label: "Code Snippets"
icon: file-code
---

# Useful Code Snippets

WC Kalculator is an advanced plugin to deal with additional product fields and calculate custom prices. Some functionalities are very specific and require an individual approach. In this section, we will post some useful code snippets for use in your own projects. You can get even more out of this plugin!

## 1. Product Quick View (lightbox)

Some themes use Quick View to show a lightbox with product details and "Add to Cart" button.
WCK Plugin can't work in a lightbox, so in this code snippet we will show how to replace ATC button with "Customize Product", which will redirect to the product page.

``` # Replace ATC Button with Customize Product Button
/*
* Code goes to the functions.php in your theme
* In most cases QV's content is returned by some AJAX action
* You can use browser dev tools to get action name and change 'REPLACE_WITH_QUICKVIEW_ACTION'
*/
if(!function_exists('wck_before_atc')) {
function wck_before_atc() {
if(
wp_doing_ajax() &&
isset($_POST['action']) &&
$_POST['action'] === 'REPLACE_WITH_QUICKVIEW_ACTION'
) {
echo '<!--'; // start HTML comment to cut ATC button and QTY input field
}
}
}
if(!function_exists('wck_after_atc')) {
function wck_after_atc() {
if(
wp_doing_ajax() &&
isset($_POST['action']) &&
$_POST['action'] === 'REPLACE_WITH_QUICKVIEW_ACTION'
) {
global $product;
$product_url = get_permalink( $product->ID );
echo '-->'; // end HTML content
?>
<a href="<?php echo esc_url($product_url); ?>" class="single_add_to_cart_button button alt">
Customize Product
</a>
<?php
}
}
}
add_action('woocommerce_before_add_to_cart_button', 'wck_before_atc');
add_action('woocommerce_after_add_to_cart_button', 'wck_after_atc');
```

## 2. Move Price block to the different position - for example above the fields

```
if( !function_exists('wck_add_custom_price_block') ) {
function wck_add_custom_price_block() {
/*
* Here you can customize price block.
* Tag with id="wckalkulator-price" is required!
*/
?>
<p class="wckalkulator-price">
<span id="wckalkulator-price"></span>
</p>
<?php
}
}
if( !function_exists('wck_remove_default_price_block') ) {
function wck_custom_price_block() {
remove_action(
'woocommerce_before_add_to_cart_button',
array('WCKalkulator\Woocommerce\Product', 'price_block')
);
remove_action(
'woocommerce_after_add_to_cart_button',
array('WCKalkulator\Woocommerce\Product', 'price_block')
);
}
}
// priority must be > 10 and the hook must be wp_enqueue_scripts
add_action( 'wp_enqueue_scripts', 'wck_remove_default_price_block', 20);
// woocommerce_before_add_to_cart_form - is an example
add_action('woocommerce_before_add_to_cart_form', 'wck_add_custom_price_block');
```
16 changes: 15 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Tags: woocommerce custom fields, woocommerce product price, woocommerce product fields, woocommerce custom price field, woocommerce personalized product, woocommerce custom product fields, product fields, custom product price, price calculation, price formula
Requires at least: 5.0
Tested up to: 6.0.1
Stable tag: 1.5.7
Stable tag: 1.6.0
Requires PHP: 5.6
License: GNU GPLv2
Donate link: https://www.paypal.com/donate/?hosted_button_id=5DNZK72H5YCBY
Expand Down Expand Up @@ -149,6 +149,20 @@ You can use `acf('field_name')` function to get ACF field value in custom price
Full documentation at: [www.wckalkulator.com](https://wckalkulator.com)

== Changelog ==

2022-09-?? v.1.6.0
- image file upload bug fixes
- conditional visibility works with static fields (html, paragraph)
- added support for {image:size} in HTML field's content (for example: {={image:size}} MB)
- display calculated product price in the cart widget (cart popup)
- new parameter: product_is_on_sale to use in formula
- added parameters to js dynamic formula in HTML field
- added placeholder for select and dropdown fields
- bug fixed: incorrect value of the image swatch in the cart

v.1.5.5-1.5.7
- bug fixes

2022-08-21 v.1.5.4
- add option to show Price Block before or after "Add to cart" button
- bug fixes
Expand Down
5 changes: 4 additions & 1 deletion src/Ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public static function enqueue_scripts()
{
$fieldset = FieldsetProduct::getInstance();
if ($fieldset->has_fieldset('current')/* && $fieldset->has_expression('current')*/) {
$fieldset->init();
$formula_parameters = $fieldset->set_additional_input_variables(true);

wp_enqueue_script(
'wck-ajax-script',
Expand All @@ -76,7 +78,8 @@ public static function enqueue_scripts()
'_wck_i18n_required' => __('You should check at least one option.', 'wc-kalkulator'),
'_wck_i18n_maxfilesize' => __('This file is too big!', 'wc-kalkulator'),
'form' => Settings::get('form_css_selector'),
'_wck_visibility_rules' => $fieldset->visibility_rules()
'_wck_visibility_rules' => $fieldset->visibility_rules(),
'_wck_additional_parameters' => ($formula_parameters)
)
) . ';'
);
Expand Down
4 changes: 4 additions & 0 deletions src/Cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ public static function delete_customer_uploads()
//$upload_path = wp_upload_dir()['basedir'] . $customer_dir;
$upload_path = Settings::get('upload_customer_data_dir');

if(!file_exists($upload_path)) {
return;
}

$ext = array('jpg', 'jpeg', 'png', 'gif');
$dir = new \RecursiveDirectoryIterator($upload_path);
$files = new \RecursiveIteratorIterator($dir);
Expand Down
Loading

0 comments on commit 9b9929e

Please sign in to comment.