Skip to content

Commit

Permalink
Refactored to using shipping rule conditions. Added shipping_class as…
Browse files Browse the repository at this point in the history
… condition type.
  • Loading branch information
dennisnissle committed Nov 13, 2023
1 parent c7bfaa0 commit ea0edfa
Show file tree
Hide file tree
Showing 8 changed files with 274 additions and 116 deletions.
11 changes: 10 additions & 1 deletion assets/css/admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ table.wc-gzd-shipments-shipping-rules tbody td.conditions .conditions-columns {
display: flex;
flex-wrap: wrap;
align-items: baseline; }
table.wc-gzd-shipments-shipping-rules tbody td.conditions .conditions-columns .shipping-rules-type-container input {
table.wc-gzd-shipments-shipping-rules tbody td.conditions .conditions-columns .shipping-rules-condition-type-container input {
max-width: 80px; }
table.wc-gzd-shipments-shipping-rules tbody td.conditions .conditions-columns .conditions-column {
margin-right: 5px; }
Expand All @@ -322,6 +322,15 @@ table.wc-gzd-shipments-shipping-rules tbody td.conditions .conditions-columns {
margin-left: auto; }
table.wc-gzd-shipments-shipping-rules tbody td.conditions .conditions-columns .conditions-column.conditions-actions .button {
margin-top: 0; }
table.wc-gzd-shipments-shipping-rules tbody td.conditions .conditions-columns .conditions-column .select2-container {
min-width: 180px;
min-height: 28px; }
table.wc-gzd-shipments-shipping-rules tbody td.conditions .conditions-columns .conditions-column .select2-container .select2-search__field {
height: 28px; }
table.wc-gzd-shipments-shipping-rules tbody td.conditions .conditions-columns .conditions-column .select2-container .select2-search--inline {
margin: 0; }
table.wc-gzd-shipments-shipping-rules tbody td.conditions .conditions-columns .conditions-column .select2-container .select2-selection--multiple ul.select2-selection__rendered {
display: block; }

table.wc-gzd-shipments-shipping-rules tbody td.costs p.form-field input {
max-width: 110px; }
Expand Down
2 changes: 1 addition & 1 deletion assets/css/admin.min.css

Large diffs are not rendered by default.

19 changes: 18 additions & 1 deletion assets/css/admin.scss
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ table.wc-gzd-shipments-shipping-rules {
flex-wrap: wrap;
align-items: baseline;

.shipping-rules-type-container {
.shipping-rules-condition-type-container {
input {
max-width: 80px;
}
Expand All @@ -502,6 +502,23 @@ table.wc-gzd-shipments-shipping-rules {
margin-top: 0;
}
}

.select2-container {
min-width: 180px;
min-height: 28px;

.select2-search__field {
height: 28px;
}

.select2-search--inline {
margin: 0;
}

.select2-selection--multiple ul.select2-selection__rendered {
display: block;
}
}
}
}
}
Expand Down
71 changes: 38 additions & 33 deletions assets/js/admin-shipping-rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
var currentRules = { ...this.get( 'rules' ) };
currentRules[ String( packaging ) ] = rules;

console.log(currentRules);

this.set( 'rules', currentRules );
},
updateConditions: function( conditions, packaging, ruleId ) {
Expand All @@ -34,8 +32,6 @@
rule['conditions'] = conditions;
currentRules[ String( packaging ) ][ theRuleId ] = rule;

console.log(currentRules);

this.set( 'rules', currentRules );
},
getRulesByPackaging: function( packaging ) {
Expand Down Expand Up @@ -116,20 +112,43 @@

view.initRow( rowData );
},
initRow: function( rowData ) {
var view = this,
$tr = view.$el.find( 'tr[data-id="' + rowData.rule_id + '"]'),
$tbody = $tr.parents( 'tbody' );
initFields: function( $tr, rowData ) {
$( document.body ).trigger( 'wc-enhanced-select-init' );

// Support select boxes
// Support select2 select boxes
$tr.find( 'select' ).each( function() {
var attribute = $( this ).data( 'attribute' );
$( this ).find( 'option[value="' + rowData[ attribute ] + '"]' ).prop( 'selected', true );

if ( ! rowData[ attribute ] ) {
return;
}

var selected = Array.isArray( rowData[ attribute ] ) ? rowData[ attribute ] : Array( String( rowData[ attribute ] ) ),
$select = $( this );

$.each( selected, function( i, sel ) {
$isSelected = $select.find( 'option[value="' + String( sel ) + '"]' );

if ( $isSelected.length > 0 ) {
$isSelected.prop( 'selected', true );
}
} );

if ( $select.hasClass( 'enhanced' ) ) {
$select.trigger( 'change' );
}
} );

$tr.find( 'input.decimal' ).on( 'change', { view: this }, this.onChangeDecimal );
$tr.find( 'input.wc_input_price' ).on( 'change', { view: this }, this.onChangePrice );
$tr.find( 'input.decimal, input.wc_input_price' ).trigger( 'change' );
},
initRow: function( rowData ) {
var view = this,
$tr = view.$el.find( 'tr[data-id="' + rowData.rule_id + '"]'),
$tbody = $tr.parents( 'tbody' );

view.initFields( $tr, rowData );

$tr.find( '.shipping-rule-remove' ).on( 'click', { view: this }, this.onDeleteRow );
$tr.find( '.shipping-rule-add' ).on( 'click', { view: this }, this.onAddRow );
Expand All @@ -151,7 +170,6 @@
}
}

