Skip to main content

Shopify show related product with count validation and meta value

 
Go to theme and add custom liquid block where you want to show related products and also mention meta field value that you want to show instead of product title


{% assign related_products = product.metafields.custom.related_products.value %}

{% if related_products and related_products != blank %}
<div class="related-products">
  <h3>Related Products</h3>
  <div class="related-products-list">

    {% for recommended in related_products %}
    <div class="related-product">
      <a href="{{ recommended.url }}" class="related-product-link">
        <div class="related-product-image">
          {% if recommended.featured_image %}
            <img src="{{ recommended.featured_image.src | img_url: 'medium' }}" alt="{{ recommended.title }}">
          {% else %}
            <img src="{{ 'no-image.png' | asset_url }}" alt="No image available">
          {% endif %}
        </div>
        <div class="related-product-details">
          <!-- Displaying the color_name metafield as the title -->
          <h4 class="related-product-title">
            {% if recommended.metafields.custom.color_name %}
              {{ recommended.metafields.custom.color_name.value }}
            {% else %}
              {{ recommended.title }}
            {% endif %}
          </h4>
        </div>
      </a>
    </div>
    {% endfor %}

  </div>
</div>
{% endif %}


css to beautify the section


.related-products {
  margin-top: 20px;
}

.related-products h3 {
  font-size: 1.5em;
  margin-bottom: 10px;
}

.related-products-list {
  display: flex;
  flex-wrap: wrap;
  gap: 20px; /* Adjust spacing between related products */
}

.related-product {
    width: calc(50% - 20px);
    padding: 10px;
    border: 1px solid #ddd;
}

.related-product-link {
  display: flex;
  flex-direction: column;
  text-align: center;
  text-decoration: none;
  color: #333;
}

.related-product-image img {
  width: 100%;
  height: auto;
  max-height: 200px; /* Adjust image height as needed */
  object-fit: cover;
  border-radius: 5px;
}

.related-product-details {
  padding: 10px 0;
}

.related-product-title {
    color: var(--g-color-heading);
    font-weight: var(--g-p-font-weight);
    line-height: var(--g-p-font-lineheight);
    font-size: var(--g-p-font-size);
    font-family: var(--g-p-font_family);
    letter-spacing: var(--g-p-font-spacing);
    text-transform: var(--g-p-font-transform);
    display: block;
}

.related-product-price {
  font-weight: bold;
  color: #f00; /* Adjust price color */
}

Comments

Popular posts from this blog

Run and compile sass scss file to css using node

  Today we learn how to use scss and generate css using node  or Run and compile sass scss file to css using node   So please follow simple  steps :-   Today we will create a project that can read scss file and generates css with it  Note: Make sure you have installed node in your system. If you want to help to install node js based on your system then check our other tutorial or check node js official website. Now create a blank folder and open  terminal(linux) or cmd(windows) and navigate to your current project folder by using cd command Now run below command npm init after enter it will ask you some package info that you can fill according to you or just keep enter until it finished. The above command will generate package.json file Now  we will install npm module that will convert our scss to css Run below command: npm install node-sass So we have installed node-sass package . Now open package.json file in your editor and add below code into it into

How to retrieve Facebook Likes, share , comment Counts

function facebook_count($url){     // Query in FQL     $fql  = "SELECT share_count, like_count, comment_count ";     $fql .= " FROM link_stat WHERE url = '$url'";     $fqlURL = "https://api.facebook.com/method/fql.query?format=json&query=" . urlencode($fql);     // Facebook Response is in JSON     $response = file_get_contents($fqlURL);     return json_decode($response); } $fb = facebook_count('https://www.facebook.com/BahutHoGyiPadhai'); // facebook share count echo $fb[0]->share_count;  echo "like"; // facebook like count echo $fb[0]->like_count ; echo "comment"; // facebook comment count echo $fb[0]->comment_count;  ?>

jQuery Datatable add date range filter

jQuery Datatable add date range filter Datatable is most useful jQuery plugin that helps to make our html tables more powerful and give powers to user to filter , search, sort, pagination etc, But Data table provides a common filter only and yes we can customize and add filter for each column, but still sometimes we need an advance filter like show results only between a date range, So today we will learn how to create a minimum and maximum date range fields and show date picker on it, and user can fill dates by selecting dates and data table will auto filter records based on it. Keep follow below steps :- I am using Bootstrap if you want to use any other framework then you can use. Create a new index.php file  and paste below code in it, i have used all required CDN like bootstrap, datatable, datepicker etc. <!DOCTYPE html> <html> <head>     <title>Datatable Date Range Filter Example</title>     <link rel="stylesheet" href="https://maxcd