Magento - how to add new product status - magento

I'm trying to make new product statuses but i can't figure out how to do it and all the stuff set on the web is not consistent or simply talks about order status which i don't want to change.

What is your motivation to have new product statuses? I think it's little bit risky to change this part of app. I suggest you to add new attribute and use this one instead system product's attribute 'status', this attribute tells to system if product is enabled or disabled. I guess there is nothing between :)

Override class Mage_Catalog_Model_Product_Status to the local folder. Then open the file
\app\code\local\Mage\Catalog\Model\Product\Status.php
At the top of the file you can see the constants
const STATUS_ENABLED = 1;
const STATUS_DISABLED = 2;
Add your custom status below them, for example
const STATUS_SUSPENDED = 3;
Then edit the function getOptionArray
static public function getOptionArray()
{
return array(
self::STATUS_ENABLED => Mage::helper('catalog')->__('Enabled'),
self::STATUS_DISABLED => Mage::helper('catalog')->__('Disabled'),
self::STATUS_SUSPENDED => Mage::helper('catalog')->__('Suspended')
);
}
That's it. Don't forget to clear the cache.

Related

Create handle with 2 variables

I would like to create a layout specified for only simple product with attribute set id of 4.
currently my code is something like this:
public function initProductLayout(ResultPage $resultPage, $product, $params = null)
{
$resultPage->addPageLayoutHandles(
['attribute_set_id' => $product->getAttributeSetId()]
);
and i have this layout catalog_product_view_attribute_set_id_4.xml
its all good in the products page but my problem is that catalog_product_type_bundle.xml is not being used. So I would like to have a layout file which is like catalog_product_type_bundle_attribute_set_id_4.xml if it is possible.
It is possible to get the layout file you are talking about. To do this you should add this code in addition to one you already have
$resultPage->addPageLayoutHandles(['type_'.$product->getTypeId().'_attribute_set_id' => $product->getAttributeSetId()]);
As a result you will get this

How to change magento config programatically for different stores?

I have a list of custom options in magento config and I need to change them programmatically when an user clicks on "Save Config". In generally it's not a big problem to get it done using observer but I need to have one option like "Pack of options" and if user choose one of package appropriate options must be changed programatically. It's not a big deal too:
Mage::getConfig()->saveConfig('path/to/config/', 1, 'default', 0);
Mage::getConfig()->saveConfig('path/to/config2/', 1, 'default', 0);
But the problem is that I can't get it working for different stores, it's working for default scope only. Example:
Mage::getConfig()->saveConfig('path/to/config2/', 1, 'german', 0);
isn't working.
How can I update options programatically for certain stores only? So the user could check witch store he wants to apply options too?
Thanks for any help.
Try this:
$storeCode = 'german';
$store = Mage::getModel('core/store')->load($storeCode);
$path = 'path/to/config';
$config = Mage::getModel('core/config_data');
$config->setValue('Your value here');
$config->setPath($path);
$config->setScope('stores');
$config->setScopeId($store->getId());
$config->save();
Look at the definition of the function :
public function saveConfig($path, $value, $scope = 'default', $scopeId = 0)
The scope can actually take only 3 values : 'default', 'stores' or 'websites'.
Let's suppose your german store has 3 as Id, here is the code that would work :
$config = Mage::getModel('core/config');
$config->saveConfig('path/to/config/', 'value_to_set', 'stores', 3);
For a website-scoped config :
$config->saveConfig('path/to/config/', 'value_to_set', 'websites', $websiteId);

ZF2 empty session container between pages

After looking around the internet for days, i decided to ask your help here.
I got a problem with Zend Framework 2 session container management. I don't understand why, but the framework emptied all my containers each time i'm changing page.
My case is simple, i'm building an online shop :
The customer is on a product and click the "add to cart" button
The product is saved to session
The customer get back to the products list to choose another product ... but there is no product anymore in his cart.
Here is a piece of code :
// Create container to add product
$container = new Zend\Session\Container('frontCart');
// Add product to cart
$container->offsetSet('frontCartContent',
array(1 => serialize($my_product_object));
If i make a debug of the session just after added :
Debug::dump($_SESSION);
// Display this :
["frontCart"] => object(Zend\Stdlib\ArrayObject)#70 (4) {
["storage":protected] => array(1) {
["frontCartContent"] => array(1) {
[1] => string(1175) "my serialized product object"
}
}
["flag":protected] => int(2)
["iteratorClass":protected] => string(13) "ArrayIterator"
["protectedProperties":protected] => NULL
}
Then, if i simply reload the page, or if switch from :
http://mydomain.com/products_list/my_product
to
http://mydomain.com/products_list
I get :
Debug::dump($_SESSION);
// Display this :
["frontCart"] => NULL
Please, help :-(
I don't understand at all why ZF2 has this behavior, and this is very problematic for an online shop customer if he can't add and by products.
Thx
EDIT
Following Tim's demand here is more code.
I initialize my session container in the controller's constructor
public function __construct()
{
if (!$this->sessionCart)
{
$this->sessionCart = new Container(ConstantSession::FRONT_CART);
}
}
Then, here is the exact way i'm adding the product to the container
$this->sessionCart->offsetSet(ConstantSession::FRONT_CART_CONTENT,
array($cartNumber => serialize($product))
);
$cartNumber is incremented following the number of products in the cart (when it'll work).
$product is an object with all its properties.
EDIT 2
Following Tim's advises i changed my "add to cart" code to :
$this->sessionCart->frontCartContent = array($cartNumber => $product);
When i want to get back my session content i create a new instance of Container :
// Init new container
$container = new Zend\Session\Container('frontCart');
// Get the content
$container->frontCartContent;
If i make a Debug::dump() of the last line, i still get NULL after changing page.
There are a few issues with your code. Try:
// Create container to add product
$container = new Zend\Session\Container('cart');
// Add product to cart
$container->frontCartContent = array($my_product_object);
then on the other page, you need to create the container again with the same parameter you used above, and then check the contents. Don't just called $_SESSION:
$container = new Zend\Session\Container('cart');
var_dump($container->frontCartContent);
See if that gives you better results.

Translating customer prefixes in Magento

I'm trying to translate the prefix like 'mr.' and 'mrs.' those are set in: System >> Configuration >> Customer Configuration >> Prefix Dropdown Options
I need it in English, Dutch and German. Each language is a separate storeview.
I've added the translation to multiple .csv files like the themes translate.csv and the Mage_Core.csv
The default <?php echo $this->__($prefix) ?> works on the frontend like the checkout. But in the backend and emails it isn't translated.
Any way to translate those?
I found two ways to fix this.
remove the build in prefix and add a new one with custom prefixes.
overrule the Mage_Customer_Helper_Data class and it's getNamePrefixOptions method.
I used Nr2
NR 1: is more work, you will need to update tempaltes. But it is easier to manage for a customer if needs to ad a new translation.
NR2: You can add more prefix values in the config. Then in the class make a switch which to use in which store/language. Adding a new language will probably need to add more code the the class.
What I used to overrule the getNamePrefixOptions
public function getNamePrefixOptions($store = null)
{
$pre_val = $this->_prepareNamePrefixSuffixOptions(
Mage::helper('customer/address')->getConfig('prefix_options', $store)
);
// Show all prefixes in the admin
if (Mage::app()->getStore()->isAdmin()) {
return $pre_val;
}
// Transform array keys to integers
$pre_val = array_values( $pre_val );
$lang_code = Mage::app()->getLocale()->getDefaultLocale();
$new_prefixes_values = array();
// Add language codes here, and add their prefix key's, count starts at 0
switch ( $lang_code ) {
case "nl_NL":
// Set key the same as the value, that's how Magetnto gives it at first
$new_prefixes_values = array( $pre_val[0] => $pre_val[0], $pre_val[1] => $pre_val[1]);
break;
case "de_DE":
$new_prefixes_values = array( $pre_val[2] => $pre_val[2], $pre_val[3] => $pre_val[3]);
break;
default: // other languages
$new_prefixes_values = array( $pre_val[0] => $pre_val[0], $pre_val[1] => $pre_val[1]);
break;
}
return $new_prefixes_values;
}
What I filled in the Prefix Dropdown Options setting: Dhr.;Mevr.;Herr;Frau
It is a bit hard coded but works and allows you to change the values

Magento product tier prices are deleted on product images update

I have created a script to update programmatically my products pictures but my script delete all tier_prices on $product->save();
Here is my bilder update script :
foreach ($productCollection as $product) {
$formatted_sku = $product->getSku();
$config = $product->getMediaConfig();
// JPG files verification
$jpg_file = $images_folder.$formatted_sku.".".$extension[0];
if (file_exists($jpg_file) ) {
$fileurl = $config->getMediaUrl($jpg_file);
$product->addImageToMediaGallery($jpg_file, $visibility, false, false);
$product->save();
}
}
How can I avoid the update of my tier_prices ?
Thanks a lot.
For those of you that are still running into this issue, there is a simple fix to try. It looks like the tierprice data is not read by the default getModel for the product. To fix this, just call the product's getTierPrice method to load it.
$tp=$product->getTierPrice();
You don't have to do anything else, just load it. Then when you save the product, the tiered pricing data is saved.
I run into the same problem. I finally work it out in a very weird way, but it definitely worked. You just need to create a "fake" tierprice:
$tierPrices = array(
'website_id' => 0,
'cust_group' => 2,
'price_qty' => 3,
'price' => 10
);
(note that there are no [ ])
Then add it (it won't add anything actually), but you need to do this:
$product->setTierPrice($tierPrices);
And finally save the product:
$product->save();
It will save your product without deleting your old tier prices. Hope it helps!!
How do you created that $productCollection? Maybe product was not populated with needed data (tier_prices) so save() persist product without that data. Try to add some attributes to select with addAttributeToSelect()
The comments of others on this post helped to lead me to a solution that worked. For me, simply setting the tier price to false is what was needed to prevent it from being overwritten/modified.
$product->setTierPrice(false);
I personally prefer this option over some of the other solutions as it's clean, simple, doesn't set fake values, and it works. To those points, I would have preferred it if the solution presented by GregC would have worked as it's simply loading the tier price, but in my testing that did not work as expected - the tier price was still deleted.
Here would be the modified version of the code from the OP.
foreach ($productCollection as $product) {
$formatted_sku = $product->getSku();
$config = $product->getMediaConfig();
// JPG files verification
$jpg_file = $images_folder.$formatted_sku.".".$extension[0];
if (file_exists($jpg_file) ) {
$fileurl = $config->getMediaUrl($jpg_file);
$product->addImageToMediaGallery($jpg_file, $visibility, false, false);
$product->setTierPrice(false); // set tier price to false to prevent it from being overwritten
$product->save();
}
}
This code was tested and used with Magento EE 1.14.12.0

Resources