Recherche…


Obtenir le produit par sku

$sku = 'sku-goes-here';
$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);

Obtenir le produit par ID

$id = 1;
$product = Mage::getModel('catalog/product')->load($id);
if($product->getId()){
    //product was found
}

Collection de produits - requête LIKE

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToFilter('sku', array('like' => 'UX%'));

Obtenir la collection de produits par attribut

$collection = Mage::getModel('catalog/product')->getCollection();
// Using operator
$collection->addAttributeToFilter('status', array('eq' => 1)); 
// Without operator (automatically uses 'equal' operator
$collection->addAttributeToFilter('status', 1); 

Obtenir des données à partir de l'objet produit

// First load a product object

$product->getSku();
$product->getName();

// Alternative method
$product->getData('sku');
$product->getData('name');

Obtenir une collection de produits sous forme de données

// First load a collection object

foreach($collection as $product) {
    
    $product->getSku();
    $product->getName();

    // Alternative method
    $product->getData('sku');
    $product->getData('name'); 
}    

Collection de produits - avec attributs

//all attributes
$collection = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect('*');
//specific attributes
$collection = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect('name');
//certain attributes are special, such as price and images
//for images, then you can use 'getMediaGalleryImages'
$product->load('media_galley');

Vérifiez si le produit a été correctement chargé

$productFound = ($product->getId() !== null)

Obtenir l'identifiant du produit par SKU

$sku = 'some-sku';
$productId = Mage::getModel('catalog/product')->getIdBySku($sku);
if($productId){
   //sku exists
}

Obtenir une collection de produits à partir d'une liste de références

$skuList = array('SKU-1', 'SKU-2',...,'SKU-n);

$_productCollection = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToFilter('sku', array('in' => $skuList));

OU

$_productCollection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToFilter('sku', array('in' => $skuList));

Définir la limite dans la collection de produits

$collection = Mage::getModel('catalog/product')
            ->getCollection()
            ->setPageSize(20)
            ->setCurPage(1);


Modified text is an extract of the original Stack Overflow Documentation
Sous licence CC BY-SA 3.0
Non affilié à Stack Overflow