I have tried many different approaches from native Bricks dynamic data to Bricks Ultimate and PHP coding (not very good at it) to build a Woocommerce cart with customized content - unfortunately all trials without success. Here is what I would like to achieve:
I assume you mean the latest version of Bricks Ultimate? I have both the latest version of BU (1.7.11) and Bricks (1.11.1) and added the following dynamic data tags:
width β {cf_attribute_breite} and {bu_product_dimensions_width}
length β {cf_attribute_laenge} and {bu_product_dimensions_length}
color β {cf_attribute_produktfarbe}
unit β {cf_attribute_lieferform}
None of these provide the desired result. In fact, they are not outputting anything.
I also found the tag bu_product_atts which outputs an array of attributes but since I cannot find any documentation on this tag, I donβt know how to filter it properly. Could you please help with this?
ChatGPT brought me to the solution. Add a code element to the cart loop with the following functions:
<?php
// Ensure WooCommerce functions are available
if (function_exists('WC')) {
// Get the current product ID
$product_id = get_the_ID();
// Get the product object
$product = wc_get_product($product_id);
if ($product) {
// Check if the product is in the cart
foreach (WC()->cart->get_cart() as $cart_item) {
if ($cart_item['product_id'] == $product_id || $cart_item['variation_id'] == $product_id) {
// Handle variable products
if ($product->is_type('variable')) {
$selected_value = $cart_item['variation']['attribute_pa_width'] ?? null; // Replace 'pa_color' with your attribute slug
if ($selected_value) {
// Get the term name from the selected attribute slug
$term = get_term_by('slug', $selected_value, 'pa_width'); // Replace 'pa_color' with your taxonomy
if ($term && !is_wp_error($term)) {
echo esc_html($term->name);
} else {
echo 'Selected value not found.';
}
} else {
echo 'No attribute value selected.';
}
}
// Handle simple products
else {
// Define the specific attribute you want to display (e.g., 'pa_color')
$taxonomy = 'pa_width'; // Replace with your taxonomy slug
// Check if the product has terms for this taxonomy
$terms = wp_get_post_terms($product_id, $taxonomy);
if (!empty($terms) && !is_wp_error($terms)) {
// Output the name of the first term found
echo esc_html($terms[0]->name);
}
// Fallback: Check for custom attributes in product meta
else {
$attributes = $product->get_attributes();
if (!empty($attributes) && isset($attributes[$taxonomy])) {
$attribute = $attributes[$taxonomy];
// Check if it is a custom (non-taxonomy) attribute
if (!$attribute->is_taxonomy()) {
$value = implode(', ', $attribute->get_options());
echo esc_html($value);
} else {
echo 'No terms found for this taxonomy.';
}
} else {
echo 'No attributes found for this product.';
}
}
}
// Exit the loop once the relevant cart item is found
break;
}
}
} else {
echo 'Product not found.';
}
} else {
echo 'WooCommerce is not active.';
}
?>
Change pa_width to your desired custom WooCommerce attribute (3x) whilte pa_ remains unchanged and width needs to be your custom attribute slug. It works both for simple products and variable products.
The above code does not account for different values of the same product in the cart. It will display only the variation that has been selected the first time. For all other selections, the first value is shown.
Example: First selection: Width 30 mm & Color yellow β width 30 mm & color yellow in the cart Second selection: Width 50 mm & Color white β width 30 mm & color yellow in the cart