Commit 04867f2e authored by Reinhold Kainhofer's avatar Reinhold Kainhofer
Browse files

Implement widget to modify variable replacements

parent fa5f77ff
OpenTools Advanced Ordernumbers for WooCommerce - Open TODOs
============================================================
- Make sure the numbers are generated only for new orders, not for exsiting ones
- Check REST API
- Would it make sense to configure the order password format?
- Implement custom variables
- Find out how multisite works and make the plugin work with it
- Add action to create an updated / new order number for an existing order
- implement woocommerce_hidden_order_itemmeta hook to hide the order number post meta?
- Use $order->order_date for the date-relating fields!
- Collect and return php warning messages (i.e. all other output) in the AJAX call and return it inside the JSON rather than as extra HTML.
HOOKS for order handling:
woocommerce_new_order: FE new
woocommerce_checkout_update_order_meta: FE updated
woocommerce_checkout_order_processed: FE created, API created
woocommerce_process_shop_order_meta: API created, API updated, BE updated
woocommerce_api_create_order: API created
woocommerce_api_edit_order: API updated
woocommerce_create_order: // Give plugins the opportunity to create an order themselves
woocommerce_generate_order_key
woocommerce_order_class
woocommerce_order_status_changed
woocommerce_order_action_
wc_create_order( $order_data );
woocommerce_new_order', $order_id );
Order created in BackEnd:
Created: wp_insert_post => Assign placeholder (no address yet)
Modified:
\ No newline at end of file
......@@ -17,10 +17,10 @@ td.counter_value {
}
table.wc_ordernumber_counters thead > tr:nth-child(odd) > th,
table.wc_ordernumber_counters tfoot > td.addcounter_row > th {
table.wc_ordernumber_counters tfoot > tr.addcounter_row > td {
background: #E0E0E0;
}
table.wc_ordernumber_counters tbody > tr:nth-child(odd) > td {
table.wc_ordernumber_counters tbody > tr:nth-child(even) > td {
background: #F0F0F0;
}
.ordernumber-btn {
......
table.ordernumber_variables {
border: 1px solid #888888;
width: inherit;
}
table.ordernumber_variables td, table.ordernumber_variables th {
padding: 0px;
vertical-align: middle;
}
/* table.ordernumber_variables td.sort:before { */
/* float: none; */
/* display: inline-block; */
/* } */
table.ordernumber_variables thead th {
text-align: center;
width: auto;
}
td.counter_value {
text-align: center;
}
table.ordernumber_variables thead > tr:nth-child(odd) > th,
table.ordernumber_variables tfoot > tr.addreplacement_row > td {
background: #E0E0E0;
}
table.ordernumber_variables tbody > tr:nth-child(even) > td {
background: #F0F0F0;
}
table.ordernumber_variables tbody tr td input {
width: 120px;
}
.ordernumber-btn {
cursor: pointer;
}
table.ordernumber_variables img {
padding: 0;
margin: 0;
}
tr.rowhidden {
display: none;
}
\ No newline at end of file
var ordernumberVariablesAddRow = function (template, element) {
jQuery("#" + template + " tr").clone(true)
.appendTo("table#" + element + " tbody")
.find('input,select,button,img')
.removeAttr('disabled');
jQuery("tr#ordernumber-replacements-empty-row")
.addClass("rowhidden")
.find('input')
.attr('disabled', 'disabled');
}
jQuery(document).ready (function () {
jQuery('img.ordernumber-replacement-deletebtn').click(
function () {
jQuery(this).closest('tr').remove();
var count = jQuery(this).closest('table').find('tbody tr').length;
if (count==0) {
jQuery("tr#ordernumber-replacements-empty-row")
.removeClass("rowhidden")
.find('input,select,button,img')
.removeAttr('disabled');
}
}
);
});
......@@ -69,14 +69,15 @@ if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', g
'type' => 'text',
),
array(
'title' => __( 'Global', 'woocommerce-advanced-ordernumbers' ),
'desc' => __( 'Counter Scope', 'woocommerce-advanced-ordernumbers' ),
'title' => __( 'Use global counter', 'woocommerce-advanced-ordernumbers' ),
'desc' => __( 'A global counter is independent of the number format and will never reset. A non-global counter runs within the number format and will start from the inital value whenever any of the variables used in the format changes (to be precise: a new counter will be used, so it is possible to have multiple counters running in parallel).', 'woocommerce-advanced-ordernumbers' ),
'desc_tip' => true,
'id' => 'ordernumber_global',
'type' => 'checkbox',
'default' => 'no'
),
array(
'title' => __( 'Counter Digits', 'woocommerce-advanced-ordernumbers' ),
'title' => __( 'Counter Digits', 'woocommerce-advanced-ordernumbers' ),
'desc' => __( 'Minimum number of digits for the number', 'woocommerce-advanced-ordernumbers' ),
'desc_tip' => true,
'id' => 'ordernumber_padding',
......@@ -119,7 +120,6 @@ if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', g
array(
'name' => __( 'Current Counters', 'woocommerce-advanced-ordernumbers' ),
// 'desc' => __( 'View and modify the current counter values.', 'woocommerce-advanced-ordernumbers' ),
'type' => 'title',
'id' => 'ordernumber_counters'
),
......@@ -142,7 +142,7 @@ if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', g
add_option ('ordernumber_start', '1');
add_option ('ordernumber_step', '1');
// add_option ('ordernumber_counters', '1');
add_option ('ordernumber_variables', array());
// register filters and actions
......@@ -151,8 +151,9 @@ if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', g
// The checkout settings page assumes all subpages are payment gateways, so we have to override this and manually pass our settings:
add_action( 'woocommerce_settings_checkout', array( $this, 'settings_output' ) );
add_action( 'woocommerce_settings_save_checkout', array( $this, 'settings_save' ) );
add_action( 'woocommerce_admin_field_ordernumber_variables', array( $this, 'admin_field_variables' ) );
add_action( 'woocommerce_admin_field_ordernumber_counters', array( $this, 'admin_field_counters' ) );
add_action( 'woocommerce_admin_field_ordernumber_variables', array( $this, 'admin_field_variables' ) );
add_action( 'pre_update_option_ordernumber_variables', array( $this, 'update_option_variables'));
add_action( 'admin_print_styles-woocommerce_page_wc-settings', array($this, 'print_admin_styles'));
add_action( 'admin_print_scripts-woocommerce_page_wc-settings', array($this, 'print_admin_scripts'));
......@@ -217,6 +218,9 @@ if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', g
public function print_admin_styles () {
wp_register_style ( 'ordernumber-counter-style', plugins_url('assets/css/ordernumber-counter.css', __FILE__) );
wp_enqueue_style('ordernumber-counter-style');
wp_register_style ( 'ordernumber-variables-style', plugins_url('assets/css/ordernumber-variables.css', __FILE__) );
wp_enqueue_style('ordernumber-variables-style');
}
/**
* Print the JS for the counter values and counter variables tables to the page header in the WC backend admin setting page
......@@ -242,27 +246,147 @@ if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', g
// in JavaScript, object properties are accessed as ajax_object.ajax_url, ajax_object.we_value
wp_localize_script( 'ordernumber-counter-script', 'ajax_ordernumber', $localizations );
wp_register_script( 'ordernumber-variables-script', plugins_url( 'assets/js/ordernumber-variables.js', __FILE__), array('jquery') );
wp_enqueue_script( 'ordernumber-variables-script');
}
/**
* Render the Custom Variables configuration table
*/
public function admin_field_variables($settings) {
echo "<h2>TODO!</h2>";
$variables = get_option( $settings['id'], array() );
if (!is_array($variables)) {
$variables = array();
}
// echo "<pre>Admin field " . print_r($settings['id'],1) . " has value: " . print_r($variables, 1) . "</pre>";
// Description handling
$field_description = WC_Admin_Settings::get_field_description( $settings );
extract( $field_description );
?>
<tr valign="top">
<td class="forminp forminp-<?php echo sanitize_title( $settings['type'] ) ?>" colspan="2">
<table id="ordernumber_variables_template" style="display:none">
<?php echo $this->create_replacements_row_html($settings['id'], array(), 'disabled'); ?>
</table>
<table id="ordernumber_variables" class="ordernumber_variables widefat wc_input_table sortable" cellspacing="0">
<?php
$columns = apply_filters( 'woocommerce_ordernumber_variables_columns', array(
'variables_ifvar' => __( 'If variable ...', 'woocommerce-advanced-ordernumbers' ),
'variables_ifop' => '',
'variables_ifval' => __( 'Value', 'woocommerce-advanced-ordernumbers' ),
'variables_then' => __( '', 'woocommerce-advanced-ordernumbers' ),
'variables_thenvar' => __( 'Set variable ...', 'woocommerce-advanced-ordernumbers' ),
'variables_thenval' => __( 'to value ...', 'woocommerce-advanced-ordernumbers' ),
'sort' => '&nbsp;',
'variables_settings' => '',
) );
?>
<thead>
<tr>
<?php
foreach ( $columns as $key => $column ) {
echo '<th class="' . esc_attr( $key ) . '">' . esc_html( $column ) . '</th>';
}
?>
</tr>
<tr id = "ordernumber-replacements-empty-row" class="oton-empty-row-notice <?php echo (empty($variables))?'':'rowhidden'; ?>">
<td class="oton-empty-row-notice" colspan="8">
<em> <?php echo __('No custom variables have been defined.', 'woocommerce-advanced-ordernumbers'); ?></em>
<input type="hidden" name="<?php echo $settings['id']; ?>" value="" <?php echo (empty($variables))?'':'disabled'; ?>>
</td>
</tr>
</thead>
<colgroup>
<?php
foreach ($columns as $key => $column) {
echo '<col class="' . esc_attr($key) . '" />';
}
?>
</colgroup>
<tbody>
<?php
foreach ($variables as $var) {
echo $this->create_replacements_row_html($settings['id'], $var);
} ?>
</tbody>
<tfoot>
<tr class='addreplacement_row'>
<td colspan=8 class='variable_add'>
<div class='ordernumber-variables-addbtn ordernumber-btn' onClick="ordernumberVariablesAddRow('ordernumber_variables_template', 'ordernumber_variables')">
<div class='ordernumber-ajax-loading'><img src='<?php echo plugins_url( 'assets/images/icon-16-new.png', __FILE__ ); ?>' class='ordernumber-counter-addbtn' /></div>
<?php _e('Add new custom variable', 'woocommerce-advanced-ordernumbers'); ?>
</div>
</td>
</tr>
</tfoot>
</table>
</td>
</tr>
<?php
}
protected function create_replacements_row_html($name, $values = array(), $disabled = '') {
$operator = (isset($values['conditionop'])?$values['conditionop']:'');
$operators = array(
'equals' => '=',
'contains' => __('contains', 'woocommerce-advanced-ordernumbers'),
'smaller' => '<',
'smallerequal' => '<=',
'larger' => '>',
'largerequal' => '>=',
'startswith' => __('starts with', 'woocommerce-advanced-ordernumbers'),
'endswith' => __('ends with', 'woocommerce-advanced-ordernumbers'),
);
$html = '
<tr>
<td class="oton-replacement-variable"><input name="' . $name . '[conditionvar][]" value="' . (isset($values['conditionvar'])?$values['conditionvar']:'') . '" ' . $disabled . '/></td>
<td class="oton-replacement-op" ><select name="' . $name . '[conditionop][]" ' . $disabled . '>';
foreach ($operators as $op => $opname) {
$html .= ' <option value="' . esc_attr($op) . '" ' . (($op === $operator)?'selected':'') . '>' . esc_html($opname) . '</option>';
}
$html .= '</td>
<td class="oton-replacement-value" ><input name="' . $name . '[conditionval][]" value="' . (isset($values['conditionval'])?$values['conditionval']:'') . '" ' . $disabled . '/></td>
<td>=></td>
<td class="oton-replacement-variable"><input name="' . $name . '[newvar][]" value="' . (isset($values['newvar'])?$values['newvar']:'') . '" ' . $disabled . '/></td>
<td class="oton-replacement-newvalue"><input name="' . $name . '[newval][]" value="' . (isset($values['newval'])?$values['newval']:'') . '" ' . $disabled . '/></td>
<td class="sort">&nbsp;</td>
<td><img src="' . plugins_url( 'assets/images/icon-16-delete.png', __FILE__ ) . '" class="ordernumber-replacement-deletebtn ordernumber-btn"></td>
</tr>';
return $html;
}
/**
* Store the variable replacements array into the options. Need to transpose the array before we can store it into the options...
* This filter is called directly before the option is saved.
*/
public function update_option_variables ($value) {
if (!is_array($value))
return array();
$keys = array_keys($value);
$vallist = array();
foreach (array_keys($value[$keys[0]]) as $i) {
$entry = array();
foreach ($keys as $k) {
$entry[$k] = $value[$k][$i];
}
$vallist[] = $entry;
}
return $vallist;
}
/**
* Render the Counter Values modification table
*/
public function admin_field_counters ($settings) {
// if ( ! isset( $value['id'] ) ) {
// $value['id'] = '';
// }
// if ( ! isset( $value['title'] ) ) {
// $value['title'] = isset( $value['name'] ) ? $value['name'] : '';
// }
// Description handling
$field_description = WC_Admin_Settings::get_field_description( $settings );
extract( $field_description );
......@@ -422,23 +546,19 @@ if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', g
public function counter_delete_callback() {
$json = array('action' => 'delete_counter', 'success' => 0);
$json['success'] = $this->_deleteCounter($_POST['nrtype'], $_POST['counter']);
// print("Counter was set to $nr, post variables: nrtpe=$_POST['nrtype'], counter=$_POST['counter'], value=$_POST['value']\n\n");
$json["message"] = "Counter was deleted, post variables: nrtpe=".$_POST['nrtype'].", counter=".$_POST['counter']."\n\n";
wp_send_json($json);
}
public function counter_add_callback () {
$type = $_POST['nrtype'];
$format = $_POST['counter'];
// $value = $_POST['value'];
$value = "0";
$value = isset($_POST['value'])?$_POST['value']:"0";
$json = array('action' => 'add_counter', 'success' => 0);
if ($this->_getCounter($type, $format, -1) != -1) {
// Counter already exists => error message
$json['error'] = sprintf(__('Counter "%s" already exists, cannot create again.'), $format);
} else {
$json['success'] = $this->_setCounter($type, $format, $value);
// $json['error'] = "Created counter $format with value $value";
$json['newrow'] = $this->create_admin_counter_row($type, $format, $value);
}
wp_send_json($json);
......@@ -447,7 +567,6 @@ $json["message"] = "Counter was deleted, post variables: nrtpe=".$_POST['nrtype'
public function counter_set_callback () {
$json = array('action' => 'set_counter', 'success' => 0);
$json['success'] = $this->_setCounter($_POST['nrtype'], $_POST['counter'], $_POST['value']);
// print("Counter was set to $nr, post variables: nrtpe=".$_POST['nrtype'].", counter=".$_POST['counter'].", value=".$_POST['value']."\n\n");
wp_send_json($json);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment