Tagged: 

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #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

    #1422597

    Hey 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:
    Enfold_Support_3702.jpeg
    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:
    Enfold_Support_3708.jpeg
    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,
    Mike

    #1422620

    Good 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,
    Oriano

    #1422641

    Hi,
    I added this css:

    #top .product-attributes .price {
    	display: block;
    }

    Please clear your browser cache and check.

    Best regards,
    Mike

    #1422908

    Hi 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
    Oriano

    #1423253

    Hi,
    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,
    Mike

    #1423281

    Hi 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
    Oriano

    #1423298

    Hi,
    The commas are not in the author links:
    Enfold_Support_3759.jpeg
    but please note that the entire product grid item is wrapped in a link to the product page:
    Enfold_Support_3761.jpeg
    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,
    Mike

    #1423304

    Hello 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,
    Oriano

    #1423311

    Hi,
    For the product page, the commas are not inside of the links:
    Enfold_Support_3763.jpeg

    Best regards,
    Mike

    #1423315

    Hi 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,
    Oriano

    #1423332

    Hi,
    Ok, hopefully it also works well on the live site, this has been a big customization, more than normal.

    Best regards,
    Mike

Viewing 12 posts - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.