Editing product page with dimsav/laravel-translatable - laravel

im using a package called Laravel-Translatable
but is giving me more problemns that i expect, mainly with quite simple tasks. For example i have a list of all records (products), and each of them haves 2 languages translated (en,es). But now i need to edit the product information to put in the inputs fields, and for this i wish in my edit page get all the translated details (title, description), but for some reason, is returning me only one language, it doesnt return all the translated details from a specific product:
ex: return Product::where('id', '2')->get();
Somebody uses this package?

Just read the documentation. Therein is all that you need.
Some example:
$product = Product::where('id', 2)->get();
$product->translate('de')->title = "Germany title";
$product->translateOrNew('pl')->title = "Polish title";
//Shortcut
$product->{'title:pl'} = 'lorem ipsum';
$product->save(); //It Will save all translations and main model

Related

How to convert CodeIgniter urls into user friendly ones?

I've explored lot of questions and articles regarding this but I can't find how to get this done.
I'm doing a website which provides specifications of several products such as phones, tablets, tv etc. Here's what I've:
Controller - Specs (create and display specification of all products)
Method - Display (fetches detailed specs of selected model and shows)
Method - Index (lists names of all models stored in the table. this is where I build anchor links)
Display method takes three arguments (1, 2, 3).
1 - Type of product (Phones, Tablets, TV etc)
2 - Model Slug (iphone-6, galaxy-tab-s3, bravia-kdl-50w800d etc)
3 - Model ID (1, 4, 13 etc)
My URLs right now are like this:
localhost/sitename/specs/display/phones/iphone-6/1
localhost/sitename/specs/display/tablets/galaxy-tab-s3/4
localhost/sitename/specs/display/tv/bravia-kdl-50w800d/13
What I want to achieve is URLs which are like this:
localhost/sitename/iphone-6
localhost/sitename/galaxy-tab-s3
localhost/sitename/bravia-kdl-50w800d
I don't mind restructuring my tables/controllers/methods or anything else if this can be achieved using whatever.
Thanks for reading.
Edit:
Route.php
$route['default_controller'] = 'Specs/index';
$route['404_override'] = 'Errors/show_404';
$route['translate_uri_dashes'] = FALSE;
This is how I'm building the anchor links (view_file->index.php, called from Index method):
<?php
foreach model(in the table)
echo anchor(specs_controller.display_function.product_type.model_slug.model_id, model_name);
end foreach
?>
I can get the desired URLs with following code in route.php. Only problem is I'm not able to make the 'urlController/urlMethod' return a value in the function which can be assigned to $result variable.
$route['(:any)'] = function ($1)
{
$result = 'urlController/urlMethod/'.$1;
return $result;
};
I'm not sure how to do this. Can someone suggest how I should call 'urlController/urlMethod'?
You could achieve it with CodeIgniter URI Routing. Considering
localhost/sitename/galaxy-tab-s3
maps to
localhost/sitename/specs/display/tablets/galaxy-tab-s3/4
And, model id i.e 4, in this case, is static with respect to galaxy tab s3, as you have not mentioned any such Id in the simplified URL.
My understanding is with every URL localhost/sitename/iphone-6, you need three details about the string 'iphone-6'. i.e. type of product, model-slug, model id. One way could be write something like
$route['sitename/(:any)'] = 'routingController/commonRoutingMethod/$1';
Here, create a new routingController and write some logic into commonRoutingMethod() method, which takes the string like iphone-6 and fetches its all three details i.e. product type, model id etc. And then redirects by building the exact URL using
header('Location: http://localhost/sitename/specs/display/$productType/$modelSlug/$modelId/');
NOTE : There could be more forward ways just using regex match in routes.php, given that you create diffrentiated structure of the string, based on product type and model id e.g p_iphone-6_1 or t_galaxy-tab-s3_4.
Please use below routing code, to achieve it.
localhost/sitename/specs/display/phones/iphone-6/1
localhost/sitename/specs/display/tablets/galaxy-tab-s3/4
localhost/sitename/specs/display/tv/bravia-kdl-50w800d/13
localhost/sitename/iphone-6
localhost/sitename/galaxy-tab-s3
localhost/sitename/bravia-kdl-50w800d
$route['(:any)'] = 'specs/display/$1/$1/$1';
Let me know if you need any help.

Importing multiple categories on single products

