Skip to content

Instantly share code, notes, and snippets.

@TanvirHasan19
Created January 13, 2026 02:28
Show Gist options
  • Select an option

  • Save TanvirHasan19/e0c6dc08866bac997b3ff95f4769ded3 to your computer and use it in GitHub Desktop.

Select an option

Save TanvirHasan19/e0c6dc08866bac997b3ff95f4769ded3 to your computer and use it in GitHub Desktop.
Fix filter priorities to ensure custom fields load before data manipulation
/**
* Fix filter priorities to ensure custom fields load before data manipulation
*/
add_action( 'plugins_loaded', function() {
// Only run if Product Feed Elite classes exist
if ( ! class_exists( '\AdTribes\PFE\Classes\Extra_Attributes' ) ||
! class_exists( '\AdTribes\PFE\Classes\Product_Data_Manipulation' ) ) {
return;
}
// Remove existing filters
remove_filter( 'adt_get_product_data', array(
\AdTribes\PFE\Classes\Extra_Attributes::instance(),
'add_extra_attributes_to_product_data'
), 10 );
remove_filter( 'adt_get_product_data', array(
\AdTribes\PFE\Classes\Product_Data_Manipulation::instance(),
'process_product_data_manipulation'
), 10 );
// Re-add with correct priorities
// Custom fields load first (priority 5)
add_filter( 'adt_get_product_data', array(
\AdTribes\PFE\Classes\Extra_Attributes::instance(),
'add_extra_attributes_to_product_data'
), 5, 3 );
// Data manipulation runs after custom fields (priority 15)
add_filter( 'adt_get_product_data', array(
\AdTribes\PFE\Classes\Product_Data_Manipulation::instance(),
'process_product_data_manipulation'
), 15, 3 );
}, 20 );
/**
* Enhance data manipulation to fetch custom field values from product meta
* This ensures custom fields are available even if filter order is wrong
*/
add_filter( 'adt_get_product_data', function( $product_data, $feed, $product ) {
// Only process if data manipulation is enabled
$product_data_manipulation_setting = get_option( 'adt_enable_data_manipulation_support' );
if ( 'yes' !== $product_data_manipulation_setting ) {
return $product_data;
}
// Check if feed has field manipulation rules
if ( ! is_object( $feed ) || ! method_exists( $feed, 'field_manipulation' ) ) {
return $product_data;
}
$field_manipulation = $feed->field_manipulation;
if ( empty( $field_manipulation ) || empty( $product_data ) || ! is_object( $product ) ) {
return $product_data;
}
// Collect all custom field attributes that might be needed
$needed_custom_fields = array();
foreach ( $field_manipulation as $manipulation ) {
if ( ! isset( $manipulation['becomes'] ) || ! is_array( $manipulation['becomes'] ) ) {
continue;
}
foreach ( $manipulation['becomes'] as $become ) {
$attribute = $become['attribute'] ?? '';
if ( empty( $attribute ) ) {
continue;
}
// Check if this is a custom field attribute that's missing from product_data
if ( strpos( $attribute, 'custom_attributes_' ) === 0 ) {
if ( ! isset( $product_data[ $attribute ] ) || empty( $product_data[ $attribute ] ) ) {
$needed_custom_fields[] = $attribute;
}
}
}
}
// Fetch missing custom fields from product meta
if ( ! empty( $needed_custom_fields ) ) {
$extra_attributes = get_option( 'adt_extra_attributes', array() );
foreach ( $needed_custom_fields as $field_key ) {
// Skip if already in product_data
if ( isset( $product_data[ $field_key ] ) && ! empty( $product_data[ $field_key ] ) ) {
continue;
}
$meta_key = str_replace( 'custom_attributes_', '', $field_key );
// Check if this field is enabled in extra attributes
if ( ! isset( $extra_attributes[ $field_key ] ) ) {
continue;
}
// Get value from product meta
if ( $product->is_type( 'variation' ) ) {
// For variations, check variation first, then parent
$meta_exists = $product->meta_exists( $meta_key );
if ( $meta_exists ) {
$meta_value = $product->get_meta( $meta_key, true );
} else {
$parent_product = wc_get_product( $product->get_parent_id() );
if ( $parent_product ) {
$meta_value = $parent_product->get_meta( $meta_key, true );
} else {
$meta_value = '';
}
}
} else {
$meta_value = $product->get_meta( $meta_key, true );
}
// Add to product_data if value exists
if ( ! empty( $meta_value ) ) {
$product_data[ $field_key ] = apply_filters(
'adt_product_data_extra_attribute_value',
$meta_value,
$field_key,
$meta_key,
$product
);
}
}
}
return $product_data;
}, 8, 3 ); // Priority 8 ensures it runs before data manipulation (priority 15)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment