Tagged: woocommerce
-
AuthorPosts
-
October 14, 2023 at 12:09 pm #1422461
Hi Mike, I would need a new “small” adjustment, if possible.
In cases where a book has more than one author, the names appear as “inline-block” in the single product (see attached image).
Would it be possible to obtain the same arrangement of the author’s names also in the “product grid” too? (as shown in the lower part of the image).PS: If necessary, all indications on Private Content relating to the “stage site” are always valid.
Best regards
October 15, 2023 at 10:01 pm #1422597Hey Oriano,
Thanks for your patience and the link to your site, as I understand your request, you want the author names in the grid to show as inline so they are not on multiple lines, this can be achieved with this css:.pa_autore p { display: inline; }
I added this for you:
The trouble is adding the commas between the names, but not if there is only one name, or after the last name, but I believe that I achieved this:
Please clear your browser cache and check.
This is the full updated code://Display_custom_attribute_in_product_grid add_action('woocommerce_after_shop_loop_item_title', 'display_custom_attribute_in_product_grid', 5); function display_custom_attribute_in_product_grid(){ // define the desired product attributes to be displayed $defined_attributes = array('autore'); global $product; $attributes = $product->get_attributes(); if ( ! $attributes ) { return; } $out = '<span class="product-attributes">'; foreach ( $attributes as $attribute ) { $attribute_slug = str_replace( 'pa_', '', $attribute->get_name() ); if ( ! in_array($attribute_slug, $defined_attributes) ) { continue; } if ( $attribute->get_variation() ) { continue; } $name = $attribute->get_name(); if ( $attribute->is_taxonomy() ) { $terms = wp_get_post_terms( $product->get_id(), $name, 'all' ); $tax = $terms[0]->taxonomy; $tax_object = get_taxonomy($tax); if ( isset ( $tax_object->labels->singular_name ) ) { $tax_label = $tax_object->labels->singular_name; } elseif ( isset( $tax_object->label ) ) { $tax_label = $tax_object->label; if ( 0 === strpos( $tax_label, 'Product ' ) ) { $tax_label = substr( $tax_label, 8 ); } } $out .= '<span class="' . esc_attr( $name ) . '"style="width:100%" >'; $tax_terms = array(); foreach ( $terms as $term ) { $single_term = array('name' => esc_html( $term->name ), 'url' => get_term_link($term->term_id, $name)); array_push( $tax_terms, $single_term ); } $author_names = array(); foreach ($tax_terms as $term) { $author_names[] = $term['name']; } $author_count = count($author_names); if ($author_count > 1) { $author_list = implode(', ', array_slice($author_names, 0, -1)) . ', ' . end($author_names); } else { $author_list = $author_names[0]; } $out .= '<p data-url="' . $term['url'] . '" class="attribute-value" style="margin:0">' . $author_list . '</p>'; } else { $value_string = implode( ', ', $attribute->get_options() ); $out .= '<span class="' . sanitize_title($name) . ' ' . sanitize_title( $value_string ) . '">'; } } $out .= '</span>'; echo $out; }
Best regards,
MikeOctober 16, 2023 at 8:24 am #1422620Good morning Mike, and thanks for your help.
Your “function” aligns the author names, but also aligns the “price” :(
Is it possible to have only the authors’ names aligned, and the price instead continue to be on a new line?
Best regards,
OrianoOctober 16, 2023 at 11:56 am #1422641Hi,
I added this css:#top .product-attributes .price { display: block; }
Please clear your browser cache and check.
Best regards,
MikeOctober 18, 2023 at 3:59 pm #1422908Hi Mike,
I have now found an error, which I don’t know if it is caused by the last intervention made, or if it was an error that was also present before this last change.When a product/book has more than one author, for example “Author #1, Author #2, Author #3, in the single product the links of the three author names correctly refer to the respective pages of the two authors.
However, when the same product is displayed within the product grid, the links of the three authors (Author #1, Author #2, Author #3) are all redirected to the page of the last author (Author #3) :(See the details in the Private Content.
Best regards
OrianoOctober 21, 2023 at 7:39 pm #1423253Hi,
I believe the new modification is working correctly, please check.
This is the new code:add_action('woocommerce_after_shop_loop_item_title', 'display_custom_attribute_in_product_grid', 5); function display_custom_attribute_in_product_grid() { // define the desired product attributes to be displayed $defined_attributes = array('autore'); global $product; $attributes = $product->get_attributes(); if (! $attributes) { return; } $out = '<span class="product-attributes">'; foreach ($attributes as $attribute) { $attribute_slug = str_replace('pa_', '', $attribute->get_name()); if (! in_array($attribute_slug, $defined_attributes)) { continue; } if ($attribute->get_variation()) { continue; } $name = $attribute->get_name(); if ($attribute->is_taxonomy()) { $terms = wp_get_post_terms($product->get_id(), $name, 'all'); $tax = $terms[0]->taxonomy; $tax_object = get_taxonomy($tax); if (isset($tax_object->labels->singular_name)) { $tax_label = $tax_object->labels->singular_name; } elseif (isset($tax_object->label)) { $tax_label = $tax_object->label; if (0 === strpos($tax_label, 'Product ')) { $tax_label = substr($tax_label, 8); } } $out .= '<span class="' . esc_attr($name) . '" style="width:100%" >'; $tax_terms = array(); foreach ($terms as $term) { $single_term = array('name' => esc_html($term->name), 'url' => get_term_link($term->term_id, $name)); array_push($tax_terms, $single_term); } $author_names = array(); foreach ($tax_terms as $term) { $author_names[] = '<p data-url="' . $term['url'] . '" class="attribute-value" style="margin:0">' . $term['name'] . '</p>'; } // Combine author names into a single string $author_list = implode(', ', $author_names); $out .= $author_list; } else { $value_string = implode(', ', $attribute->get_options()); $out .= '<span class="' . sanitize_title($name) . ' ' . sanitize_title($value_string) . '">'; } } $out .= '</span>'; echo $out; }
Best regards,
MikeOctober 22, 2023 at 9:10 am #1423281Hi Mike,
Would it be possible, in the product grid, not to have the product link on the comma (between one author and another)?Best regards
OrianoOctober 22, 2023 at 12:14 pm #1423298Hi,
The commas are not in the author links:
but please note that the entire product grid item is wrapped in a link to the product page:
What is your goal, do you want to change the color of the commas?
Try this CSS in your Enfold Theme Options ▸ General Styling ▸ Quick CSS field:.inner_product.main_color a.woocommerce-LoopProduct-link { color: #7d7d7d; }
Best regards,
MikeOctober 22, 2023 at 12:22 pm #1423304Hello Mike, commas have product link.
The ideal would be for the commas not to contain links (as happens on the individual product page),
to avoid possible confusion.
It would be possible?Best regards,
OrianoOctober 22, 2023 at 12:57 pm #1423311October 22, 2023 at 1:59 pm #1423315Hi Mike,
Yes, I see.
I was just hoping there was a solution to mask the product link under the “comma” character.
The sequence of <link Author1><“, ” link product,><link Author 2><“, ” link product><link Author 3 etc…> risks creating a bit of confusion among users.However, I have now used your CSS to give the comma the same color as the authors’ names, so that the comma doesn’t attract too much attention.
Tomorrow I will insert the functions and CSS used on the stage site, on the production site, to see how it behaves.Best regards,
OrianoOctober 22, 2023 at 7:24 pm #1423332 -
AuthorPosts
- You must be logged in to reply to this topic.