My problem is getting the correct menu structure when importing my data.
I use Magento 1.9.2.2
I recieve my data in CSV format like this:
"sku","Cat1","Cat2","Cat3","Cat4"
"001","Plumbing","Pressfittings & pipes","Unipipe - MLC","Clutch"
"002","Tools","Handtools|Pipetools","Pipetools|Pipecutters & Scissors","Plastic scissors"
"003","Tools|Plumbing","Handtools|Pipetools|Pipes & Fittings","Pipetools|Calibration|Alupex fittings & tubes","Calibration tools|Tools for alupex"
I have made a small program to strip away the "|" and what comes after, so that:
"002","Tools","Handtools|Pipetools","Pipetools|Pipecutters & Scissors","Plastic scissors"
becomes:
"002","Tools","Handtools","Pipetools","Plastic scissors"
But I would love to create all the underlaying categories, so I also get this for sku 002:
"002","Tools","Pipetools","Pipecutters & Scissors","Plastic scissors"
I believe that the structure is used by Magento somehow, but I have a hard time figuring out how to import it.
I have tried normal import for Magento after creating the categories manually, and this did not work.
I have also tried creating them with Magmi, but I cannot get Magmi to work with the multiple main and subcategories either.
Has anyone seen this data format and have a clue in the right direction, on how to get it imported to a menu structure?
I have 2500 main and subcategories all together, so manually creating them simply wont work.
Ideas, questions or comments are welcome! :)
The MAGMI 'on-the-fly category importer' will certainly do this for you. I recommend you read the instructions on the MAGMI wiki very very carefully: all the information you need is there but I frequently find I have to double-check and re-read the details and examples on the MAGMI wiki.
MAGMI instructions for download install and running : http://wiki.magmi.org/index.php?title=Main_Page#Download_and_Install_Magmi
MAGMI instructions for on-the-fly category importer : http://wiki.magmi.org/index.php?title=On_the_fly_category_creator/importer
If you haven't used MAGMI before then work with a simple CSV to start with to ensure your install is set up correctly and you are able to import a csv that creates a simple product.
I think the main challenge you face is converting the CSV file format described in your question into a CSV format that MAGMI can work with. Alternatively you can write code in MAGMI to convert the CSV data but I think that might be overly complicated if this is a one-off process.
I'm not sure that I follow the syntax of your CSV example but if the first string is always a top category and then the pipe symbol means 'subcategory follows' then for
"003","Tools|Plumbing","Handtools|Pipetools|Pipes & Fittings","Pipetools|Calibration|Alupex fittings & tubes","Calibration tools|Tools for alupex"
you seek to make a MAGMI CSV like this
"sku","categories"
"003","Tools::1::1::1/Plumbing::1::0::1;;Handtools::1::1::1/Pipetools::1::0::1/Pipes & Fittings::1::0::1;;Pipetools::1::1::1/Calibration::1::0::1/Alupex fittings & tubes::1::0::1;;Calibration tools::1::1::1/Tools for alupex::1::0::1"
The obscure ::1::1::1 indicates is_active, is_anchor and include_in_menu respectively and can be omitted.
It's none of my business but I am deeply concerned by the two spaces in the subcategory label 'Tools for alupex' (don't start me on the lower-case Alupex or AluPEX)
You have to create those categories programatically, not possible by import (maybe via magmi but I don't know). Place a script into your magento root and run it. This is how you could proceed (needs polishing):
$category = Mage::getModel('catalog/category');
$f = fopen($file, "r");
$row = 0;
while ( ($data = fgetcsv( $f, 0, ",") ) !== FALSE) {
$multiple_categories = explode("|", $data);
//save main ones
$category->setName($multiple_categories[0])
->setIsActive(1)
->setDisplayMode('PRODUCTS')
->setIsAnchor(1)
->setAttributeSetId($category->getDefaultAttributeSetId()); //or the id of your attribute set, this is important.
$category->save();
unset($category);
//now deal with children
if(count($multiple_categories) > 1){
i = 1;
foreach($multiple_categories as $subcategory){
$category->setName($multiple_categories[i])
->setIsActive(1)
->setDisplayMode('PRODUCTS')
->setIsAnchor(1)
->setAttributeSetId($category->getDefaultAttributeSetId());
//manage parents now
$_category = Mage::getResourceModel('catalog/category_collection')
->addFieldToFilter('name', $multiple_categories[i-1])
->getFirstItem(); //this needs more work
$parentId = $_category->getId();
$parentCategory = Mage::getModel('catalog/category')->load($parentId);
$category->setPath($parentCategory->getPath());
$category->save();
unset($category);
i++;
}
}
$row++;
}

Programmatically modify related products in magento

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)
);

