I am using external image urls in Woocommerce with Bricks Builder.
Displaying variable product image already work on archive and shop page when not using a color filter. When using color filter i want to display external image of the product variation for that color. Is it possible to use a dynamic tag for displaying external variation image?
I have tried following filter but it doesn’t catch the color from GET_[“_color”]
add_filter('bricks/dynamic_data/render_content', 'render_my_tag', 20, 3);
add_filter('bricks/frontend/render_data', 'render_my_tag', 20, 2);
function render_my_tag($content, $post, $context = 'text') {
// Exit early if no dynamic tag is found
if (strpos($content, '{my_dd_tag}') === false && strpos($content, '{cart_image_tag}') === false) {
return $content;
}
// Ensure $post is valid before accessing its properties
if ($post instanceof WP_Post) {
$product_id = $post->ID;
log_error("Hello from product " . $product_id . get_post_type($product_id));
} else {
// Attempt to get the product ID in other ways if $post is null (e.g., on cart page)
$product_id = get_the_ID(); // Fallback to global post ID if available
if (is_cart() && isset(WC()->cart)) {
foreach (WC()->cart->get_cart() as $cart_item) {
$product_id = $cart_item['product_id'];
break; // Use the first product in the cart
}
}
// Additional fallback for WooCommerce cart page
}
$base_url = "https://images.pfconcept.com/ProductImages_All/JPG/500x500/";
// Check if a variation ID was found
if ($variation_id) {
$variation_image_url = $base_url . get_post_meta($variation_id, '_external_variation_main_image', true);
// Display the image
$image_html = '<img src="' . esc_url($variation_image_url) . '" alt="Product Image" style="max-width: 100px; height: auto;">';
$content = str_replace('{cart_image_tag}', $variation_image_url , $content);
} else {
// Optional: Display a fallback if no variation was found
$content = str_replace('{cart_image_tag}', 'No variation image available.', $content);
}
}
log_error("Hello from filter");
// Get selected color from the URL parameter
$selected_color = isset($_GET['_color']) ? sanitize_text_field($_GET['_color']) : '';
log_error("Selected color from filter: " . $selected_color);
// Only proceed if a color filter is detected
if (!empty($selected_color) && $post->post_type === 'product') {
$product_id = $post->ID;
// Check if the product is a variable product
if ($post->post_type === 'product' && $product_id) {
$product = wc_get_product($product_id);
if ($product->is_type('variable')) {
// Loop through variations to find the matching color
$available_variations = $product->get_available_variations();
foreach ($available_variations as $variation) {
$variation_id = $variation['variation_id'];
$variation_attributes = $variation['attributes'];
// Match the selected color with variation color attribute
if (isset($variation_attributes['attribute_pa_color']) && $variation_attributes['attribute_pa_color'] === $selected_color) {
// Get the external image URL for the variation
$external_image_url = get_post_meta($variation_id, '_external_variation_main_image', true);
if ($external_image_url) {
// Create the full image URL
$image_url = esc_url($base_url . $external_image_url);
$content = str_replace('{my_dd_tag}', $image_url, $content);
}
}
}
}
}
}
return $content;
}