Magento 2 Add product to category (code)
/**
* @var \Magento\Catalog\Api\CategoryLinkManagementInterface
*/
protected $_categoryLinkManagement;
$this->_categoryLinkManagement->assignProductToCategories($sku, $categoryIds);
//where $sku is sku of product, and $categoryIds is array of real categories ids
Display magento products by category ID
get Product from specific category
$categoryIds = array(2,4);//category id
$collection = Mage::getModel('catalog/product')
->getCollection()
->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
->addAttributeToSelect('*')
->addAttributeToFilter('category_id', array('in' => $categoryIds))
get Product for specific product id$productids = array(52,62);//product ids
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addFieldToFilter('entity_id',array( 'in' => $productids));
then write this in phtml<?php $_collectionSize = $collection->count() ?>
<?php //$_columnCount = $this->getColumnCount(); ?>
<?php $i=0; foreach ($collection as $product): ?>
<?php if ($i++%4==0): ?>
<ul class="products-grid">
<?php endif ?>
<li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">
<a href="<?php echo $product->getProductUrl()?>" title="<?php echo $product->getName()?>">
<img src="<?php echo Mage::helper('catalog/image')->init($product, 'small_image')->resize(197, 167); ?>" alt="<?php echo $product->getName()?>" border="0" />
</a>
<h2 class="product-name"><a href="<?php echo $product->getProductUrl()?>" title="<?php echo $product->getName()?>"><?php echo $product->getName() ?></a></h2>
<div class="price-box">
<?php echo Mage::helper('core')->currency($product->getPrice(),true,false);?>
</div>
<div class="actions">
<?php if($product->isSaleable()): ?>
<button class="button" onclick="setLocation('<?php echo Mage::getUrl('checkout/cart/add/')?>product/<?php echo $product->getId() ?>/')" title="<?php echo $this->__('Köp');?>" type="submit"><span><span><?php echo $this->__('Köp');?></span></span></button>
<?php else: ?>
<p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
<?php endif; ?>
</div>
</li>
<?php if ($i%$_columnCount==0 || $i==$_collectionSize): ?>
</ul>
<?php endif ?>
<?php endforeach ?>
hope this help you How do display products by category in CMS page in Magento?
{{block type="catalog/product_list" category_id="8" template="catalog/product/featured.phtml"}}
add above code in cms page and add featured.phtml file in catalog/product and put this code <?php $_productCollection=$this->getLoadedProductCollection() ?>
<?php if(!$_productCollection->count()): ?>
<div class="note-msg">
<?php echo $this->__('There are no products matching the selection. Please provide a category ID.') ?>
</div>
<?php else: ?>
<?php // Grid Mode ?>
<ul id="featured" class="jcarousel-skin-tango">
<?php $_collectionSize = $_productCollection->count() ?>
<?php $i=0; foreach ($_productCollection as $_product): ?>
<?php if($i++%4==0): ?>
<?php endif ?>
<li><a class="preview" rel="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(300, 300); ?>" href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>">
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(105, 105); ?>" width="105" height="105" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" />
</a> </li>
<?php if ($i%4==0 && $i!=$_collectionSize): ?>
<?php endif ?>
<?php endforeach ?>
</ul>
<?php endif; ?>
How to assign categories for products in magento Programmatically
Here is how you can assign multiple products to a category and merge with the existing products.
The example is for one category but you can turn it into a loop to make it work for more.
$categoryId = 6;
$category = Mage::getModel('catalog/category')->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID)->load($categoryId);
//get the current products
$products = $category->getProductsPosition();
//now attach the other products.
$newProductIds = array(1,2,3,4,5);
foreach ($newProductIds as $id){
$products[$id] = 1;//you can put any other position number instead of 1.
}
//attach all the products to the category
$category->setPostedProducts($products);
//save the category.
$category->save();
If you want an even faster way of doing it you can do direct inserts in the table catalog_category_product
.Just make sure you reindex when you are done.
List active products in a category in Magento with SQL
This might help a bit.
Here's my query for retrieving the category names that are active, along with the count of all of the products within the category, such that the count is greater than zero.
SELECT t0.value, t0.c0
FROM (
SELECT t2.value, (
SELECT COUNT(*)
FROM catalog_category_product AS t4
WHERE (t4.category_id = t1.entity_id)) AS c0
FROM catalog_category_entity AS t1
INNER JOIN catalog_category_entity_varchar AS t2 ON (t1.entity_id = t2.entity_id)
INNER JOIN catalog_category_entity_int AS t3 ON (t1.entity_id = t3.entity_id)
WHERE (((((t2.attribute_id = 41)
AND (t2.store_id = 0))
AND (t3.attribute_id = 42))
AND (t3.store_id = 0))
AND (t3.value = 1))) AS t0
WHERE (t0.c0 > 0)
I haven't tried filtering products by what is in stock though. Magento mass-assign products to category
I managed to resolve the problem with the following code :
$write = Mage::getSingleton('core/resource')->getConnection('core_write');
$x = 1171;
$y = 2000;
$categoryID = 4;
$productPosition = 0;
while($x <= $y) {
$write->query("REPLACE INTO `catalog_category_product` (`category_id`, `product_id`, `position`) VALUES ($categoryID, $x++, $productPosition)");
}
echo "The job is done";
?>
I hope the code is clear for everyone,if it's not,reply and i'll try to explain it.@nachito : here it is.
Products is not displaying in category page in Magento2.2.0?
I got the solution for this problem. Magento didn't get placeholder image.
I see that in /pub/media/catalog/product/, no path define for placeholder image. that's why the product is not displayed in category page in my case.
I put an image in /pub/media/catalog/product/images/placeholder/image.jpg
This is work for me properly.
Related Topics
Storing Leading Zeros of Integers in MySQL Database as Integer
How to Get Content of Remote HTML Page
How to Get an Array of Data from $_Post
Sending JavaScript Object to PHP via Ajax
Callback Function Using Variables Calculated Outside of It
How to Generate a Round Robin Tournament in PHP and MySQL
How to Access Magento Customer's Session from Outside Magento
Use of Undefined Constant Mcrypt_Rijndael_128 - Assumed 'Mcrypt_Rijndael_128'
PHP Curl Post Returns a 415 - Unsupported Media Type
Symfony2:Two Forms in a Same Page
Use PHP to Check If Page Was Accessed with Ssl
How to Upload Files in Web Workers Without Using Formdata
How to Tag a User in a Photo Using the Facebook Graph API
Converting Float Decimal to Fraction
Having Trouble Writing to a File with PHP on Ubuntu
Z-Scores(Standard Deviation and Mean) in PHP