Magento change product name adding to cart - magento

I want to change some values of some products while adding them to the cart in Magento CE 1.7
Im trying with the Observer checkout_cart_add_product_complete for this. Change the price (CustomPrice) works well if I try to change the product name or product images, its not saved.
Is there a way to change this attributes already on adding to cart?
public function checkout_cart_add_product_complete(Varien_Event_Observer $observer) {
[...]
// Set new Price
$lastAddedItem->setOriginalCustomPrice($originalProduct->getPrice());
$lastAddedItem->setCustomPrice($originalProduct->getPrice());
// Set Product-Name
$lastAddedItem->setName($originalProduct->getName());
// Set Product-Images
$lastAddedItem->setImage($originalProduct->getImage());
$lastAddedItem->setSmallImage($originalProduct->getSmallImage());
$lastAddedItem->setThumbnail($originalProduct->getThumbnail());
// Save updated Item and Cart
//$lastAddedItem->save();
Mage::getSingleton('checkout/cart')->save();
// Recalc Totals and save
$quote->setTotalsCollectedFlag(false);
$quote->collectTotals();
$quote->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
}

The function Mage_Sales_Model_Quote_Item::setProduct resets some of the basic information each time a product is saved (updated). Luckily there is an event "sales_quote_item_set_product" you can latch onto.
config.xml
<config>
...
<global>
<events>
<sales_quote_item_set_product>
<observers>
<samples>
<type>singleton</type>
<class>samples/observer</class>
<method>salesQuoteItemSetProduct</method>
</samples>
</observers>
</sales_quote_item_set_product>
</events>
<global>
...
</config>
Observer.php
class Mynamespace_Samples_Model_Observer
{
public function salesQuoteItemSetProduct(Varien_Event_Observer $observer)
{
/* #var $item Mage_Sales_Model_Quote_Item */
$item = $observer->getQuoteItem();
$item->setName('Ians custom product name');
return $this;
}
}

Related

Magento - Change the quote_id after login

I'm adding products to my cart while I'm not logged in. My quote_id is 597.
In the checkout process I'm logging in (in the first step) and I've noticed that my quote has changed.
quote_id 597 has disappeared from database and the new quote_id is now 555.
What's the observer,class and method that are in charge of this quote change, as I have a temporary table in the database where I also need to modify that quote_id?
on checkout login there is an observer that performs Quote Merge,
that is if the customer is having nay quotes in database that will merge with the current one ,
if you do not want to merge the quotes you can set your own observer to remove all previous items,
config file
<global>
<events>
<sales_quote_merge_before>
<observers>
<mymage_clearoldcartproducts_observer>
<type>singleton</type>
<class>MyMage_Clearoldcartproducts_Model_Observer</class>
<method>removeCustomerQuote</method>
</mymage_clearoldcartproducts_observer>
</observers>
</sales_quote_merge_before>
</events>
</global>
Observer.php file
<?php
class MyMage_Clearoldcartproducts_Model_Observer extends Mage_Checkout_Model_Session {
public function removeCustomerQuote() {
$customerQuote = Mage::getModel('sales/quote')
->setStoreId(Mage::app()->getStore()->getId())
->loadByCustomer(Mage::getSingleton('customer/session')->getCustomerId());
if ($customerQuote->getId() && $this->getQuoteId() != $customerQuote->getId()) {
// Removing old cart items of the customer.
foreach ($customerQuote->getAllItems() as $item) {
$item->isDeleted(true);
if ($item->getHasChildren()) {
foreach ($item->getChildren() as $child) {
$child->isDeleted(true);
}
}
}
$customerQuote->collectTotals()->save();
} else {
$this->getQuote()->getBillingAddress();
$this->getQuote()->getShippingAddress();
$this->getQuote()->setCustomer(Mage::getSingleton('customer/session')->getCustomer())
->setTotalsCollectedFlag(false)
->collectTotals()
->save();
}
return $this;
}
}
?>

Magento 1.9 check if customer buys from certain category

I'm trying to check if a new order has products from certain category, and if it does pass some params to a function and execute that function. Ex:
I add 2 products in cart(one is from X category) and checkout, place the order.Now, when i finish buying the products and the order has been placed, I want to execute a function with the order_id and products_id as params.
I have serched the entire google and didn't find anything. Do you guys have any ideea how could this be created?
I am assuming you are aware of event&observer
Therefore, you can use sales_order_place_after event to check, like in the following example:
<sales_order_place_after>
<observers>
<b4u_order_observer>
<type>singleton</type>
<class>Mshop_B4u_Model_Observer</class>
<method>after_order_placed</method>
</b4u_order_observer>
</observers>
</sales_order_place_after>
Need to write the following code in observer( mshop -> b4u -> model -> Observer.php )
public static function after_order_placed($observer) {
$event = $observer->getEvent();
$order = $event->getOrder();
$order_no = (string) $order->getRealOrderId();
$order->loadByIncrementId($order_no);
$items = $order->getAllVisibleItems();
foreach($items as $i):
// done your code here
endforeach;
}

Call to a member function insert() on a non-object Magento

