I need to copy the categories pivot for the product where id=15 and apply it to another product. Is there any shorthand to do the copy paste instead of getting the array and attach loop through?
$product = App\Product::find(15);
$product->categories()->attach([1, 5]);
$product = App\Product::find(15);
$newProduct = $product->replicate();
$newProduct->save();
and for the relations
$newProduct->categories()->attach($product->categories);
If I understood well your nee you want to attach the same categories which are attached to product with id = 15 to another product.
$product = App\Product::find(15);
After getting the concern product I can get Array of ids of categories to which that product is belongs to by calling pluck on collection of categories attached to that product
$categories_id = $product->categories()->pluck('id'); // [1,2,3,4,5]
If the second product on which I want to attach that categories has id = 5 I can perform the replication like this
$otherProduct = App\Product::find(5);
$otherProduct->categories()->sync($categories_id);
Related
Since
Mage::app()->getWebsite(true)->getDefaultStore()->getRootCategoryId();
returns '2' I've wrote
$productCatalog = Mage::getModel('catalog/product')->load($product->getId());
$productCatalog->setCategoryIds('2')->save();
but it doesn't change product's category - in Categories tab checkbox "Default Category" isn't ticked.
Say you have a product called $product and a category ID called $category_id
You can put it into a category by doing the following
$product->setCategoryIds(array($category_id));
$product->save();
I think the problem is, you enclosing category id inside a single quote. The method expect an integer array as its parameter. So try this
$category_id = Mage::app()->getWebsite(true)->getDefaultStore()->getRootCategoryId();
$productCatalog = Mage::getModel('catalog/product')->load($product->getId());
$productCatalog->setCategoryIds(array($category_id));
$productCatalog->save();
I am trying to display the Swatch attributes of all the associated simple products to the configurable product that the user is viewing.
I need to do this in the footer which is proving more difficult than I thought as a lot of methods etc are not available in the footer.
I have this code which just shows the Swatch attribute for the configurable product, I need this modified to show the Swatch attribute for all the simple products associated to this configurable.
<?php
$SKU = "2726578";
$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$SKU);
echo $product->getSwatch();
?>
$sku = "2726578";
$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
if($product->getTypeId() == "configurable"){
$childs = $product->getTypeInstance()->getUsedProducts();
}
Hope this helps! Just iterate over the childs and fetch the values.
cheers!
I have toons of products in a magento store thats need to be
sorted into new categories based on diffrent attributes any
one knows how to do this with a script.
example:
if the product attribute "COLOR" has a value of Blue the product should be in cat 10
and so on
Once you iterate over your products you can use the setCategoryIds method (this code is meant to be used for a dropdown attribute as it uses the getAttributeTextMethod):
$product->load($productId);
if( strcmp($product->getAttributeText('COLOR') , 'Blue') == 0 ){
$product->setCategoryIds(array('cat1Id' ,'cat2Id'));
}
$product->save();
Heyhey,
I'm trying to make a script that will load all products from one category and add them to another category (so, basically, just link all products to an additional category). What I'm trying is:
$category = Mage::getModel('catalog/category');
$category->load($id); // Preset category id
$collection = $category->getProductCollection();
$collection->addAttributeToSelect('*');
foreach ($collection as $product) {
$result[] = $product->getProductId();
// Now get category ids and add a specific category to them and save?
}
$result comes up empty, and I have no idea how to proceed. Any ideas?
First thing to look at, don't select all attributes, $collection->addAttributeToSelect('id') is enough. Second to get product ID use
$product->getId();
To change the categories you could try something like this:
$categories = $product->getCategoryIds();
$categories[] = 4; // Category to add
$product->setCategoryIds($categories);
$product->save();
I have some code that returns an array of product details including a url_path. To create a link to this product I have to know the category and subcategory of the product. Unfortunately out all the data this method returns neither the category or sub category are pulled out.
Here is the code I have that gets a product:
$product_id = array(231, 230,229,228);
foreach ($product_id as $id){
$productDetails = Mage::getModel('catalog/product')->load($id)->getData();
echo $productDetails['url_path'].'<br />';
}
Is it possible to get the category and subcategory for each product?
you are looking for
foreach ($product_id as $id){
$categoryIds = Mage::getModel('catalog/product')->load($id)->getCategoryIds();
}
which will return you an array of category ids that the product belongs to.
Better way not to miss something with rewrites is to use core functionality.
Mage_Catalog module has own url model, with which product urls can be modified.
So, the code will be:
$product_id = array(231, 230,229,228);
$urlModel = Mage::getSingleton('catalog/url');
foreach ($product_id as $id) {
$urlModel->refreshProductRewrite($id);
}
And it is no need to retrieve category ids for this purpose.