Tagged: woocommerce
-
AuthorPosts
-
September 23, 2023 at 11:55 am #1420102
In this previous tickets ( Change Shop #3, Change Shop #2, Change Shop), we saw how to display the “Author” attribute (and others) under the woocommerce-product-details__short-description of single product page.
Now I would like, if possible, that the “Autore” attribute (Author) also appear in the Shop Page and in the products listed in the avia-product-slider-container, under the title.
As in this imageCould you please help me?
Best regards
OrianoSeptember 23, 2023 at 11:15 pm #1420157Hey Oriano,
I’m not sure if this is possible, but do you have a staging site with the other modifications active that we can test on?Best regards,
MikeSeptember 24, 2023 at 8:04 am #1420164Hi mike,
yes I have a staging site.
Find in Private content you account.September 24, 2023 at 7:40 pm #1420199Hi,
Thanks, I added this code to the end of your child theme functions.php file in Appearance ▸ Editor:add_action('woocommerce_after_shop_loop_item_title', 'display_custom_attribute_in_product_grid', 10); function display_custom_attribute_in_product_grid(){ // HERE define the desired product attributes to be displayed $defined_attributes = array('autore'); global $product; $attributes = $product->get_attributes(); if ( ! $attributes ) { return; } $out = '<ul class="taste-attributes">'; foreach ( $attributes as $attribute ) { // Get the product attribute slug from the taxonomy $attribute_slug = str_replace( 'pa_', '', $attribute->get_name() ); // skip all non desired product attributes if ( ! in_array($attribute_slug, $defined_attributes) ) { continue; } // skip variations if ( $attribute->get_variation() ) { continue; } $name = $attribute->get_name(); if ( $attribute->is_taxonomy() ) { $terms = wp_get_post_terms( $product->get_id(), $name, 'all' ); // get the taxonomy $tax = $terms[0]->taxonomy; // get the tax object $tax_object = get_taxonomy($tax); // get tax label if ( isset ( $tax_object->labels->singular_name ) ) { $tax_label = $tax_object->labels->singular_name; } elseif ( isset( $tax_object->label ) ) { $tax_label = $tax_object->label; // Trim label prefix since WC 3.0 if ( 0 === strpos( $tax_label, 'Product ' ) ) { $tax_label = substr( $tax_label, 8 ); } } $out .= '<li class="' . esc_attr( $name ) . '"style="width:100%" >'; $out .= '<span class="attribute-label">' . esc_html( $tax_label ) . ': </span> '; $tax_terms = array(); foreach ( $terms as $term ) { $single_term = esc_html( $term->name ); array_push( $tax_terms, $single_term ); } $out .= '<span class="attribute-value">' . implode(', ', $tax_terms) . '</span>'; } else { $value_string = implode( ', ', $attribute->get_options() ); $out .= '<li class="' . sanitize_title($name) . ' ' . sanitize_title( $value_string ) . '">'; $out .= '<span class="attribute-label">' . $name . ': </span> '; } } $out .= '</ul>'; echo $out; }
and now the author is showing in the product grid and in the Related products at the bottom of product pages:
Best regards,
MikeSeptember 24, 2023 at 10:08 pm #1420205Hi Mike,
Would it be possible to eliminate the word “Author”?
And also, would it be possible to move the author’s name below the title? (Between the title and the price).Best regards,
OrianoSeptember 24, 2023 at 10:16 pm #1420206Hello Mike,
and, importantly, the link of the author’s name should direct to the author page..
Example, the link on the author name “Raimondo Maria Pavarin” should open his author page:
https://bibliotecaclueb.it/clueb-stage/libreria/autore/pavarin-raimondo-maria/Best regards,
OrianoSeptember 24, 2023 at 10:48 pm #1420208September 25, 2023 at 8:13 am #1420226Good morning Mike,
and thanks for your help.I presented your solution to the “publishing house” in question, but the director of the publishing house insists on the request to have the author’s name linked to the author page (where all the volumes written by the author appear).
I told him it’s not possible, but he tells me that he saw a WordPress / WooCommerce site, which has what he’s looking for.
I asked him for the URL of this site, and he said he will give it to me.
In case it is of any use, as soon as I have the URL of this site I will write it here.Best regards,
OrianoSeptember 25, 2023 at 10:23 am #1420240Hi,
Thank you for the update.
We modified the display_custom_attribute_in_product_grid function a bit to get the term or autore link and to redirect to the author page, you have to add this script in the functions.php file.
function av_custom_inline_script() { // redirect to autore page wp_add_inline_script( 'jquery', " (function($) { $(document).ready(function() { $('.products .product a').on('click', function(e) { e.preventDefault(); var link = $(this).attr('href'); if(e.target.className.includes('attribute-value')) link = $(e.target).data('url') window.location.href = link; }); }); })(jQuery); " ); } add_action( 'wp_enqueue_scripts', 'av_custom_inline_script' );
Best regards,
IsmaelSeptember 25, 2023 at 10:38 am #1420241Hi Ismael,
I saw that the code “// redirect to autore page” was inserted in the child function, but it doesn’t seem to work, the link assigned to the author’s name always refers to the product and not to the author page :(Is there something I’m doing wrong?
Best regards,
OrianoSeptember 25, 2023 at 10:42 am #1420242Hi Ismale, sorry, you’re right, it works now.
I will do more tests and respond further to update the situation.Best regards, and thanks to all Enfold.
OrianoSeptember 25, 2023 at 10:47 am #1420243Sorry, maybe you are working on the stage site…
I’m signing out of my account.
OrianoSeptember 25, 2023 at 11:08 am #1420245Hi,
the director of the “publishing house” gave me the URL of the site that has the solution he was looking for. I put the sati in “private content”.Oriano
September 25, 2023 at 7:37 pm #1420325Hi,
Did Ismael’s solution work? I see you wrote that it did but I can’t see it?
The reason the other site can have a author link below the image is because only the image has the product link, but on your site the whole element has the product link.
I’m not sure how we can change that without a major element re-write.Best regards,
MikeOctober 5, 2023 at 10:55 am #1421467Hi Mike, sorry for my late reply, to see the stage site, see the info in Private Content.
The URL below the author’s name indicates the product page, but if you click on the name, you are then redirected to the author’s page.
It seems to me that it is working correctly now.Best regards,
OrianoOctober 5, 2023 at 7:07 pm #1421531Hi,
Ok, so it is working correctly now, shall we close this thread then?Best regards,
MikeOctober 5, 2023 at 7:14 pm #1421532Hi Mike
> Ok, so it is working correctly now, shall we close this thread then?
yes.Thank you very much.
Best regards,
Oriano -
AuthorPosts
- The topic ‘Change Shop #4’ is closed to new replies.