I am Using Observer Event in my custom module's Config.xml
<controller_action_layout_render_before>
<observers>
<Test_Check_Model_Observer>
<class>Test_Check_Helper_Data</class>
<method>checkValidi</method>
</Test_Check_Model_Observer>
</observers> </controller_action_layout_render_before>
Now in Test/Check/Helper/Data checkValidi Method I am inserting a block in Content.
>
class Test_Check_Helper_Data extends Mage_Core_Helper_Abstract {
> public function checkValidi($observer) {
> $layout = Mage::app()->getLayout();
> $content = $layout->getBlock('content');
> $block = 'hello! I am Working';
> $content->insert($block);
> }
But In Frontend When I am Filling Checkout billing and other information it gives me an Error Call to a member function insert() on a non-object in right side bar of Your Checkout Progress ,Please Give me any Solution for that Thanks
If you look at layout of checkout module checkout.xml you can see that some handles do not have content block. Eg, checkout_onepage_progress_billing or checkout_onepage_progress_shipping
So, you will get non-object error with your code. I think you should check $block variable before calling method.
$layout = Mage::app()->getLayout();
$content = $layout->getBlock('content');
if ($content) {
$block = 'hello! I am Working';
$content->insert($block);
}
It is not good idea to call helper from observer.
Observer always call a model file ,it did not class
i have modify and code config.xml code is
<global>
<models>
<testcheck>
<class>Test_Check_Model</class>
</testcheck>
</models>
</global>
<events>
<controller_action_layout_render_before>
<observers>
<test_check_codel_observer>
<type>singleton</type>
<class>testcheck/observer</class>
<method>your_function_name</method>
</test_check_codel_observer>
</observers>
</controller_action_layout_render_before>
</events>
And then Create Observer Observer.php file Under app/code/YourcodePoll/Test/Check/Model/
and below
code in Observer.php
is
<?php class Test_Check_Model_Observer
{
public function your_function_name($observer){
$block = $this->getLayout()
->createBlock('core/text', 'example-block')
->setText('<h1>This is a text block</h1>');
$observer->getEvent()->getLayout()->getBlock('content')->append($block);
//$observer->getEvent()->getLayout()->getUpdate();
}
}

Category attributes on frontend

I have a multiselect attribute in my categories named location
How do I display the selected / saved values on the frontend?
Thanks
you have to add the attribute to the colleciton via event:
"catalog_category_flat_loadnodes_before"
$observer->getSelect()->columns(
array( 'location' )
);
Try this:
$category->getResource()
->getAttribute('location')
->getSource()
->getOptionText($category->getData('location'))
register an ovserver in magento via xml:
<events>
<catalog_category_flat_loadnodes_before>
<observers>
<category_add_attribute>
<type>model</type>
<class>myModule/observer_catalog_category</class>
<method>addMenuAttributes</method>
</category_add_attribute>
</observers>
</catalog_category_flat_loadnodes_before>
</events>
and then in your Class
class MyModule_Namespace_Model_Observer_Catalog_Category
{
public function addMenuAttributes( Varien_Event_Observer $observer )
{
$observer->getSelect()->columns(
array( 'custom_attribute_name' )
);
}
}
add the customAttribute

Adding a custom option using an extension in Magento

I'm creating a custom extension and I would like to add a custom option when a certain item is purchased.
For example, when the product "Name Tag" is purchased, the extension would detect that the specific product has been ordered and assign a custom option of "Year" to it. The user does not see this, but the attribute is added and displayed in the admin when viewing the order.
Are there any specific listeners our there to accomplish this?
EDIT Based on Ben's suggestions in the comments, I'm editing the comment to reflect progress I've made with the answers located here: https://stackoverflow.com/a/9496266/268165
I now have an extension running under Namespace_addYear and the following code running:
Namespace/addYear/etc/config.xml:
<?xml version="1.0" encoding="utf-8"?>
<config>
<modules>
<Namespace_addYear>
<version>1.0.0</version>
</Namespace_addYear>
</modules>
<global>
</global>
<frontend>
<events>
<catalog_product_load_after>
<observers>
<Namespace_addYear>
<type>model</type>
<class>addYear/observer</class>
<method>catalogProductLoadAfter</method>
</Namespace_addYear>
</observers>
</catalog_product_load_after>
</events>
</frontend>
</config>
Namespace/addYear/Model/Observer.php:
class Namespace_addYear_Model_Observer
public function catalogProductLoadAfter(Varien_Event_Observer $observer)
{
// set the additional options on the product
$action = Mage::app()->getFrontController()->getAction();
if ($action->getFullActionName() == 'checkout_cart_add')
{
// assuming you are posting your custom form values in an array called extra_options...
if ($options = $action->getRequest()->getParam('extra_options'))
{
$product = $observer->getProduct();
// add to the additional options array
$additionalOptions = array();
if ($additionalOption = $product->getCustomOption('additional_options'))
{
$additionalOptions = (array) unserialize($additionalOption->getValue());
}
foreach ($options as $key => $value)
{
$additionalOptions[] = array(
'label' => $key,
'value' => $value,
);
}
// add the additional options array with the option code additional_options
$observer->getProduct()
->addCustomOption('additional_options', serialize($additionalOptions));
}
}
}
}
The Observer.php was taken from the answer linked above. So far, I cannot get the store to do anything other than display a 404 page when I remove the tags in config.xml.
Is there anything someone can immediately see that is wrong?

Resources