Magento re-generate url-keys for categories - magento

I must re-generate all url-keys for all categories.
When I added some main categories i copied them using one of way from forum.
Everything was going ok, but when i’m copied about 20k categories i saw: when the name has polish letter the rest was cutted:
Example:
name of category: Części karoseryjne
should be: czesci-karoseryjne
after copy: cz
name of category: Próg zwalniajacy
should be: prog-zwalniajacy
after copy: pr
I’m found on polish forum the way to fix it (with polish letters), but it works only with new added categories.
Now, when i select any category, clear the url-key and click save - the naming is ok, but ....... there is 20k categories…
Can somebody write how to fix it ?

http://www.errorin.com/open-source/how-to-add-category-programmatically-in-magento-1-7-0-with-custom-field/
require_once 'app/Mage.php';
Mage::app('default'); // Default or your store view name.
//get a new category object
$category = Mage::getModel('catalog/category');
$category->setStoreId(0); // 0 = default/all store view. If you want to save data for a specific store view, replace 0 by Mage::app()->getStore()->getId().
//if update
if ($id) {
$category->load($id);
}
$general['name'] = "My Category";
$general['path'] = "1/2/23"; // catalog path
$general['description'] = "Great My Category";
$general['meta_title'] = "My Category"; //Page title
$general['meta_keywords'] = "My , Category";
$general['meta_description'] = "Some description to be found by meta search robots. 2";
$general['landing_page'] = ""; //has to be created in advance, here comes id
$general['display_mode'] = "PRODUCTS_AND_PAGE"; //static block and the products are shown on the page
$general['is_active'] = 1;
$general['is_anchor'] = 0;
$general['page_layout'] = 'two_columns_left';
//$general['url_key'] = "cars";//url to be used for this category's page by magento.
//$general['image'] = "cars.jpg";
$category->addData($general);
try {
$category->save();
echo "Success! Id: ".$category->getId();
}
catch (Exception $e){
echo $e->getMessage();
}

Related

Import Profile Product Review import/export

I'm using Magento extension https://www.magentocommerce.com/magento-connect/product-review-import-export.html for importing prodcuts review.
I've exported review from one Magento site and I'm trying to import on another website.
While importing products it's show message "Processed 0% 0/1 records" and keep it showing, No process in importing products.
Importing Preview
I've changed my table prefix in "app/code/local/MK/Reviewexport/Model/Convert/Adapter/Reviewimport.php" but still nothing happening.
Waited too long but it keep showing me "Processed 0% 0/1 records" I've too many reviews and so it was not working I've removed all reviews from CSV and kept only one review.
This extension is created by : https://magento.stackexchange.com/users/599/mufaddal
Anyways I've resolved the issue by making custom script for importing reviews which is exported by that mentioned extension.
Here is a code
<?php
ini_set('memory_limit', '128M');
error_reporting(E_ALL);
ini_set('display_errors', '1');
require_once 'app/Mage.php';
Mage::app();
$fileLocation = "var/import/import_review.csv";
$fp = fopen($fileLocation, 'r');
$count = 1;
while($data = fgetcsv($fp)){
if($count > 1){
//intiate requirement varibles
$_createdAt = $data[0];
$_sku = $data[1];
$_catalog = Mage::getModel('catalog/product');
$_productId = $_catalog->getIdBySku($_sku);
$_statusId = $data[2];
$_title = $data[3];
$_detail = $data[4];
$_customerId = NULL;
$_nickname = $data[5];
//load magento review model and assign values
$review = Mage::getModel('review/review');
$review->setCreatedAt($_createdAt); //created date and time
$review->setEntityPkValue($_productId);//product id
$review->setStatusId($_statusId); // status id
$review->setTitle($_title); // review title
$review->setDetail($_detail); // review detail
$review->setEntityId(1); // leave it 1
$review->setStoreId(Mage::app()->getStore()->getId()); // store id
$review->setCustomerId($_customerId); //null is for administrator
$review->setNickname($_nickname); //customer nickname
$review->setReviewId($review->getId());//set current review id
$review->setStores(array(Mage::app()->getStore()->getId()));//store id's
$review->save();
$review->aggregate();
//set review ratings
if($data[7]){
$arr_data = explode("#",$data[7]);
if(!empty($arr_data)) {
foreach($arr_data as $each_data) {
$arr_rating = explode(":",$each_data);
if($arr_rating[1] != 0) {
Mage::getModel('rating/rating')
->setRatingId($arr_rating[0])
->setReviewId($review->getId())
->setCustomerId($_customerId)
->addOptionVote($arr_rating[1], $_productId);
}
}
}
$review->aggregate();
}
}
// if($count == 5){
// die("total $count reviews are imported!");
// }
$count++;
}
echo "total $count reviews are imported!";
?>

