Skip to content

Commit

Permalink
Checkbox status rendering (+ editing), e.g. for parcel delivery check…
Browse files Browse the repository at this point in the history
…box.
  • Loading branch information
dennisnissle committed Nov 17, 2023
1 parent 2e0bfe7 commit bef0591
Show file tree
Hide file tree
Showing 9 changed files with 370 additions and 100 deletions.
19 changes: 19 additions & 0 deletions assets/css/admin.scss
Original file line number Diff line number Diff line change
Expand Up @@ -608,4 +608,23 @@ table.wc-gzd-tax-example tr th:first-child, table.wc-gzd-tax-example tr td:first
}
}
}
}

.wc-gzd-checkbox-log-list {
max-width: 400px;

.wc-gzd-log-checkbox {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
margin: 1em 0;

p.checkbox-title, p.checkbox-status {
margin: 0;
}

&:last-child {
margin-bottom: 0;
}
}
}
26 changes: 0 additions & 26 deletions assets/js/static/admin-settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@ window.germanized = window.germanized || {};
* Order Data Panel
*/
germanized.settings = {

params: {},

init: function() {

var self = this;

this.params = wc_gzd_admin_settings_params;

try {
Expand All @@ -25,7 +22,6 @@ window.germanized = window.germanized || {};

$( document )
.on( 'change', 'input[name=woocommerce_gzd_dispute_resolution_type]', this.onChangeDisputeResolutionType )
.on( 'click', 'a.woocommerce-gzd-input-toggle-trigger', this.onInputToogleClick )
.on( 'change', '.wc-gzd-setting-tabs input.woocommerce-gzd-tab-status-checkbox', this.onChangeTabStatus )
.on( 'change', '.wc-gzd-setting-tab-enabled :input', this.preventWarning )
.on( 'click', 'a.wc-gzd-install-extension-btn', this.onInstallExtension )
Expand Down Expand Up @@ -352,28 +348,6 @@ window.germanized = window.germanized || {};
$( '#woocommerce_gzd_alternative_complaints_text_' + val ).parents( 'tr' ).show();
},

onInputToogleClick: function() {
var $toggle = $( this ).find( 'span.woocommerce-gzd-input-toggle' ),
$row = $toggle.parents( 'fieldset' ),
$checkbox = $row.find( 'input[type=checkbox]' ),
$enabled = $toggle.hasClass( 'woocommerce-input-toggle--enabled' );

$toggle.removeClass( 'woocommerce-input-toggle--enabled' );
$toggle.removeClass( 'woocommerce-input-toggle--disabled' );

if ( $enabled ) {
$checkbox.prop( 'checked', false );
$toggle.addClass( 'woocommerce-input-toggle--disabled' );
} else {
$checkbox.prop( 'checked', true );
$toggle.addClass( 'woocommerce-input-toggle--enabled' );
}

$checkbox.trigger( 'change' );

return false;
},

initMailSortable: function() {
if ( $( '#woocommerce_gzd_mail_attach_imprint' ).length > 0 ) {
var table = $( '#woocommerce_gzd_mail_attach_imprint' ).parents( 'table' );
Expand Down
46 changes: 46 additions & 0 deletions assets/js/static/admin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*global woocommerce_admin_meta_boxes, woocommerce_admin, accounting, woocommerce_admin_meta_boxes_order */
window.germanized = window.germanized || {};

( function( $, germanized ) {

/**
* Order Data Panel
*/
germanized.admin = {
params: {},

init: function() {
var self = this;
this.params = wc_gzd_admin_params;

$( document ).on( 'click', 'a.woocommerce-gzd-input-toggle-trigger', this.onInputToogleClick );
},

onInputToogleClick: function() {
var $toggle = $( this ).find( 'span.woocommerce-gzd-input-toggle' ),
$row = $toggle.parents( 'fieldset' ),
$checkbox = $row.find( 'input[type=checkbox]' ).length > 0 ? $row.find( 'input[type=checkbox]' ) : $toggle.parent().nextAll( 'input[type=checkbox]:first' ),
$enabled = $toggle.hasClass( 'woocommerce-input-toggle--enabled' );

$toggle.removeClass( 'woocommerce-input-toggle--enabled' );
$toggle.removeClass( 'woocommerce-input-toggle--disabled' );

if ( $enabled ) {
$checkbox.prop( 'checked', false );
$toggle.addClass( 'woocommerce-input-toggle--disabled' );
} else {
$checkbox.prop( 'checked', true );
$toggle.addClass( 'woocommerce-input-toggle--enabled' );
}

$checkbox.trigger( 'change' );

return false;
}
};

$( document ).ready( function() {
germanized.admin.init();
});

})( jQuery, window.germanized );
86 changes: 86 additions & 0 deletions includes/admin/class-wc-gzd-admin-order.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public static function instance() {
}

public function __construct() {
add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ), 35 );
add_action( 'woocommerce_process_shop_order_meta', array( $this, 'save_editable_checkboxes' ), 45 );

if ( wc_gzd_enable_additional_costs_split_tax_calculation() || wc_gzd_calculate_additional_costs_taxes_based_on_main_service() ) {
add_action(
'woocommerce_order_item_shipping_after_calculate_taxes',
Expand Down Expand Up @@ -117,6 +120,89 @@ public function __construct() {
}
}

public function get_order_screen_id() {
return function_exists( 'wc_get_page_screen_id' ) ? wc_get_page_screen_id( 'shop-order' ) : 'shop_order';
}

protected function init_order_object( $post ) {
if ( is_callable( array( '\Automattic\WooCommerce\Utilities\OrderUtil', 'init_theorder_object' ) ) ) {
\Automattic\WooCommerce\Utilities\OrderUtil::init_theorder_object( $post );
} else {
global $post, $thepostid, $theorder;

if ( ! is_int( $thepostid ) ) {
$thepostid = $post->ID;
}

if ( ! is_object( $theorder ) ) {
$theorder = wc_get_order( $thepostid );
}
}
}

public function save_editable_checkboxes( $order_id ) {
$checkboxes = WC_GZD_Legal_Checkbox_Manager::instance()->get_editable_checkboxes( 'order' );
$visible = isset( $_POST['_checkboxes_visible'] ) ? (array) wc_clean( wp_unslash( $_POST['_checkboxes_visible'] ) ) : array(); // phpcs:ignore WordPress.Security.NonceVerification.Missing

if ( $order = wc_get_order( $order_id ) ) {
$updated = false;
if ( ! empty( $checkboxes ) ) {
foreach ( $checkboxes as $checkbox_id ) {
if ( ! in_array( $checkbox_id, $visible, true ) ) {
continue;
}

$is_checked = isset( $_POST[ "_checkbox_{$checkbox_id}" ] ) ? wc_string_to_bool( wc_clean( wp_unslash( $_POST[ "_checkbox_{$checkbox_id}" ] ) ) ) : false; // phpcs:ignore WordPress.Security.NonceVerification.Missing

if ( 'parcel_delivery' === $checkbox_id ) {
$order->update_meta_data( '_parcel_delivery_opted_in', wc_bool_to_string( $is_checked ) );
} elseif ( 'photovoltaic_systems' === $checkbox_id ) {
$order->update_meta_data( '_photovoltaic_systems_opted_in', wc_bool_to_string( $is_checked ) );
} else {
$order->update_meta_data( "_checkbox_{$checkbox_id}", wc_bool_to_string( $is_checked ) );
}

$updated = true;
}
}

if ( $updated ) {
$order->save();
}
}
}

public function add_meta_boxes() {
$order_type_screen_ids = array_merge( wc_get_order_types( 'order-meta-boxes' ), array( $this->get_order_screen_id() ) );

// Orders.
foreach ( $order_type_screen_ids as $type ) {
add_meta_box(
'woocommerce-gzd-order-checkboxes',
__( 'Checkboxes', 'woocommerce-germanized' ),
function( $post ) {
global $theorder;
$this->init_order_object( $post );
$order = $theorder;

$this->render_checkboxes_meta_box( $order );
},
$type,
'side',
'default'
);
}
}

/**
* @param WC_Order $order
*
* @return void
*/
protected function render_checkboxes_meta_box( $order ) {
WC_GZD_Legal_Checkbox_Manager::instance()->render_checkbox_log( $order, 'order' );
}

/**
* @param WC_Order_Item $item
*
Expand Down
Loading

0 comments on commit bef0591

Please sign in to comment.