Home / Admin / Add Custom Attributes and Group Them
Duplicate Snippet

Embed Snippet on Your Site

Add Custom Attributes and Group Them

<10
Code Preview
php
<?php
// Step 1: Add your custom attribute(s) to a named group
function add_custom_grouped_attributes( $attributes ) {
    // Define your own group label here
    $group_label = 'Other fields';  // e.g., 'Image attributes', 'Custom data', etc.
    // Initialize group if not set
    if ( ! isset( $attributes[ $group_label ] ) ) {
        $attributes[ $group_label ] = array();
    }
    // Add your custom attribute(s)
    $attributes[ $group_label ]['custom_product_flag'] = 'Custom Product Flag';
    $attributes[ $group_label ]['custom_image_url'] = 'Custom Image URL';
    return $attributes;
}
add_filter( 'adt_product_feed_attributes', 'add_custom_grouped_attributes' );
// Step 2: Populate your custom attribute values for each product
function populate_custom_attribute_data( $product_data, $feed, $product ) {
    // Example: get custom meta or ACF field values
    $custom_flag = get_post_meta( $product->get_id(), 'custom_product_flag', true );
    $custom_image_url = get_post_meta( $product->get_id(), 'custom_image_url', true );
    // Assign values to the feed data array with the keys matching the attribute keys above
    $product_data['custom_product_flag'] = $custom_flag ?: '';
    $product_data['custom_image_url'] = $custom_image_url ?: '';
    return $product_data;
}
add_filter( 'adt_get_product_data', 'populate_custom_attribute_data', 10, 3 );

Comments

Add a Comment