Magento - can't delete programmatically imported pictures - magento

I hase used this code :
require_once("app/Mage.php");
Mage::app();
$fullImagePath = "app/code/local/Mycompany/Import/data/images/test_picture.jpg";
$product = Mage::getModel('catalog/product');
$product->load(16);
$product->setMediaGallery (array('images'=>array (), 'values'=>array ()));
$product->addImageToMediaGallery ($fullImagePath, array ('image'), false, false);
$product->save();
to import pictures to a product.
The problem in that is that when I delete the programmatically imported picture from the backend, it stay always visible in frontend.
I don't know what's wrong in my code.
Thanks for help.

I think in your import code instead of doing setMediaGallery() and hard coding your image url you should,
$config = $product->getMediaConfig();
//and get your file url from the set media path in your config.
$fileurl = $config->getMediaUrl('sth/test_picture.jpg');

Related

How to Create a Category with Custom Module Installation? [duplicate]

I actually can add a category via setup script, the thing is for some reason some of the fields doesn't get set properly. Here's is my code
$this->startSetup();
Mage::register('isSecureArea', 1);
$category = Mage::getModel('catalog/category');
$category->setPath('1/2') // set parent to be root category
->setName('Category Name')
->setUrlKey('category-name')
->setIsActive(0)
->setIncludeInMenu(1)
->setInfinitescroll(1)
->setDisplayMode('PAGE')
->setLandingPage($idToCmsBlock)
->setPageLayout('anotherLayoutThanDefault')
->setCustomUseParentSettings(0)
->setCustomLayoutUpdate('<reference name="head"><action method="addCss"><stylesheet>css/somecss.css</stylesheet></action></reference>')
->save();
$this->endSetup();
After running this script, I have a category created with all my value set in the EAVs table.
However the Flat table will be missing displayMode, landingPage, pageLayout, customLayoutUpdate even if I re-index the flat table.
The weird thing is that if I go in the admin, I can see all those fields properly set but if I go in my frontend most of those fields are ignored. I will have to go to the admin, unset those value and reset them for each of them to work properly.
Also let say I use setEnabled(1), my category will be "enable" in the admin but not show up in the frontend.
PS: I have Flat Category activated, if I disable it seems to work fine but if I re-index it still not working.
I finally found it, I'm not sure why but those fields are not showing up properly because they were inserted for the default store (storeId=1) because my script is running in an update script. You need to use the storeId 0.
With this information you would think that the solution would be something like :
$this->startSetup();
Mage::register('isSecureArea', 1);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$category = Mage::getModel('catalog/category');
$category->setPath('1/2') // set parent to be root category
->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID)
->setName('Category Name')
...
->save();
$this->endSetup();
But this code doesn't work either. Indeed after looking into Mage::app() (Mage_Core_Model_App Line 804) I noticed a IF condition that would always return the default store if you're in a setup script.
The trick is to fake that you're not in a setup script, my working solution is:
$this->startSetup();
Mage::register('isSecureArea', 1);
// Force the store to be admin
Mage::app()->setUpdateMode(false);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$category = Mage::getModel('catalog/category');
$category->setPath('1/2') // set parent to be root category
->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID)
->setName('Category Name')
...
->save();
$this->endSetup();
I ran into the same issue when updating a category via a data install script. The solution provided in the accepted answer did work for updating the category, but can be improved upon as follows:
In the solution, the user that triggers the update script is forced to the admin environment. This can be remedied by saving the current store id and switching back at end of the script.
It doesn't seem that adding isSecureArea to the registry or disabling update mode had any use (at least for the use case of updating a category).
I ended up with the following data install script for updating a category (in this example, a category is loaded by name, after which the name is updated):
<?php
$this->startSetup();
//Switch to admin store (workaround to successfully save a category)
$originalStoreId = Mage::app()->getStore()->getId();
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
//update category
$category = Mage::getModel('catalog/category')
->loadByAttribute('name', 'OLD_CATEGORY_NAME');
if ($category) {
$category
->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID)
->setName('NEW_CATEGORY_NAME')
->save();
}
//Set store to original value
Mage::app()->setCurrentStore($originalStoreId);
$this->endSetup();
?>
Try this
<?php
require_once "../app/Mage.php";
umask(0);
Mage::app('default');
$proxy = new SoapClient("http://127.0.0.1/magento/index.php/api/soap/?wsdl");
$sessionId = $proxy->login($magento_webservices_username, $magento_webservices_passwd);
$data = array('name'=>'Nokia',
'description'=>'',
'meta_description'=>'',
'meta_keywords'=>'',
'default_sort_by'=>'price',
'available_sort_by'=>'price',
'is_active'=>1
);
$newCategoryId = $proxy->call($sessionId, 'category.create', array(3, $data, 1));
echo "Category ID: ".$newCategoryId;
?>
And also have a look Magento create category
Take a look at this. Hope it will help you.
http://inchoo.net/ecommerce/magento/how-to-add-new-custom-category-attribute-in-magento/
I have created multiple categories via installer script.
<?php
$installer = $this;
$installer->startSetup();
Mage::register('isSecureArea', 1);
$category = Mage::getModel('catalog/category');
$category->setPath('1/2/4') // set parent to be root category
->setName('CAT NAME') //Category Name
->setIsActive(1) // Category Status
->setIncludeInMenu(1) // Show in Menu
->setIsAnchor(1) // used for Layered navigation
->setDisplayMode('PAGE') // Product Only
->setPageLayout('one_column') // Page layout
->save();
$installer->endSetup();

how to get product images url for storeid

I've created a magento product with no default (Admin) images, but
I've properly setted an image as thumbnail, small_image and imageset for the "italian" store view.
If I use the following code in a phtml files I'get the correct image url:
echo "url = " . Mage::helper('catalog/image')->init($product, 'thumbnail')->resize(95,95)
I get :
media/catalog/product/cache/6/image/80x/9df78eab33525d08d6e5fb8d27136e95/1/9/1901186_10152274267007184_1589016979_n_7.jpg
Inside a controller for an ajax callback fucntion I use the following:
$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
echo "url = " . Mage::helper('catalog/image')->init($product, 'thumbnail')->resize(95,95);
but I alwais get the placeholder image
/media/catalog/product/cache/6/thumbnail/95x95/9df78eab33525d08d6e5fb8d27136e95/placeholder/websites/3/LOGO_1.png
I try to set the product storeId before loading the product data but no result:
$product = Mage::getModel('catalog/product');
$product = $product->setStoreId(6);
$product = $product->loadByAttribute('sku',$sku);
echo "vediamo " . Mage::helper('catalog/image')->init($product, 'thumbnail')->resize(95,95);;
Thank for any helps and sorry for my english.
Giuseppe
This is working for me
$cathlpimg = new Mage_Catalog_Helper_Image();
echo $cathlpimg->init($_item, 'image')->resize(200);
I'm surprised to see that didn't work - the key thing for me was to specify the store ID before calling load(), but I can see you already tried that.
Although, strictly speaking, you didn't use load(), you used loadByAttribute(). Maybe it doesn't work in quite the same way. Have you tried:
$product = Mage::getModel('catalog/product');
$product = $product->setStoreId(6);
$product = $product->load($id);
If you only know the sku and not the ID, just use this:
$id = Mage::getModel('catalog/product')->getIdBySku($sku);

Hide Products without images magento

I have followed this answer to hide products without images on the category listing page. It worked nicely for a while.
Now, for some reason it seems the products without images are still showing up on the listing page.
Any ideas as to why this may be happening?
Note:
The same list.phtml page is being used.
Thank you.
Add the following to list.phtm:
//$_productCollection=$this->getLoadedProductCollection();
$_productCollection = clone $this->getLoadedProductCollection();
$_productCollection->clear()
->addAttributeToFilter('small_image', array('neq' => 'no_selection'))
->load();
This answer recommended the following:
->addAttributeToFilter('image', array('neq' => 'no_selection'))
Whereas I have set it to:
->addAttributeToFilter('small_image', array('neq' => 'no_selection'))
The reason the previous answer did not work was because the product collection doesn't load the regular image, and therefore the regular image cannot be added as an attribute to filter, so instead, I added the small_image as the attribute to filter.
You can also try R.S's answer where he adds the image to the page and hence the collection. You may have to also add all attributes using:
->addAttributeToSelect('*')
There are some tricks to keeping Magento in line. One thing I've learned is that the Magento Model will change for many different reasons, and its kinda hard to figure out why. There are better ways to do this (modifying the collection, etc) but it sometimes just does not work and you don't have days to figure out why.
If you want a surefire way to make sure your image exists, use the following code... It may not be the 'magento way' but it works, and I tested it on my site (Magento EE 1.12). Put it in a function, or use it directly in your phtml if you want!
It basically just makes sure the URL exists.
$exists = false;
$entity_id = 8800;
$product = Mage::getModel('catalog/product')->load($entity_id);
$mediaUrl= Mage::getBaseUrl('media');
$imageUrl = $mediaUrl . "catalog/product" . $product->getImage();
$file_headers = #get_headers($imageUrl);
if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
$exists = false;
}
else {
$exists = true;
}
var_dump($exists);
var_dump($imageUrl);
echo '<img src="' . $imageUrl . '" />';
$exists will either be true (image does exist) or false (image does not)
I think that the issue is that you are trying to get the 'image' (base image) property on the list.phtml (by default i can only access the thumbnail, small_image).
On list.phtml (not loading the full product resource like on view.pthml)
echo $_product->getImage() //null
echo $_product->getThumbnail() // path/name.jpg
echo $_product->getSmallImage() // path/name.jpg
I think you may need to add adding something like this to app/design/frontend/default/yourtheme/layout/catalog.xml
<action method="addAttribute"><name>image</name></action>
See How to get access to custom Image Attribute in list.phtml in Magento

