• Resolved jsnowbusiness

    (@jsnowbusiness)


    I want to display 4 of my product attributes on archives where the short description usually is.

    I came up with this code just to echo the values:

    <div class="summary-attributes">
    CPU: <?php echo wc_get_product()->get_attribute( 'pa_cpu' ); ?>
    RAM: <?php echo wc_get_product()->get_attribute( 'pa_guaranteed-ram' ); ?>
    Disk: <?php echo wc_get_product()->get_attribute( 'pa_disk' ); ?>
    Traffic: <?php echo wc_get_product()->get_attribute( 'pa_traffic' ); ?>
    </div>

    But it seems WooCommerce templates are all functions and my PHP isn’t that good… so I have no idea how to make these four attributes show on archives where short description usually is…

    I also want it to show on product pages where short description usually is as well. (There is a plugin for that, but it displays ALL attributes instead of just specific ones, plus it only shows on product pages but not archive pages.)

    Greatly appreciate someone showing me where/how to accomplish this. Thanks.

    • This topic was modified 7 years, 6 months ago by jsnowbusiness.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter jsnowbusiness

    (@jsnowbusiness)

    I’ve been trying to use the code found here like this:

    // For WooCommerce Version 3.0+ (only)
    add_action( 'woocommerce_shop_loop_item_title', 'custom_attributes_display', 20 );
    function custom_attributes_display(){
    
        // Just for product category archives pages
        if(is_product_category()){
            global $product;
    
            // the array of attributes names
            $attribute_names = array('pa_guaranteed-ram', 'pa_cpu', 'pa_disk', 'pa_traffic');
            foreach( $attribute_names as $key => $attribute_name ) {
    
                // For WooCommerce version 3.0+
                $product_id = $product->get_id(); // WC 3.0+
    
                // Getting the value of an attribute (OK for WC 3.0+)
                $attribute_value = array_shift(wc_get_product_terms( $product_id, $attribute_name));
    
                // Displays only if attribute exist for the product
                if(!empty($attribute_value)){ // Updated
                    echo $attribute_value;
    
                    // Separating each number by a " / "
                    if($key < 3) echo ' / ';
                }
            }
        }
    }

    But I always end up with error: Catchable fatal error: Object of class WP_Term could not be converted to string in ../functions.php on line 51 which is this line:

    
    echo $attribute_value;

    Each of the four attributes I’m trying to pull terms from only have one value so I’m not sure if the array_shift is part of the problem, but it still doesn’t work when removed.

    I’ve been at it for a few hours reading other posts/guides and have tried changing things a bit but it still never helps. Any help is appreciated.

    Thread Starter jsnowbusiness

    (@jsnowbusiness)

    Accomplished via this in functions.php:

    // Compatibility for WC 3+ and automation enhancements
    add_action( 'woocommerce_shop_loop_item_title', 'custom_attributes_display', 20 );
    function custom_attributes_display() {
      $product = wc_get_product();
      echo '<div class="custom-attr"><div class="attr-ram">RAM: <strong>' . $product->get_attribute( 'pa_guaranteed-ram' ) . '</strong></div>';
      echo '<div class="attr-cpu">CPU: <strong>' . $product->get_attribute( 'pa_cpu' ) . '</strong></div>';
      echo '<div class="attr-disk">Disk: <strong>' . $product->get_attribute( 'pa_disk' ) . '</strong></div>';
      echo '<div class="attr-traffic">Traffic: <strong>' . $product->get_attribute( 'pa_traffic' ) . '</strong></div></div>';
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Display Specific Attributes where Short Description displays’ is closed to new replies.