How to create in Magento with custom code?

I am using this code to create categories and subcategories. This code works fine but the problem is I can't create my own category codes. And I need it to use it for the requeriments.
$category = Mage::getModel('catalog/category');
$category->setStoreId(0); // 0 = default/all store view. If you want to save data for a specific store view, replace 0 by Mage::app()->getStore()->getId().
//if update
if ($id) {
$category->load($id);
}
$general['name'] = "My Category";
$general['path'] = "1/3/"; // catalog path here you can add your own ID
$general['description'] = "Great My Category";
$general['meta_title'] = "My Category"; //Page title
$general['meta_keywords'] = "My , Category";
$general['meta_description'] = "Some description to be found by meta search robots. 2";
$general['landing_page'] = ""; //has to be created in advance, here comes id
$general['display_mode'] = "PRODUCTS"; //static block and the products are shown on the page
$general['is_active'] = 1;
$general['is_anchor'] = 0;
$general['page_layout'] = 'two_columns_left';
//$general['url_key'] = "cars";//url to be used for this category's page by magento.
//$general['image'] = "cars.jpg";
$category->addData($general);
try {
$category->setId(255); // Here you cant set your own entity id
$category->save();
echo "Success! Id: ".$category->getId();
}
catch (Exception $e){
echo $e->getMessage();
}
I tried to do something like that:
$general['id'] = $myCustomId;
But it's not working.
The key needs to map the column header, which is entity_id. So, change $general['id'] to
$general['entity_id'] = $myCustomId;

Deleting Products with Magmi Datapump