How To Use Magento Collections to Run Query, Get Results, and Print Them

I am fairly new to programming, php, Magento, and most of all, SQL. Please forgive me if this is somehow a dumb question.
I am trying to use Magento collections to select two different columns in two different tables and join them. These two columns contain product numbers that follow the same conventions and my goal is to get and display the product numbers that field a (lets call it 'product_id') contains that field b ('item_nr') does not.
Here is my function so far, located currently in a model that will be called by a controller action.
public function importCompare() {
$orderlistCollect = Mage::getModel('personal_orderlist/orderlist')->getCollection()
->addFieldToSelect('product_id')
->addFieldToFilter('b.item_nr', null)
->getSelect()
->joinLeft( array('b'=>$this->getTable('catalog/product')), 'main_table.product_id = b.item_nr', array('b.item_nr'));
echo $orderlistCollect;
}
By echoing the variable, I get the following query.
SELECT `main_table`.`product_id`, `b`.`erp_item_nr`
FROM `mag1personal_orderlist` AS `main_table`
LEFT JOIN `` AS `b` ON main_table.product_id = b.item_nr
WHERE (b.item_nr = '')
This looks fairly close to what I want, however the big problem is that I have no idea how to retrieve the information I should derive from this query and echo it. I've tried to use a variety of collection manipulation methods to no avail. Is it easy to use them out of order as well?
Any help here is appreciated.
Collections use interfaces from PHP's standard library to implement for each-able behaviors.
If that sounded like greek, give this a try.
foreach($orderlistCollect as $item)
{
$data = $item->getData();
var_dump($data);
$sku = $item->getSku();
var_dump($sku);
$sku = $item->getData('sku');
var_dump($sku);
}
If you're interested in learning how to do with with your own PHP objects, the Object Iteration section of the manual is a good place to start. However, there's no need to dive deep on this — just treat a collection like you would an array of objects, and you'll be fine.

Magento - get results view HTML for a collection of products

I get a list of magento ids from a web service. I load these into and array $product_ids, so I have something like this:
Array
(
[0] => 1965
[1] => 3371
[2] => 1052
)
I can then make this into a collection:
$collection = Mage::getModel('catalog/product')->getCollection()
->addIdFilter($product_ids);
Using my Magento inspector, I've seen that the category pages use the class Mage_Catalog_Block_Product_List to display lists of products. I'd like to do something similar in my class. I've tried loading:
$ProductList = new Mage_Catalog_Block_Product_List();
$ProductList->setCollection($collection);
And then I've tried to load the HTML of the results as follows:
$CollectionHTML = $ProductList->_toHtml();
But $CollectionHTML is empty.
How would I get the HTML of what you see in the list view (i.e. the generated output of frontend/base/default/template/catalog/product/list.phtml, but given my collection)?
Making the code work the right way is much more easier in Magento than trying to work with ugly legacy code. I would gladly help you make the code the proper way when you have specific questions. Also, in the longterm, technical debt is gonna cost alot more.
Anyway, back to your issue.
In Magento block are not instantiated like in any app $myvar = new className ... almost never. This tutorial can help you understand better Magento's layout and blocks.
But if you want to create a block a way to do it is:
$block = Mage::getSingleton('core/layout')->createBlock('catalog/product_list')
Now related to your product collection you should check how Mage_Catalog_Block_Product_List::_getProductCollection actually works, because it uses the layered navigation, not a simple product collection.
Further, assuming that at least you are using a Magento controller and you are within a function, the following code will display the first page of products for a specified category:
//$category_id needs to be set
$layout = Mage::getSingleton('core/layout');
$toolbar = $layout->createBlock('catalog/product_list_toolbar');
$block = $layout->createBlock('catalog/product_list');
$block->setChild('toolbar', $toolbar);
$block->setCategoryId($category_id);
$block->setTemplate('catalog/product/list.phtml');
$collection = $block->getLoadedProductCollection();
$toolbar->setCollection($collection);
//render block object
echo $block->renderView();
Displaying specific ids:
you use root category id for $category_id variable (also make sure that display root category is set (or another category id that contains your product ids)
you can hook into catalog_block_product_list_collection event to add your ID Filter to the collection (this is called in _beforeToHtml function)
But, all this construction is not solid and there are still some points that require attention (other child blocks, filters and so on)

Resources