Skip to content

Instantly share code, notes, and snippets.

@Auke1810
Created December 15, 2023 19:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Auke1810/bd0c82c7b08ab5a657189dbcb03c03db to your computer and use it in GitHub Desktop.
Save Auke1810/bd0c82c7b08ab5a657189dbcb03c03db to your computer and use it in GitHub Desktop.
Adding the WooCommerce Product Feed Manager Unique Identifier fields to the Quick Edit screen.
<?php
// These functions will be added natively to our product feed manager plugin from version 3.2.0
function wppfm_show_quick_edit_custom_fields() {
if ( function_exists('wppfm_create_gtin_wc_support_field' ) ) {
// Add the Brand field.
woocommerce_wp_text_input(
array(
'id' => 'wppfm_product_brand',
'label' => 'Product brand',
'class' => 'wppfm_product_brand',
)
);
// Add the GTIN field.
woocommerce_wp_text_input(
array(
'id' => 'wppfm_product_gtin',
'label' => 'Product GTIN',
'class' => 'wppfm_product_gtin',
)
);
// Add the MPN field.
woocommerce_wp_text_input(
array(
'id' => 'wppfm_product_mpn',
'label' => 'Product MPN',
'class' => 'wppfm_product_mpn',
)
);
}
}
add_action( 'woocommerce_product_quick_edit_start', 'wppfm_show_quick_edit_custom_fields' );
function wppfm_add_quick_edit_custom_fields_data( $column, $post_id ){
if ( function_exists('wppfm_create_gtin_wc_support_field' ) ) {
if ( 'name' !== $column ) {
return;
}
echo '<div id="wppfm_product_brand_data_' . $post_id . '" hidden>' . esc_html( get_post_meta( $post_id, 'wppfm_product_brand', true ) ) . '</div>
<div id="wppfm_product_gtin_data_' . $post_id . '" hidden>' . esc_html( get_post_meta( $post_id, 'wppfm_product_gtin', true ) ) . '</div>
<div id="wppfm_product_mpn_data_' . $post_id . '" hidden>' . esc_html( get_post_meta( $post_id, 'wppfm_product_mpn', true ) ) . '</div>';
}
}
add_action( 'manage_product_posts_custom_column', 'wppfm_add_quick_edit_custom_fields_data', 10, 2 );
function wppfm_populate_quick_edit_custom_fields() {
if ( function_exists( 'wppfm_create_gtin_wc_support_field' ) ) {
?>
<script type="text/javascript">
(function($) {
$('#the-list').on('click', '.editinline', function() {
var post_id = $(this).closest('tr').attr('id');
post_id = post_id.replace('post-', '');
var brand_field = $('#wppfm_product_brand_data_' + post_id).text();
$('input[name="wppfm_product_brand"]', '.inline-edit-row').val(brand_field);
var gtin_field = $('#wppfm_product_gtin_data_' + post_id).text();
$('input[name="wppfm_product_gtin"]', '.inline-edit-row').val(gtin_field);
var mpn_field = $('#wppfm_product_mpn_data_' + post_id).text();
$('input[name="wppfm_product_mpn"]', '.inline-edit-row').val(mpn_field);
});
})(jQuery); </script>
<?php
}
}
add_action( 'admin_footer', 'wppfm_populate_quick_edit_custom_fields' );
function wppfm_save_quick_edit_custom_fields( $product ) {
if ( function_exists('wppfm_create_gtin_wc_support_field' ) ) {
// Get the custom fields' data.
$brand = $_POST['wppfm_product_brand'] ?? '';
$gtin = $_POST['wppfm_product_gtin'] ?? '';
$mpn = $_POST['wppfm_product_mpn'] ?? '';
// Save the custom fields' data.
$product->update_meta_data( 'wppfm_product_brand', sanitize_text_field( $brand ) );
$product->update_meta_data( 'wppfm_product_gtin', sanitize_text_field( $gtin ) );
$product->update_meta_data( 'wppfm_product_mpn', sanitize_text_field( $mpn ) );
$product->save();
}
}
add_action( 'woocommerce_product_quick_edit_save', 'wppfm_save_quick_edit_custom_fields' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment