Hikashop product methods

  • Posts: 46
  • Thank you received: 2
7 years 2 months ago #262488

I'm building a small "last products" slider module for a twig processed Joomla template, (gantry).

As i dont want to rebuild any feature which already exists in the core, i was looking for the best way to get the "n" first products results ordered by creation date.

I saw the "get", getProducts, or even getProduct methods in the Product class which take ids as parameters, but i didnt find any functions for what i'm looking for.

I suppose that you used this kind of function with your own products display module, but i'm not used to joomla architecture (come from Laravel) and i cant find it.

Please Log in or Create an account to join the conversation.

  • Posts: 46
  • Thank you received: 2
7 years 2 months ago #262499

For now, u just used the vanilla Joomla pdo to retrieve my product ids, then, i call your getProduct method from the Product class:

if(!@include_once(rtrim(JPATH_ADMINISTRATOR,DS).DS.'components'.DS.'com_hikashop'.DS.'helpers'.DS.'helper.php')){ return false; }

class Last_Products
{
    public $ids = NULL; 
    public $products = NULL; 

    public function getProducts ($nombre_produits = 3) {

        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
         
        $query->select($db->quoteName('product_id'));
        $query->from($db->quoteName('#__hikashop_product'));
        $query->where($db->quoteName('product_published') . ' = '. $db->quote('1'));
        $query->order($db->quoteName('product_created') . ' DESC');
        $query->setLimit( $nombre_produits );

        $productClass = hikashop_get('class.product');
        
        $db->setQuery($query);
        $this->ids= $db->loadColumn();

        foreach( $this->ids as $id ) {
            $this->products[$id] =  $productClass->getProduct($id);
        }

        return $this->products;

    }


}

Then i can call my product object in twig
{% extends '@nucleus/partials/particle.html.twig' %}

{% block particle %}
    <p>
        <ul>
        {% for product in gantry.last_products.getProducts() %}
            <li>{{ product.product_name }}</li>
        {% endfor %}
        </ul>
    </p>
{% endblock %}

Even if it is a way too heavy, it works for the name. But i would have prefered to call directly one of your methods.

Now i have to get the image url, and the product url....how can i do that ?

Last edit: 7 years 2 months ago by saturnales.

Please Log in or Create an account to join the conversation.

  • Posts: 81515
  • Thank you received: 13069
  • MODERATOR
7 years 2 months ago #262514

Hi,

There is no one function to get that. At least not for now.
The code to load the products listing data is located in the listing function of the file components/com_hikashop/views/product/view.html.php
There are a lot of things there in order to handle many different features that you don't need, so even if there was a neat function to call for all that, I wouldn't recommend it for your case since it would be overkill.
Doing a query like that is fine.

You can run a second query to load the images of the product from the hikashop_file table checking on the file_ref_id = product_id and the file_type = 'product'
Then, you can look at the file components/com_hikashop/views/product/tmpl/listing_img_title.php for how we get the images links:

<?php $imageHelper = hikashop_get('helper.image');
$img = $imageHelper->getThumbnail($image->file_path, array('width' => 200, 'height' => 200), array('default' => true,'forcesize'=> true,'scale'=>'inside');
if($img->success) {
echo '<img class="hikashop_product_listing_image" title="'.$this->escape(@$this->row->file_description).'" alt="'.$this->escape(@$this->row->file_name).'" src="'.$img->url.'"/>';
}
?>
And for the link:
$productClass = hikashop_get('class.product');
$productClass->addAlias($product);
$link = hikashop_contentLink('product&task=show&cid=' . $product->product_id . '&name=' . $product->alias . '&Itemid=XXX', $product);
echo $link;
where XXX is the id of the menu item which will be used for the display of the product (usually a categories listing menu item or a products listing menu item).

Please Log in or Create an account to join the conversation.

  • Posts: 46
  • Thank you received: 2
7 years 2 months ago #262559

Thanks, what is the $product from

$productClass->addAlias($product);

and what is the addAlias method supposed to do ?

Please Log in or Create an account to join the conversation.

  • Posts: 46
  • Thank you received: 2
7 years 2 months ago #262597

Done ! Heres the code for those who interested :

in templates/[GANTRY_TEMPLATE]/custom/includes/Last_Products.php

<?php

if(!@include_once(rtrim(JPATH_ADMINISTRATOR,DS).DS.'components'.DS.'com_hikashop'.DS.'helpers'.DS.'helper.php')){ return false; }

class Last_Products
{
    public $ids = NULL; 
    public $products = NULL; 
 
    public function __construct() {

    }

    public function getProducts ($nombre_produits = 3) {

        // Get a db connection.
        $db = JFactory::getDbo();
         
        // Create a new query object.
        $query = $db->getQuery(true);
         
        $query->select($db->quoteName('product_id'));
        $query->from($db->quoteName('#__hikashop_product'));
        $query->where($db->quoteName('product_published') . ' = '. $db->quote('1'));
        $query->order($db->quoteName('product_created') . ' DESC');
        $query->setLimit( $nombre_produits );


        //instancie la classe hikashop
        $productClass = hikashop_get('class.product');
         
        //recupere les ids des n derniers produits dans un tableau
        $db->setQuery($query);
        $this->ids= $db->loadColumn();

        foreach( $this->ids as $id ) {
            $this->products[$id]['object'] =  $productClass->getProduct($id);

            $config = hikashop_config();
            $image = reset($this->products[$id]['object']->images);
            $path = '/'.$config->get('uploadfolder').$image->file_path;


            $this->products[$id]['image'] =  $path;
        
            $productClass->addAlias($this->products[$id]['object']);
            $this->products[$id]['link'] =  hikashop_contentLink('product&task=show&cid=' . $this->products[$id]['object']->product_id . '&name=' . $this->products[$id]['object']->alias . '&Itemid=XXX', 
                $this->products[$id]['object']); 

        }

        return $this->products;

    }


}

in templates/[GANTRY_TEMPLATE]/custom/particles/last-products.html.twig
{% extends '@nucleus/partials/particle.html.twig' %}

{% block particle %}
    
        <div class="g-content-pro-slideset style1 gutter-enabled">
            {% if particle.mainheading or particle.introtext %}
                <div class="g-particle-intro">
                    {% if particle.mainheading %}
                        <h3 class="g-title g-main-title">{{ particle.mainheading|raw }}</h3>
                        <div class="g-title-separator {% if particle.introtext == false %}no-intro-text{% endif %}"></div>
                    {% endif %} 
                    {% if particle.introtext %}<p class="g-introtext">{{ particle.introtext|raw }}</p>{% endif %}
                </div>
            {% endif %}         
            <div class="uk-slidenav-position" data-uk-slideset="{small: 1, medium: {{ particle.columns|default('3')|e }}, large: {{ particle.columns|default('3')|e }}, duration:200, animation: 'fade'}">
                <div class="uk-slider-container">
                    <ul class="uk-slideset uk-grid">
						{% for slide in gantry.last_products.getProducts(particle.products|default('3')|e)|batch(particle.columns|default('3')|e) %}
							{% for item in slide %}
							<li>
							<div class="g-content-pro-item">
								<div class="g-content-pro-image">
									<a href="{{ item['link'] }}" class="uk-overlay uk-overlay-hover">
									<span class="uk-overlay-panel uk-overlay-background uk-overlay-icon uk-overlay-fade"></span>
									<img alt="{{ item['object'].product_name }}" src="{{ item['image'] }}" {{ item[image]|imagesize|raw }}>
									</a>
								</div>

								<div class="g-info-container">
									<h4 class="g-content-pro-title">
										<a target="_parent" href="{{ item['object'].product_alias|e }}">
											{{ item['object'].product_name|raw }}
										</a>
									</h4>
									<p class="g-content-pro-desc">{{ item['object'].product_description|raw }}</p>
								</div>
							</div>
							</li>
							{% endfor %}
						{% endfor %}
                    </ul>
                </div>
                <ul class="uk-slideset-nav uk-dotnav uk-flex-center">
                    {% set counter = 0 %}
                    {% for item in gantry.last_products.getProducts() %}
                        <li data-uk-slideset-item="{{ counter }}"><a href=""></a></li>
                        {% set counter = counter + 1 %}
                    {% endfor %}
                </ul>
            </div>              
        </div>
{% endblock %}

in templates/[GANTRY_TEMPLATE]/custom/particles/last-products.yaml
name: Last Products
description: Affiche les derniers produits
type: particle

form:
  fields:
    mainheading:
      type: input.text
      label: Titre
      description: Entrer le titre
      placeholder: 'Entrer le titre'
      default: ''

    introtext:
      type: textarea.textarea
      label: Intro Text
      description: Texte d introduction.
      placeholder: 'Entrer le texte d introduction'
      default: ''

    columns:
      type: select.select
      label: Produits par slide
      description: Sélectionner le nombre d articles par slide.
      placeholder: 'Selectionner...'
      default: 3
      options:
        3: 3
        4: 4
        5: 5
        6: 6

    products:
      type: select.select
      label: Nombre de produits
      description: Sélectionner le nombre d articles au total.
      placeholder: 'Selectionner...'
      default: 3
      options:
        3: 3
        4: 4
        5: 5
        6: 6


Finally, override the main templates/[GANTRY_TEMPLATE]/includes/theme.php with
// Require the Best Products library
include_once dirname(__FILE__).'/../custom/includes/Last_Products.php';

// Dependency Injection of Best Products
$gantry['last_products'] = new \Last_Products();

at the end.

I didnt put the scss....it depends of what you wants to see

Attachments:
Last edit: 7 years 2 months ago by saturnales.

Please Log in or Create an account to join the conversation.

  • Posts: 26000
  • Thank you received: 4004
  • MODERATOR
7 years 2 months ago #262601

Hi,

Thanks for the share !

As you noticed, $product is a variable containing a product object.
The addAlias function will process the alias and so other elements so the url returned by "contentLink" will be a nice URL.

Regards,


Jerome - Obsidev.com
HikaMarket & HikaSerial developer / HikaShop core dev team.

Also helping the HikaShop support team when having some time or couldn't sleep.
By the way, do not send me private message, use the "contact us" form instead.

Please Log in or Create an account to join the conversation.

  • Posts: 54
  • Thank you received: 0
6 years 11 months ago #269051

@saturnales
Hi.
Could you give a link to the page where this code works because I tried to test it, but something is wrong. I probably do something wrong, so I would like to see how this code works.

Last edit: 6 years 11 months ago by Wrosch.

Please Log in or Create an account to join the conversation.

  • Posts: 46
  • Thank you received: 2
6 years 11 months ago #270158

I dont think you can view anything from the front side. But heres a link with it :

www.cocottecollecte.com

The last product slider is a gantry one, directly connected to hikashop

Please Log in or Create an account to join the conversation.

Time to create page: 0.077 seconds
Powered by Kunena Forum