search

Home  >  Q&A  >  body text

How to restrict product attributes in woocommerce customer template

I can get the properties by selecting the custom property name in the table row (like "Color", "Size", "Weight", etc.), but I only want to display 3 rows. My working code is as follows but it shows all the code and I want to show only 3 lines

add_action( 'cw_shop_page_attribute', 'cw_shop_page_attribute', 25 );

function cw_shop_page_attribute() {
global $product;
$display_size = $product->get_attribute('display-size');
$processor = $product->get_attribute('processor-type');
$rearcamera = $product->get_attribute('primary-camera');
$frontcamera = $product->get_attribute('secondary-camera');
$storage = $product->get_attribute('internal-storage-gb');
$m_ram = $product->get_attribute('ram-gb');
$frontcamera = $product->get_attribute('secondary-camera');

if ( $display_size ) {
    echo'<tr class="_plspcdt"><td class="_plspcicon"><i class="fa fa-desktop fa-2x"></i></td><td class="_atrbttl">Display</td>';
    echo'<td class="_atrbvlu">'; printf ($display_size);
    echo'</td></tr>';
   }
if ( $processor ) {
    echo'<tr class="_plspcdt"><td class="_plspcicon"><i class="fa fa-microchip fa-2x"></i></td><td class="_atrbttl">Processor</td>';
    echo'<td class="_atrbvlu">'; printf ($processor);
    echo'</td></tr>';
   }
if ( $rearcamera ) {
    echo'<tr class="_plspcdt"><td class="_plspcicon"><i class="fa fa-camera fa-2x"></i></td><td class="_atrbttl">Rear Camera</td>';
    echo'<td class="_atrbvlu">'; printf ($rearcamera);
    echo'</td></tr>';
   }  
if ( $frontcamera ) {
    echo'<tr class="_plspcdt"><td class="_plspcicon"><i class="fa fa-camera fa-2x"></i></td><td class="_atrbttl">Front Camera</td>';
    echo'<td class="_atrbvlu">'; printf ($frontcamera);
    echo'</td></tr>';
   }

How to show only 3 rows of them and hide them if they are empty

P粉191323236P粉191323236474 days ago609

reply all(1)I'll reply

  • P粉691958181

    P粉6919581812023-09-08 18:30:36

    get_attribute () Returns a comma separated string of values, so you can use php's explode function to loop through the values ​​as an array and then exit after returning 3 results.

    For example:

    if ( $display_size ) {
        echo'<tr class="_plspcdt"><td class="_plspcicon"><i class="fa fa-desktop fa-2x"></i></td><td class="_atrbttl">Display</td>';
        $display_size_array = explode( ',', $display_size );
        $count = 0;
        foreach ( $display_size_array as $attribute ) {
            if ( $count >= 3 ) {
                break;
            }
            echo'<td class="_atrbvlu">' . $attribute . '</td';
            $count++;
        }
        echo'></tr>';
    }
    

    reply
    0
  • Cancelreply