Getting image for bundle product child products, currently returning placeholder

My problem:
I need to retrieve the main product image from a cut down product object which is supplied by the class: Mage_Bundle_Model_Resource_Price_Index
My code demonstrating the issue:
$_product = $this->getProduct();
$_options = Mage::helper('core')->decorateArray($this->getOptions());
foreach($_options as $_option):
$_selections = $_option->getSelections();
foreach ($_option->getSelections() as $tmpsel) {
var_dump($tmpsel->getImageUrl());
}
Which returns my placeholder image:
http://dev.dev/media/catalog/product/cache/7/image/265x/0dc2d03fe217f8c83829496872af24a0/placeholder/default/logo_3.jpg
My horrible and hacky work around:
In order to get correct image Url, I have resorted to loading a completely new product object, which is terribly inefficient.
foreach ($_option->getSelections() as $tmpsel) {
$product = Mage::getModel("catalog/product")->load($tmpsel->getId());
$image = Mage::helper('catalog/image')->init($product, 'image');
var_dump($image."");
}
This returns correctly:
http://dev.dev/media/catalog/product/cache/7/image/0dc2d03fe217f8c83829496872af24a0/M/P/MP1428219-107-Main.jpg
What I want to do:
I want to be able to use the catalog/image helper with the selection ($tmpsel), but when I try I end up getting the placeholder image again.
foreach ($_option->getSelections() as $tmpsel) {
$image = Mage::helper('catalog/image')->init($tmpsel, 'image');
var_dump($image."");
}
Extra Info:
Anything I think of that could help I will add here
Cut down product object includes some reference to image
'small_image' => string '/M/P/MP1428219-107-Main.jpg'
'thumbnail' => string '/M/P/MP1428219-107-Main.jpg'
Description of getSelection()
In: ./app/code/core/Mage/Bundle/Model/Resource/Price/Index.php
* Retrieve bundle options with selections and prices by product
The function uses low level SQL to generate the collection, so I can always extend it to add options if needed, not sure which options though.
Thank you for reading, hope someone can give me a good way of doing this, will keep updated.
Init() method require a product object
init(Mage_Catalog_Model_Product $product, $attributeName, $imageFile=null)
Try
<?php echo $this->helper('catalog/image')->init($tmpsel, 'small_image')->resize(40,40); ?>
You do not have 'image' but you do have 'small_image' attribute
See Magento - how to retrieve bundled option images
To get the full web address you can use this code:
Mage::getSingleton('catalog/product_media_config')->getMediaUrl($product->getImage());
The limitation is that bring only the main image.
Oh wow, I guess I can thank you stack overflow for making me write my thoughts down.
After comparing the full product object and the semi product object I can see that I was calling the incorrect attribute name for catalog/image
$image = Mage::helper('catalog/image')->init($tmpsel, 'image');
Should have been
$image = Mage::helper('catalog/image')->init($tmpsel, 'small_image');

MAGENTO - Registration form (+validation and callback) outside magento files?

I'm trying to integrate a php file inside an aplication that gives users the posibility to register an account. This account has to be registered inside a magento store.
This is what i have to this momment:
<?php
require_once '../../../app/Mage.php';
umask(0);
Mage::app('default');
Mage::app()->getTranslator()->init('frontend');
$session = Mage::getSingleton('core/session', array('name' => 'frontend'));
// get layout object
$layout = Mage::getSingleton('core/layout');
//get block object
$block = $layout->createBlock('core/template');
//print_r(get_class_methods(get_class($block))); <- use for seeing classes
$block = $block->setTemplate('customer/form/register.phtml')->renderView();
echo $block;
?>
This code renders the registration form but stops when he is showing the input fields. I tried with "mini.login.phtml" and it renderes correctly. I'm not very good at Magento, or english. I can provide any other information if necessary.
Any help will be appreciated!
Regards
You are using the wrong block type when dynamically creating the block. Try using this:
createBlock('customer/form_register')

Resources