I have a Magento webshop with several products in it. All of them begin with the word PRODUCT,
for example
PRODUCT 001.
I want to replace the word "PRODUCT" with "Article".
I tried to write a small script, but it's not working at all. Here is what I've got so far:
<?php
$_productCollection=$this->getLoadedProductCollection();
$_helper = $this->helper('catalog/output');
foreach($_productCollection as $_product)
{
$name = $_product->getName();
$new = str_replace("PRODUCT","Article", $name);
$_product->setName($new);
$_product->save();
};
?>
I did not really know how to run the script, so I added this into a CMS page and opened it:
{{block type="core/template" template="script.phtml"}}
What's wrong with it?
Create a file Replacename.php inside your magento root folder
and write the below code in that file and then execute it with below URL http://www.yourdomain.com/Replacename.php
<?php
require_once('app/Mage.php');
umask(0);
Mage::app();
$_productCollection = Mage::getModel('catalog/product')
->getCollection()->addAttributeToSelect('*');
foreach($_productCollection as $_product)
{
try
{
$name = $_product->getName();
$new = str_replace("PRODUCT","Article", $name);
$_product->setName($new);
$_product->save();
}
catch(Exception $e){
echo $e->getMessage();
}
}
Related
I try to fetch the media assets from products in
I am using https://github.com/akeneo/api-php-client-ee (v6) and know about https://api.akeneo.com/api-reference-50.html
This is what I came up so far:
$searchBuilder = new \Akeneo\Pim\ApiClient\Search\SearchBuilder();
$searchBuilder->addFilter('enabled', '=', true);
$searchFilters = $searchBuilder->getFilters();
$products = $client->getProductApi()->all(100, ['search' => $searchFilters]);
foreach ($products as $product) {
foreach($product['values']['assets'] as $assetData) {
foreach($assetData['data'] as $code) {
echo $code;
$asset = $client->getProductMediaFileApi()->all();
var_dump($asset);
}
}
What I have tried / Questions:
I get a code like 1234_00 (if 1234 is the product number), but I do not know how to fetch the specific file from the product media file api. Do I have to filter here? How?
I tried to $client->getAssetMediaFileApi()->download($code) but the $code I have does not seem to be the full asset code (I get a 404 not found error)
How can I find out which assets are related to a specific product to download them or get the download URL?
This works - "bilder" is the Asset Family code in our case.
foreach ($products as $product) {
foreach($product['values']['assets'] as $assetData) {
foreach($assetData['data'] as $code) {
echo $code;
$assets = $client->getAssetManagerApi()->get('bilder', $code);
foreach($assets['values']['media'] as $dataLine) {
$download = $client->getAssetMediaFileApi()->download($dataLine['data']);
file_put_contents('/tmp/' . basename($dataLine['data']), $download->getBody());
}
foreach($assets['values']['variation_image'] as $dataLine) {
$download = $client->getAssetMediaFileApi()->download($dataLine['data']);
file_put_contents('/tmp/' . basename($dataLine['data']), $download->getBody());
}
}
I found the main clue, by looking at the Akeneo Admin Panel and which requests it does :-)
I need to display product details on my custom page,
Code:
<?php
set_time_limit(0); //THIS SCRIPT JUST INITIALS THE PROFILE TO BE RUN VIA MAGENTO ADMIN "RUN PROFILE IN POPUP". Its the same thing as click just via this file that you can run via cron
$profileId = 1; // SYSTEM - IMPORT/EXPORT - DATAFLOW PROFILES PROFILES <-- you need to go into your magento admin and grab the exact profile ID
require_once 'app/Mage.php';
umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$sku = 10; //this sku you get it from your text box.
$_product = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect(array(
'name',
'sku',
'price',
'thumbnail'
))->addAttributeToFilter('status', 1)->addAttributeToFilter('sku', array(
'in' => $sku
));
$imageHelper = Mage::helper('catalog/image');
foreach($_product as $prod)
{
$name = $prod->getName();
$price = $prod->getPrice();
$thumbnail = $imageHelper->init($prod, 'thumbnail')->resize(150, 220);
}
echo "Name: ", $name;
echo "<br />";
echo "Sku: ", $sku;
echo "<br />";
echo "Price: ", $price;
echo "<br />";
echo "Image: ", $thumbnail; ?>
How can I display product image instead of image path.
I saw your original question with the code and I think you're using Magento
So you can do it like this
Load product by ID
Mage::getModel('catalog/product')->load(.....)
Or get all products
Mage::getModel('catalog/product')->getCollection() .....
Get product image
Mage::helper('catalog/image')->init($_product, 'image') // You can change to another image code
Or get all media images
$_product->getMediaGalleryImages() // It's array, you need to loop through it to get image detail
Display like image instead a url
<img src="<?php echo $image_url ?>">
Take your time and read about this HTML Images
Here is my answer:
echo "<img src='".$thumbnail ."'>";
Thank You #Hung Vo
On a custom page within Magento, I have a simple AJAX Post which passes a product ID to a php script:
jQuery.ajax({
url: 'https://www.mywebsite.com/test/add_to_basket.php',
type: "POST",
data: data,
success: function (data) {
,
error: function (data) {
}
});
Here is the add_to_basket php script:
$i = $_POST['i'];
require_once '../app/Mage.php';
umask(0);
Mage::app();
Mage::init('default');
Mage::getSingleton('core/session', array('name' => 'frontend'));
$session = Mage::getSingleton('customer/session');
$cart = Mage::getSingleton('checkout/cart');
$cart->init();
$cart->addProduct($i, 1);
$session->setCartWasUpdated(true);
$cart->save();
This works perfectly, however the mini cart doesn't update. I've read that I need to create a sections.xml file within etc/frontend like so:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Customer:etc/sections.xsd">
<action name="[frontName]/[ActionPath]/[ActionName]">
<section name="cart"/>
</action>
</config>
However I'm not sure what the [frontName]/[ActionPath]/[ActionName] would be in my example. What is the best course of action?
the most important thing – ajax.php :
require_once('/var/www/clients/client0/web1/web/app/Mage.php'); // ABSOLUTH PATH TO MAGE
umask(0);
Mage::app ();
Mage::getSingleton('core/session', array('name'=>'frontend')); // GET THE SESSION
$simbol= Mage::app()->getLocale()->currency(Mage::app()->getStore()->getCurrentCurrencyCode())->getSymbol(); // GET THE CURRENCY SIMBOL
$store=Mage::app()->getStore()->getCode(); // GET THE STORE CODE
$cart = Mage::getSingleton('checkout/cart'); //->getItemsCount();
$ajtem=$_POST['item']; // THIS IS THE ITEM ID
$items = $cart->getItems();
foreach ($items as $item) { // LOOP
if($item->getId()==$ajtem){ // IS THIS THE ITEM WE ARE CHANGING? IF IT IS:
$item->setQty($_POST['qty']); // UPDATE ONLY THE QTY, NOTHING ELSE!
$cart->save(); // SAVE
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
echo '<span>';
if($store=='en') echo $simbol;
echo number_format($item->getPriceInclTax() * $_POST['qty'],2);
if($store=='hr') echo ' '.$simbol;
echo '</span>';
break;
}
}
// THE REST IS updatTotalG FUNCTION WHICH IS CALLED AFTER AJAX IS COMPLETED
// (UPDATE THE TOTALS)
echo '<script type="text/javascript">';
echo 'function updateTotalG(){';
echo 'jQuery("#sveUkupno").html(\'';
echo '<strong><span>';
//echo 'JQuery(\'#sveUkupno\').html("<strong><span>';
if($store=='en') echo $simbol;
echo number_format(Mage::getSingleton('checkout/session')->getQuote()->getGrandTotal(),2);
//echo $simbol . ' </span></strong>");';
if($store=='hr') echo ' '.$simbol;
echo " </span></strong>');";
echo '} </script>';
You can see that we detect the currency symbol in the script and the
store that is in use. At the end of the script it generates
updateTotalG script that we use for listing cart quantity value. The
value comes from Magento.
Hello I have just started to learn magento. Now I was trying to import category in magento through script.
my magento code looks like
<?php
require_once 'businessclasses.php';
define('MAGENTO', realpath(dirname(__FILE__)));
require_once MAGENTO . '/app/Mage.php';
umask(0);
$count = 0;
echo "<pre>";
$data= new getCSV();
$rows=$data->getRootCategories(); // Gets the list of root categories.
foreach($rows as $row) {
echo $categoryName = $row['d']; // Name of Category
// Create category object
$category = Mage::getModel('catalog/category');
$category->setStoreId(1); // 'US-Store' store is assigned to this category
$rootCategory['name'] = $categoryName;
$rootCategory['path'] = "23/25"; // this is the catgeory path
$rootCategory['display_mode'] = "PRODUCTS";
$rootCategory['is_active'] = 1;
$category->addData($rootCategory);
try {
$category->save();
echo $rootCategoryId = $category->getId();
}
catch (Exception $e){
echo $e->getMessage();
}
}
?>
This code runs without an error and also this line
echo $rootCategoryId = $category->getId();
prints unique IDs for each category (loop) but when i see at the admin pannel it shows me nothing. Like no category imported.
I have reffered this Question. but it is not helping.
any help would be appreciated. Thank you.
Hey guys I got an answer.
$rootCategory['path'] = "1/23/25"; // this is the catgeory path <-- I was giving this path wrong.
Well you've got your answer yourself. However, if anyone looking for some other tools for category and product and other import/export, I recommend Magmi http://sourceforge.net/projects/magmi/, awesome tool saved me a lot of time .
I'm trying to get Wordpress to include a different template file, within single.php, if it sees a file matching the slug.
The file does exits, path is correct, safe_mode set to OFF...am I missing something?
$dir=get_bloginfo('stylesheet_directory').'/post_tmpl/';
$categories=get_categories();
foreach($categories as $cat){
if(is_file($dir.$cat->slug.".php")){
require($dir.$cat->slug.".php");
}else{
require($dir."default.php");
}
}
can you try with this code?
$dir=get_bloginfo('stylesheet_directory').'/post_tmpl/';
$categories=get_categories();
foreach($categories as $cat){
$temp = $dir.$cat->slug;
if(is_file($temp.".php")){
require($temp.".php");
}else{
require($dir."default.php");
}
}
Thanks