$( document.body ).trigger( 'wc-enhanced-select-init' );
$( document.body ).trigger( 'init_tooltips' );
},
onChangeDecimal: function( event ) {
Expand Down Expand Up @@ -215,8 +233,6 @@
index++;
} );

console.log(newRow);

rules[ 'rule_' + newRow.rule_id ] = newRow;
model.updateRules( rules, view.packaging );

Expand Down Expand Up @@ -251,8 +267,6 @@
value = parseFloat( value.replace( data.price_decimal_separator, '.' ) );
}

console.log(attribute);

if ( ! rules[ rule_index ] || String( rules[ rule_index ][ attribute ] ) !== String( value ) ) {
rules[ rule_index ][ attribute ] = value;

Expand Down Expand Up @@ -318,11 +332,7 @@
$tr = view.$el.find( 'tr[data-condition="' + rowData.condition_id + '"]'),
$tbody = $tr.parents( 'tbody' );

// Support select boxes
$tr.find( 'select' ).each( function() {
var attribute = $( this ).data( 'attribute' );
$( this ).find( 'option[value="' + rowData[ attribute ] + '"]' ).prop( 'selected', true );
} );
view.initFields( $tr, rowData );

if ( 0 === $tr.index() ) {
$tr.find( '.condition-remove' ).hide();
Expand All @@ -332,36 +342,31 @@
$tr.find( '.shipping-rules-type-container' ).hide();
$tr.find( '.conditions-column:not(.conditions-when,.conditions-actions)' ).hide();

$tr.find( '.shipping-rules-type' ).on( 'change', { view: this }, this.onChangeRuleType );
$tr.find( '.shipping-rules-type' ).trigger( 'change' );

$tr.find( 'input.decimal' ).on( 'change', { view: this }, this.onChangeDecimal );
$tr.find( 'input.wc_input_price' ).on( 'change', { view: this }, this.onChangePrice );
$tr.find( 'input.decimal, input.wc_input_price' ).trigger( 'change' );
$tr.find( '.shipping-rules-condition-type' ).on( 'change', { view: this }, this.onChangeRuleType );
$tr.find( '.shipping-rules-condition-type' ).trigger( 'change' );

$tr.find( '.condition-remove' ).on( 'click', { view: this }, this.onDeleteRow );
$tr.find( '.condition-add' ).on( 'click', { view: this }, this.onAddRow );

$( document.body ).trigger( 'wc-enhanced-select-init' );
$( document.body ).trigger( 'init_tooltips' );
},
onChangeRuleType: function( event ) {
var $tr = $( this ).closest( 'tr' ),
rule = $( this ).val(),
view = event.data.view;

$tr.find( '.shipping-rules-type-container' ).hide();
$tr.find( '.shipping-rules-condition-type-container' ).hide();
$tr.find( '.conditions-column:not(.conditions-when,.conditions-actions)' ).hide();
$tr.find( '.shipping-rules-type-container-' + rule ).show();
$tr.find( '.shipping-rules-condition-type-container-' + rule ).show();

if ( $tr.find( '.shipping-rules-type-container-' + rule + '.shipping-rule-type-operator :input' ).length > 0 ) {
$tr.find( '.shipping-rules-type-container-' + rule + '.shipping-rule-type-operator :input' ).trigger( 'change' );
if ( $tr.find( '.shipping-rules-condition-type-container-' + rule + '.shipping-rule-condition-type-operator :input' ).length > 0 ) {
$tr.find( '.shipping-rules-condition-type-container-' + rule + '.shipping-rule-condition-type-operator :input' ).trigger( 'change' );
} else {
view.updateModel( $tr.data( 'condition' ), 'operator', '' );
}

$tr.find( '.shipping-rules-type-container-' + rule + '.shipping-rule-type-operator :input' ).trigger( 'change' );
$tr.find( '.shipping-rules-type-container-' + rule ).parents( '.conditions-column' ).show();
$tr.find( '.shipping-rules-condition-type-container-' + rule + '.shipping-rule-condition-type-operator :input' ).trigger( 'change' );
$tr.find( '.shipping-rules-condition-type-container-' + rule ).parents( '.conditions-column' ).show();
},
updateModel: function( conditionId, attribute, value ) {
var model = this.model,
Expand Down
Loading

0 comments on commit ea0edfa

Please sign in to comment.