Skip to content

Commit

Permalink
Support manufacturer bulk + quick edit.
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisnissle committed Oct 2, 2024
1 parent eaedda7 commit f572e66
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 25 deletions.
10 changes: 9 additions & 1 deletion assets/js/static/admin-product.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,22 +120,29 @@ jQuery( function ( $ ) {
if ( $inline_data.find( '.gzd_delivery_time_slug' ).length > 0 ) {
var delivery_time = $inline_data.find( '.gzd_delivery_time_slug' ).text(),
delivery_time_name = $inline_data.find( '.gzd_delivery_time_name' ).text(),
manufacturer = $inline_data.find( '.gzd_manufacturer_slug' ).text(),
manufacturer_name = $inline_data.find( '.gzd_manufacturer_name' ).text(),
unit = $inline_data.find( '.gzd_unit_slug' ).text();

$( 'select[name="_unit"] option:selected', '.inline-edit-row' ).attr( 'selected', false ).trigger( 'change' );
$( 'select[name="_unit"] option[value="' + unit + '"]' ).attr( 'selected', 'selected' ).trigger( 'change' );

$( 'select[name="_delivery_time"] option' ).remove().trigger( 'change' );
$( 'select[name="_manufacturer"] option' ).remove().trigger( 'change' );

if ( delivery_time ) {
$( 'select[name="_delivery_time"]' ).append( '<option value="' + delivery_time + '" selected="selected">' + delivery_time_name + '</option>' );
}

if ( manufacturer ) {
$( 'select[name="_manufacturer"]' ).append( '<option value="' + manufacturer + '" selected="selected">' + manufacturer_name + '</option>' );
}

/**
* Ugly hack to make sure select2 initialization happens after WP cloned the data to the new div
*/
setTimeout( function() {
var $select2 = $( 'tr#edit-' + post_id + ' .wc-gzd-delivery-time-search.enhanced' );
var $select2 = $( 'tr#edit-' + post_id + ' .wc-gzd-term-search-quick-edit.enhanced' );

/**
* Destroy the select2 element from template in case it still exists and has been initialized
Expand All @@ -146,6 +153,7 @@ jQuery( function ( $ ) {
}

$( 'tr#edit-' + post_id + ' .wc-gzd-delivery-time-select-placeholder' ).addClass( 'wc-product-search', 'wc-gzd-delivery-time-search' ).removeClass( 'wc-gzd-delivery-time-select-placeholder' );
$( 'tr#edit-' + post_id + ' .wc-gzd-manufacturer-select-placeholder' ).addClass( 'wc-product-search', 'wc-gzd-manufacturer-search' ).removeClass( 'wc-gzd-manufacturer-select-placeholder' );

$( document.body ).trigger( 'wc-enhanced-select-init' );
}, 100 );
Expand Down
123 changes: 106 additions & 17 deletions includes/admin/meta-boxes/class-wc-germanized-meta-box-product-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ public static function register_product_tab( $tabs ) {
public static function quick_edit_save( $product ) {
if ( $gzd_product = wc_gzd_get_gzd_product( $product ) ) {
$delivery_time = isset( $_REQUEST['_delivery_time'] ) ? wc_clean( wp_unslash( $_REQUEST['_delivery_time'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$manufacturer = isset( $_REQUEST['_manufacturer'] ) ? wc_clean( wp_unslash( $_REQUEST['_manufacturer'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended

if ( ! empty( $delivery_time ) ) {
$needs_update = true;
Expand All @@ -148,6 +149,17 @@ public static function quick_edit_save( $product ) {
$gzd_product->set_default_delivery_time_slug( '' );
}

if ( ! empty( $manufacturer ) ) {
$needs_update = true;

if ( $slug = wc_gzd_get_or_create_product_term_slug( $manufacturer, 'product_manufacturer' ) ) {
$gzd_product->set_manufacturer_slug( $slug );
}
} else {
$needs_update = true;
$gzd_product->set_manufacturer_slug( '' );
}

if ( isset( $_REQUEST['_unit'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$unit = wc_clean( wp_unslash( $_REQUEST['_unit'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended

Expand All @@ -173,10 +185,13 @@ public static function quick_edit_data( $post, $post_type_object ) {
if ( 'product' === $post_type_object->name ) {
if ( $gzd_product = wc_gzd_get_product( $post ) ) {
$default_delivery_time = $gzd_product->get_default_delivery_time( 'edit' );
$manufacturer = $gzd_product->get_manufacturer( 'edit' );

echo '
<div class="gzd_delivery_time_slug">' . esc_html( $default_delivery_time ? $default_delivery_time->slug : '' ) . '</div>
<div class="gzd_delivery_time_name">' . esc_html( $default_delivery_time ? $default_delivery_time->name : '' ) . '</div>
<div class="gzd_manufacturer_slug">' . esc_html( $manufacturer ? $manufacturer->get_slug() : '' ) . '</div>
<div class="gzd_manufacturer_name">' . esc_html( $manufacturer ? $manufacturer->get_name() : '' ) . '</div>
<div class="gzd_unit_slug">' . esc_html( $gzd_product->get_unit( 'edit' ) ) . '</div>
';
}
Expand All @@ -189,11 +204,29 @@ public static function quick_edit() {
<label class="gzd_delivery_time_field">
<span class="title"><?php esc_html_e( 'Delivery Time', 'woocommerce-germanized' ); ?></span>
<span class="input-text-wrap">
<select class="wc-gzd-delivery-time-select-placeholder" style="width: 100%; min-width: 150px;" name="_delivery_time"
data-minimum_input_length="1" data-allow_clear="true"
data-placeholder="<?php echo esc_attr( __( 'Search for a delivery time&hellip;', 'woocommerce-germanized' ) ); ?>"
data-action="woocommerce_gzd_json_search_delivery_time" data-multiple="false">
</select>
<?php
self::output_delivery_time_select2(
array(
'name' => '_delivery_time',
'style' => 'width: 100%; min-width: 150px;',
'classes' => 'wc-gzd-term-search-quick-edit wc-gzd-delivery-time-select-placeholder',
)
);
?>
</span>
</label>
<label class="gzd_manufacturer_field">
<span class="title"><?php esc_html_e( 'Manufacturer', 'woocommerce-germanized' ); ?></span>
<span class="input-text-wrap">
<?php
self::manufacturer_select_field(
array(
'name' => '_manufacturer',
'style' => 'width: 100%; min-width: 150px;',
'classes' => 'wc-gzd-term-search-quick-edit wc-gzd-manufacturer-select-placeholder',
)
);
?>
</span>
</label>
<label class="gzd_unit_field">
Expand All @@ -217,6 +250,7 @@ public static function bulk_save( $product ) {
if ( $gzd_product = wc_gzd_get_gzd_product( $product ) ) {
$needs_update = false;
$change_delivery_time = isset( $_REQUEST['change_delivery_time'] ) ? absint( $_REQUEST['change_delivery_time'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$change_manufacturer = isset( $_REQUEST['change_manufacturer'] ) ? absint( $_REQUEST['change_manufacturer'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended

if ( ! empty( $change_delivery_time ) && in_array( $change_delivery_time, array( 1, 2 ) ) ) { // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
$delivery_time = isset( $_REQUEST['_delivery_time'] ) ? wc_clean( wp_unslash( $_REQUEST['_delivery_time'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
Expand All @@ -234,6 +268,22 @@ public static function bulk_save( $product ) {
}
}

if ( ! empty( $change_manufacturer ) && in_array( $change_manufacturer, array( 1, 2 ) ) ) { // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
$manufacturer_slug = isset( $_REQUEST['_manufacturer'] ) ? wc_clean( wp_unslash( $_REQUEST['_manufacturer'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended

if ( ! empty( $manufacturer_slug ) && 1 === $change_manufacturer ) {
$needs_update = true;

if ( $slug = wc_gzd_get_or_create_product_term_slug( $manufacturer_slug, 'product_manufacturer' ) ) {
$gzd_product->set_manufacturer_slug( $slug );
}
} elseif ( 2 === $change_manufacturer ) {
$needs_update = true;

$gzd_product->set_manufacturer_slug( '' );
}
}

if ( isset( $_REQUEST['_unit'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$unit = wc_clean( wp_unslash( $_REQUEST['_unit'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended

Expand Down Expand Up @@ -276,11 +326,45 @@ public static function bulk_edit() {
</span>
</label>
<label class="change-input">
<select class="wc-product-search wc-gzd-delivery-time-search" style="width: 100%; min-width: 150px;" name="_delivery_time"
data-minimum_input_length="1" data-allow_clear="true"
data-placeholder="<?php echo esc_attr( __( '— No change —', 'woocommerce-germanized' ) ); ?>"
data-action="woocommerce_gzd_json_search_delivery_time" data-multiple="false">
</select>
<?php
self::output_delivery_time_select2(
array(
'name' => '_delivery_time',
'placeholder' => __( '— No change —', 'woocommerce-germanized' ),
'style' => 'width: 100%; min-width: 150px;',
)
);
?>
</label>
</div>
<div class="inline-edit-group manufacturer">
<label class="alignleft">
<span class="title"><?php esc_html_e( 'Manufacturer', 'woocommerce-germanized' ); ?></span>
<span class="input-text-wrap">
<select class="change_manufacturer change_to" name="change_manufacturer">
<?php
$options = array(
'' => __( '— No change —', 'woocommerce-germanized' ),
'1' => __( 'Change to:', 'woocommerce-germanized' ),
'2' => __( 'No manufacturer', 'woocommerce-germanized' ),
);
foreach ( $options as $key => $value ) {
echo '<option value="' . esc_attr( $key ) . '">' . esc_html( $value ) . '</option>';
}
?>
</select>
</span>
</label>
<label class="change-input">
<?php
self::manufacturer_select_field(
array(
'name' => '_manufacturer',
'placeholder' => __( '— No change —', 'woocommerce-germanized' ),
'style' => 'width: 100%; min-width: 150px;',
)
);
?>
</label>
</div>
<label class="alignleft">
Expand Down Expand Up @@ -623,14 +707,15 @@ public static function manufacturer_select_field( $args = array() ) {
'name' => '_manufacturer_slug',
'placeholder' => __( 'Search for a manufacturer&hellip;', 'woocommerce-germanized' ),
'term' => false,
'classes' => 'wc-product-search wc-gzd-manufacturer-search',
'id' => '',
'style' => 'width: 50%',
)
);
$args['id'] = empty( $args['id'] ) ? $args['name'] : $args['id'];
?>
<select
class="wc-product-search wc-gzd-manufacturer-search" style="<?php echo esc_attr( $args['style'] ); ?>"
class="<?php echo esc_attr( $args['classes'] ); ?>" style="<?php echo esc_attr( $args['style'] ); ?>"
id="<?php echo esc_attr( $args['id'] ); ?>" name="<?php echo esc_attr( $args['name'] ); ?>"
data-minimum_input_length="1" data-allow_clear="true"
data-placeholder="<?php echo esc_attr( $args['placeholder'] ); ?>"
Expand Down Expand Up @@ -710,6 +795,7 @@ public static function output_delivery_time_select2( $args = array() ) {
'name' => 'delivery_time',
'placeholder' => __( 'Search for a delivery time&hellip;', 'woocommerce-germanized' ),
'term' => false,
'classes' => 'wc-product-search wc-gzd-delivery-time-search',
'id' => '',
'style' => 'width: 50%',
)
Expand All @@ -718,7 +804,7 @@ public static function output_delivery_time_select2( $args = array() ) {
$args['id'] = empty( $args['id'] ) ? $args['name'] : $args['id'];
?>
<select
class="wc-product-search wc-gzd-delivery-time-search" style="<?php echo esc_attr( $args['style'] ); ?>"
class="<?php echo esc_attr( $args['classes'] ); ?>" style="<?php echo esc_attr( $args['style'] ); ?>"
id="<?php echo esc_attr( $args['id'] ); ?>" name="<?php echo esc_attr( $args['name'] ); ?>"
data-minimum_input_length="1" data-allow_clear="true"
data-placeholder="<?php echo esc_attr( $args['placeholder'] ); ?>"
Expand Down Expand Up @@ -1245,11 +1331,14 @@ public static function save_product_data( &$product, $data, $is_variation = fals
} else {
$term = wc_clean( $data[ $term_select ] );

/**
* Convert term ids to slugs
*/
if ( is_numeric( $term ) ) {
$term_data = get_term_by( 'id', absint( $term ), $taxonomy );
if ( 'product_manufacturer' === $taxonomy ) {
$term = wc_gzd_get_or_create_product_term_slug( $term, 'product_manufacturer' );

if ( ! $term ) {
$term = '';
}
} elseif ( is_numeric( $term ) ) {
$term_data = get_term_by( 'id', absint( $term ), $taxonomy );

if ( ! is_wp_error( $term_data ) ) {
$term = $term_data->slug;
Expand Down
23 changes: 16 additions & 7 deletions includes/wc-gzd-product-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,12 +349,12 @@ function wc_gzd_recalculate_unit_price( $args = array(), $product = false ) {
*
* @return array|string|boolean
*/
function wc_gzd_get_valid_product_delivery_time_slugs( $maybe_slug, $allow_add_new = true ) {
function wc_gzd_get_or_create_product_term_slug( $maybe_slug, $taxonomy, $allow_add_new = true ) {
if ( is_array( $maybe_slug ) ) {
return array_filter(
array_map(
function ( $maybe_slug ) use ( $allow_add_new ) {
return wc_gzd_get_valid_product_delivery_time_slugs( $maybe_slug, $allow_add_new );
function ( $maybe_slug ) use ( $allow_add_new, $taxonomy ) {
return wc_gzd_get_or_create_product_term_slug( $maybe_slug, $taxonomy, $allow_add_new );
},
$maybe_slug
),
Expand All @@ -368,7 +368,7 @@ function ( $x ) {
if ( is_a( $maybe_slug, 'WP_Term' ) ) {
$slug = $maybe_slug->slug;
} elseif ( is_numeric( $maybe_slug ) ) {
$term = get_term_by( 'term_id', $maybe_slug, 'product_delivery_time' );
$term = get_term_by( 'term_id', $maybe_slug, $taxonomy );

if ( $term ) {
$slug = $term->slug;
Expand All @@ -377,16 +377,16 @@ function ( $x ) {

if ( ! $slug ) {
$possible_name = $maybe_slug;
$term = get_term_by( 'slug', sanitize_title( $possible_name ), 'product_delivery_time' );
$term = get_term_by( 'slug', sanitize_title( $possible_name ), $taxonomy );

if ( ! $term ) {
$slug = false;

if ( $allow_add_new ) {
$term_details = wp_insert_term( $possible_name, 'product_delivery_time' );
$term_details = wp_insert_term( $possible_name, $taxonomy );

if ( ! is_wp_error( $term_details ) ) {
if ( $term = get_term_by( 'id', $term_details['term_id'], 'product_delivery_time' ) ) {
if ( $term = get_term_by( 'id', $term_details['term_id'], $taxonomy ) ) {
$slug = $term->slug;
}
}
Expand All @@ -400,6 +400,15 @@ function ( $x ) {
}
}

/**
* @param $maybe_slug
*
* @return array|string|boolean
*/
function wc_gzd_get_valid_product_delivery_time_slugs( $maybe_slug, $allow_add_new = true ) {
return wc_gzd_get_or_create_product_term_slug( $maybe_slug, 'product_delivery_time', $allow_add_new );
}

function wc_gzd_product_review_is_verified( $comment_id ) {
return apply_filters( 'woocommerce_gzd_product_review_is_verified', wc_review_is_from_verified_owner( $comment_id ), $comment_id );
}
Expand Down

0 comments on commit f572e66

Please sign in to comment.