Question

J'ai ce qui suit qui montre un ensemble de produits sur la page produit :

if ($_product->getAttributeText('bundle') == yes && $simplecount > 1):
$product_ids = explode(',',$_product->get_bundle_products());

$collection = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('entity_id', array('in' => $product_ids));

echo'<div class="bun2c" style="text-align:center; float:right;">';
foreach($collection as $eachproduct){
echo '<a href="' . $eachproduct->getProductUrl() . '"><img class="bundle" src="' . $eachproduct->getImageUrl() . '" title="' . $eachproduct->getName() . '"></a>';
$productId = $eachproduct->getId();
$simplecount = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($eachproduct)->getQty();
}

Cela fonctionne bien, mais je n'ai pas besoin de le montrer lorsque l'un des produits groupés est en rupture de stock.

J'ai essayé avec la quantité d'articles ($ simplecount) mais cela ne fonctionne pas.

J'ai utilisé Mage::getModel('cataloginventory/stock_item')->loadByProduct($eachproduct)->getIsInStock() mais cela ne fonctionne que lorsque chaque article est en rupture de stock

Était-ce utile?

La solution

J'ai réussi à obtenir ce que je voulais et voici ce que j'ai fait,

J'ai d'abord calculé les articles qui étaient en stock :

$fullstock = 0;
$inStock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($eachproduct)->getIsInStock();
$fullstock += $inStock;

J'ai ensuite obtenu le nombre de produits du bundle :

$product_count = count($collection);

J'ai ensuite utilisé $fullstock == $product_count dans mon if déclaration

Le code complet est le suivant

$ids = array();
$quote = Mage::getSingleton('checkout/session')->getQuote();
$fullstock = 0;
    foreach ($quote->getAllItems() as $item) {
        $ids[$item->getProductId()] = $item->getProductId(); //used array key here to avoid duplicates
    }
$product_ids = explode(',',$_product->getbundle_products());
$collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*')->addAttributeToFilter('entity_id', array('in' => $product_ids));
    foreach($collection as $eachproduct){
        $productId = $eachproduct->getId();
        $inStock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($eachproduct)->getIsInStock();
        $fullstock += $inStock;
}
$product_count = count($collection);
    if ($_product->getAttributeText('bundle') == yes && $fullstock == $product_count):  
        echo'<div class="fgfgdfd" style="text-align:center; float:right;">';
    foreach($collection as $eachproduct){
        echo '<a href="' . $eachproduct->getProductUrl() . '"><img class="bundle" src="' . $eachproduct->getImageUrl() . '" title="' . $eachproduct->getName() . '"></a>';
            $productId = $eachproduct->getId();
if (isset($ids[$productId])) {
    echo '<span class="bundle-check">&#10003;</span>';
}
$combined_price += $eachproduct->getFinalPrice()/100*$_product->getbundle_discount();
}
setlocale(LC_MONETARY, 'en_GB.UTF-8');
echo '<div><p class="availability in-stock">Save <span class="in-stock1">' . money_format('%.2n', $combined_price) . '</span> <span class="when-you"> When you buy</span> <span class="with-with"> with</span> the bundle</p></div></div>';
endif;
Licencié sous: CC-BY-SA avec attribution
Non affilié à magento.stackexchange
scroll top