I can successfully delete products with MAGMI's web frontend.
But when using the datapump API, it just seems to ignore the magmi:delete column...
$this->magmi = Magmi_DataPumpFactory::getDataPumpInstance("productimport");
$this->magmi->beginImportSession("Default","create", new MagmiLogger($this));
$prods = Mage::getResourceModel('catalog/product_collection');
$count = $prods->getSize();
$i = 0;
foreach ($prods as $_p) {
$this->magmi->ingest(array(
'sku' => $_p->getSku(),
'magmi:delete' => '1'
));
}
$this->magmi->endImportSession();
With this code I try to delete my whole catalog. When executing, the foreach loop runs through without any exception or error by MAGMI. The Product Deleter plugin is activated in my selected Default profile.
What am I missing here?
Nevermind, I selected the wrong profile...
It works with the posted code, when you really select a profile with activated "Product Deleter" plugin.
Click Here to add MAGMI into your module -> https://magento.stackexchange.com/a/351175
Enable Product Deleter plugin.
Using CSV
"sku","magmi:delete"
"sku1","1"
"sku2","1"
Using Code
$importData = [];
$configProduct = new \Magmi_ProductImport_DataPump('productimport');
$importData['sku'] = "your Product SKU";
$importData['magmi:delete'] = "1";
$configProduct->beginImportSession("default", "update");
$run = $configProduct->ingest($importData);
$configProduct->endImportSession();
Check catalog_eav_attribute table in your db . If is_configurable column not present then update below code from getConfigurableOptsFromAsId() function in this file .../magmi/plugins/base/itemprocessors/configurables/magmi_configurableprocessor.php
//$sql .= " JOIN $cea as cea ON cea.attribute_id=ea.attribute_id AND cea.is_global=1 AND cea.is_configurable=1";
$sql .= " JOIN $cea as cea ON cea.attribute_id=ea.attribute_id AND cea.is_global=1";
} else {
//$cond .= " AND ea.is_global=1 AND ea.is_configurable=1";
$cond .= " AND ea.is_global=1";
Note:- magmi:delete : 1 is to delete the product 0 is to not delete the product

Magento : Display html element based on product attribute set?

I wish to display a static block on the product page if the product belongs to a certain Attribute Set
For example if I was a fashion store, and I have an attribute set of "Footwear" I only want the static block to show on product pages when the attribute set matches "Footwear"
I have found a little bit of code that outputs the ID of the Attribute set but I want to turn it into an else if statement.
<?php
$entityTypeId = Mage::getModel('eav/entity')
->setType('catalog_product')
->getTypeId();
$attributeSetName = 'Footwear';
$attributeSetId = Mage::getModel('eav/entity_attribute_set')
->getCollection()
->setEntityTypeFilter($entityTypeId)
->addFieldToFilter('attribute_set_name', $attributeSetName)
->getFirstItem()
->getAttributeSetId();
echo $attributeSetId;
?>
Anyone have any ideas?
G
Add this method to the Product View Block
(not to core file app/code/core/Mage/Catalog/Block/Product/View.php of course):
public function checkAttributeSet($product = null, $attributeSetName = null)
{
if(is_null($product) || is_null($attributeSetName))
return false;
$attributeSetModel = Mage::getModel("eav/entity_attribute_set");
$attributeSetModel->load($product->getAttributeSetId());
if($attributeSetModel->getAttributeSetName() == $attributeSetName) {
return true;
} else {
return false;
}
}
Then in app/design/frontend/package/theme/template/catalog/product/view.phtml:
if($this->checkAttributeSet($_product, 'Monitors')):
echo $this->getLayout()->createBlock('cms/block')->setBlockId('monitor')->toHtml();
elseif($this->checkAttributeSet($_product, 'Footwear')):
echo $this->getLayout()->createBlock('cms/block')->setBlockId('footwear')->toHtml();
endif;
FOR THE ADMINS WHO DELETE USEFUL INFORMATION FOR POINTS ON STACKOVERFLOW >>>> 3rd time replying to this post with updated material ya know just incase someone stumbles on this thread.
This is the updated magento 2.3 way of completing this task.
Add code to
module-catalog/view/frontend/templates/product/view
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$attributeSet = $objectManager->create('Magento\Eav\Api\AttributeSetRepositoryInterface');
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($_product->getId());
$attributeSetRepository = $attributeSet->get($product->getAttributeSetId());
$attribute_set_name = $attributeSetRepository->getAttributeSetName();
//$attribute_set_name_arr[] = $attribute_set_name;
//echo '<pre>'; print_r($attribute_set_name);
if( !empty($attribute_set_name) && $attribute_set_name == 'Rc' ) {
// echo $this->getLayout()
->createBlock('Magento\Cms\Block\Block')
->setBlockId('rcitems')
->toHtml();
}
?>
setBlockId = The Identifier of the block in admin.
Rc = is the attribute set
no need to add to default.xml

How can I add an order item to an existing shipment programmatically in Magento?

I am creating a shipment in Magento from an import file that reads the order items to be shipped line by line, but after I create the shipment and add the first item I'm running into difficulty. I am creating the shipment like this:
$itemsData = array(
'qtys' => array(
$orderItem->getId() => $importData['qty placed'],
)
);
$shipment = Mage::getModel('sales/service_order', $order)
->prepareShipment($itemsData['qtys']);
$shipment->addComment("Shipment created from shipment importer", false);
$shipment->register();
$shipment->getOrder()
->setIsInProcess(true);
$shipment->setShippingCost($shipCost);
if (isset($trackingNumber)) {
$this->addTrack($shipment, $importData); //this function adds the tracking data
}
if ($order->getCustomerEmail()) {
$shipment->setEmailSent(true);
$shipment->sendEmail(true, '');
}
$transactionSave = Mage::getModel('core/resource_transaction')
->addObject($shipment)
->addObject($shipment->getOrder());
$transactionSave->save();
$shipment->save();
Which works great for the first item but for shipments with multiple items I am unable to add the next item to the shipment. I am trying to add subsequent items like this:
//I first load the existing shipment
$shipItem = Mage::getModel('sales/order_shipment_item');
$shipItem->setShipment($shipment);
$shipItem->setOrderItem($orderItem);
$shipItem->setQty($qtyShipped);
$shipItem->register();
$shipment->addItem($shipItem);
$transactionSave = Mage::getModel('core/resource_transaction')
->addObject($shipment)
->addObject($shipment->getOrder());
$transactionSave->save();
$shipment->save();
Now in the Order View / Information page in admin, the second item still says 'invoiced' and not 'shipped', when I click the Shipments tab the total quantity is listed as '1' instead of 2, however when I click on that shipment to view the details, both items are listed. So it seems like it is kinda working, but not really
Not all items will ship so I can't just load the order and mark them all as shipped.
Any ideas on how I can make this work? Thanks in advance ;)
I'm having the same issue. After reading around on stackoverflow and Magento forums I have found out that the shipment api should support shipments of single items. However, it seems that your code just does not support this.
Maybe I would be best to try using the shipment API of Magento?
http://www.magentocommerce.com/wiki/doc/webservices-api/api/sales_order_shipment
Get your existing shipment model from the order
$mageShipColl = $mageOrder->getShipmentsCollection();
$mageShipment = $mageShipColl->getFirstItem();
$mageShipment->setOrder($mageOrder)
->setStoreId($mageOrder->getStoreId())
->setCustomerId($mageOrder->getCustomerId())
->setBillingAddressId($mageOrder->getBillingAddressId())
->setShippingAddressId($mageOrder->getShippingAddressId());
Mage::helper('core')->copyFieldset('sales_convert_order', 'to_shipment', $mageOrder, $mageShipment);
Now do this:
$mageShipmentItem = $convertor->itemToShipmentItem($mageOrderItem);
$mageShipmentItem->setQty($qty_you_shipped);
//register shipment item for existing shipment only
if($mageShipment->getId()) {
$mageShipmentItem->register();
}
$mageShipment->addItem($mageShipmentItem);
$mageTrack = Mage::getModel('sales/order_shipment_track');
try {
$mageTrack->setData('carrier_code', $carrier)
->setData('number', $trackNo)
->setData('title', $carrierTitle);
$comment = "Shipped via $carrier with tracking# $trackNo. Emailed confirmation to $shipToEmail.";
$mageShipment->addTrack($mageTrack);
//Do not register an existing shipment
if(!$mageShipment->getId()) {
$mageShipment->register();
}
$mageShipment->addComment($comment, $shouldNotifyCust);
//this may or may not be needed
$mageShipment->getOrder()->setIsInProcess(true);
$transactionSave = Mage::getModel('core/resource_transaction')
->addObject($mageShipment)
->addObject($mageShipment->getOrder())
->save();
//email customer only if order is complete
if($mageOrder->getStatus() == "complete") {
$mageShipment->sendEmail(true, (($this->includeCommentInCustomerEmail) ? $comment : ''));
$mageShipment->setEmailSent(true);
}
$mageShipment->save();
} catch(Exception $e) {
$errMsg = "Error saving shipment: " . $e->getMessage();
Mage::log($errMsg, null, $logPath);
}

Resources