I want to do a major changes in the weights of my products in Magento 2.
I don't want to manually change all the products, that will take days. I want to change them in the database directly, but in which table(s) are the product weights stored?
use Magento\Framework\App\Bootstrap;
include('app/bootstrap.php');
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$_product = $objectManager->create('Magento\Catalog\Model\Product');
$_product = $_product->setWeight(4.0000);
$_product->save();
It is a just example for you.
To achieve your requirement you have to getProductCollection make foreach loop over that collection and get product from that collection than you can apply this example as per your requirement.
This example will not work in .phtml file or your extension.
To check this file you need to create external .php file on root of your magento 2.
Hope this will helps you.
Related
I want to sort a product collection that is already loaded by
$_productCollection = $this->getLoadedProductCollection();
The default sort in admin Magento is Style attribute
I want to sort first by Style, then by color and then by name.
I've try
$_productCollection->setOrder(array('style', 'color','name'), asc);
and also
$_productCollection->addAttributeToSort('color', Varien_Data_Collection::SORT_ORDER_ASC);
$_productCollection->addAttributeToSort('name', Varien_Data_Collection::SORT_ORDER_ASC);
but is not working.
The default sort is working good. Can someone please help?
You can do it this way:
$_productCollection = $this->getLoadedProductCollection();
$_productCollection->clear();
$_productCollection->addAttributeToSort('color', Varien_Data_Collection::SORT_ORDER_ASC);
foreach ($_productCollection as $product) {
// ...
}
This way the collection is forced to be reloaded and then your custom sorting is applied.
You can also take clone of the loaded collection and then modify the query as you want.
// clone of the current collection.Better way of modifying the colection
$_productCollection = clone $this->getLoadedProductCollection();
// unset the cuurent coolection and append your custom filter.
$_productCollection->clear()
->addAttributeToFilter('color', Varien_Data_Collection::SORT_ORDER_ASC)
->load();
Nothing here or elsewhere worked for me. No matter what I tried it would just not sort on /Magento_Catalog/templates/product/list.phtml using 'getLoadedProductCollection()'. Also had a weird issue where products would disappear after clearing cache, until a reindex was done. No idea what was going on there, something was not right.
So I got it to work in the hacky way below (Yes I know it's not 'best practice' to use object manager but it was the only thing that worked!!)
$_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$categoryId = 2;
$category = $_objectManager->create('Magento\Catalog\Model\Category')->load($categoryId);
$productCollection = $_objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$_productCollection = $productCollection->create()
->addAttributeToSelect('*')
->setOrder('position', 'ASC')
->addCategoryFilter($category)->load();
If you have products in a collection that need more advanced sorting/oordering you can move the products to an assoc array which is much easier to sort/order as you like. Once the order is as you like you can add them back by first clearing your collection with $collection-removeAllItems() and then iterate your array as $product and move each product back to the collection with $collection->addItem($product);
I used this when I wanted a sticky that was not touched by different ordering/sorting.
You can use setOrder on collection to sort any collection data like below :
$_productCollection->setOrder('id','DESC');
Where id can be replace with your column name, & second parameter can be DESC & ASC as per your requirement.
Have it working in v2.3.7 using the following
$_productCollection = $block->getLoadedProductCollection();
$_productCollection->getSelect()->reset(Zend_Db_Select::ORDER);
$_productCollection->setOrder('price', 'ASC');
I'm trying to programmatically manipulate the product relations in a Magento store.
From what I've read, setRelatedLinkData should be the way to go.
As I simple test, I'm just trying to replace a products related products with nothing (i.e. an empty array), however it's not working - the product in question is still showing the related product in the backend.
The test code I'm working with is:
$product = Mage::getModel('catalog/product')->load($product->getId());
$linkData = array();
print_r($linkData);
$product->setRelatedLinkData($linkData);
echo "Save\n";
$r = $product->save();
As mentioned above however the product still has a related product when I reload it in the backend.
NOTE: I don't only want to remove related products, eventually I want to be able to add new ones as well, so a DELTE FROM... SQL query isn't what I am looking for. However if I can't get it to work to remove products, then it's certainly not going to work to add them, so one step at a time :-)
The quickest way I can think of is to use the Link Resource:
app/code/core/Mage/Catalog/Model/Resource/Product/Link.php saveProductLinks
// sample code
$product = Mage::getModel('catalog/product')->load(147);
$linkData = array();
Mage::getResourceModel('catalog/product_link')->saveProductLinks(
$product, $linkData, Mage_Catalog_Model_Product_Link::LINK_TYPE_RELATED
);
and if you want to assign products use the same code but provide this as $linkData:
$linkData = array(
'145' => array('position' => 1),
'146' => array('position' => 2)
);
I have imported more than 55k products in magento. And i found that some of the products don't have images imported.
Can you give me a way how can i export the products having no images ?
I have tried to export the product having image value as 'no_selection' but, it gives me only 2-3 items. Then i tried to cross verify those entity_id which have no images into 'catalog_product_entity_varchar' table. And i surprised by not finding when some entity_id presents in 'catalog_product_entity' table but not in 'catalog_product_entity_varchar' table.
Can anyone give me a way to solve this problem.
Any help will really be appreciated.
Regards
Does this query help?
SELECT t0.sku
FROM catalog_product_entity AS t0
WHERE NOT EXISTS(
SELECT NULL
FROM catalog_product_entity_media_gallery AS t1
WHERE (t1.entity_id = t0.entity_id)
)
If you only need SKUs, one method I have used in the past is via a Magento shell script. This example will output only SKU's (one per line) for only those products which have no value for the image attribute.
<?php
error_reporting(E_ALL);
error_reporting(-1);
require_once 'app/Mage.php';
Mage::app();
$collection = Mage::getModel('catalog/product')->getCollection();
foreach($collection as $product){
$productId = $product->getId();
$prod = Mage::getModel('catalog/product')->load($productId);
$hasImage = Mage::helper('catalog/image')->init($prod, 'image');
if (!$hasImage){
echo $prod->getSku() . "\n";
}
}
?>
I put this into "list-noimages.php, and into my Magento root folder. Only use this in development environment, or very limited on production. Run it via shell as so:
php list-noimages.php
For that many products you can output to a file:
php list-noimages.php >> skus.txt
Otherwise visit the file via your web browser and save the output.
I am importing data from oscommerce to magento for one of my clients.
All data including products, categories, and customers has been imported successfully. The last thing left is adding a product sort number after setting their categories.
I need following steps:
Import products -> assign categories to product -> set product sort order (display order) from the oscommerce table.
I searched a lot, but I was not able to find the method to set a product's sort order in a particular category.
Any help will be deeply appreciated.
Product's sort order in a category is by default determined by the position column in 'Category Products' tab in Manage Categories section.
I guess you need to bulk update the sort orders of the products per category. Assuming that you may need to write a php script, that can update the 'position' column in table 'catalog_category_product' in database using sql queries.
File can be kept at the root directory in your magento installation. Below code is just to give an idea, you need to modify/add/remove code in order to complete it as per your requirement and then hit the file from your browser.
<?php
$mageFilename = 'app/Mage.php';
require_once $mageFilename;
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
umask(0);
Mage::app('admin');
Mage::register('isSecureArea', 1);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
set_time_limit(0);
ini_set('memory_limit','1024M');
/***************** UTILITY FUNCTIONS ********************/
function _getConnection($type = 'core_read'){
return Mage::getSingleton('core/resource')->getConnection($type);
}
function _getTableName($tableName){
return Mage::getSingleton('core/resource')->getTableName($tableName);
}
function _updatePosition($position, $categoryId, $productId){
$connection = _getConnection('core_write');
$sql = "UPDATE " . _getTableName('catalog_category_product') . " ccp
SET ccp.position = ?
WHERE ccp.category_id = ?
AND ccp.product_id = ?";
$connection->query($sql, array($position, $categoryId, $productId));
}
hope this help !
I want to create a page in the Magento CMS, then echo specific category descriptions on it. What is the code snippet i will need to accomplish this. I am assuming I will need to reference the categories' unique id within the database to echo their description...
thanks for the help!
john
Using PHP:
$categoryId = 15;
$category = Mage::getModel('catalog/category')->load($categoryId);
if($category->getId()) {
echo $category->getDescription(); // Should escape this, blocks have $this->escapeHtml()
}
I don't know how to do this using magentos email/cms template markup (I don't think its possible) - unless you create a block or widget.