magento detecting product page programmatically - magento

On an observer file I need to detect if the loaded page corresponds to a product. I've been using a similar method to the one used on the checked answer on this question:
if (!(Mage::registry('current_product'))) return;
But until now we were testing on development. From today we are testing on pre-production servers, with lot of modules and plugins. Until now it worked seamesly but now it does not work, it does not detect when a product page is loaded. I think is related with the url rewrite to show a more "friendly" url's but can not detect how to solve it. I took a look into Alan Storm tutorial In Depth Magento Dispatch but I still can get what is wrong or what do I need to change.
Any idea? Or some other solution on how to detect when a product page is loaded?

You've verified that the observer is being hit? Make sure that for the event you are triggering on has a unique name for that event. For example in the following code the name "catalog_product_set_price" must be unique in regards to "catalog_product_load_after" event. I'm assuming you're getting into the observer, but just in case. This risk becomes possible if you add a bunch of new modules to an instance.
<catalog_product_load_after>
<observers>
<catalog_product_set_price>
<type>singleton</type>
<class>NamespaceModule_Model_Observer</class>
<method>set_price2</method>
</catalog_product_set_price>
</observers>
</catalog_product_load_after>
You could use the following array to check where you are:
$currentPageArray = array(
'request_string'=> Mage::app()->getFrontController()->getRequest()->getRequestString(),
'uri' => Mage::app()->getFrontController()->getRequest()->getRequestUri(),
'route' => Mage::app()->getFrontController()->getRequest()->getRouteName(),
'action' => Mage::app()->getFrontController()->getRequest()->getActionName(),
'controller' => Mage::app()->getFrontController()->getRequest()->getControllerName()
);
it will output something like:
Array
(
[request_string] => /some-category/some-product-page-url.html
[uri] => /catalog/product/view/id/64806/category/17
[route] => catalog
[action] => view
[controller] => product
)
You could try a different observer.
controller_action_layout_render_before_catalog_product_view
controller_action_postdispatch_catalog_product_view

Related

Validate Payment Method in Magento 2

I want to check whether a customer is allowed to use one of the activated payment methods.
For example:
If the customer had enabled "Cash on Delivery", he should see the method but I can't find anything similar in the code.
In Magento 1 there was:
public function isAvailable()
Is there any similar way to do it with Magento 2 ?
There are 2 approaches you could take:
Observer
If you take a look at \Magento\Payment\Model\Method\AbstractMethod (the parent class of \Magento\OfflinePayments\Model\Cashondelivery) in the isAvailable method, you will find that it dispatches the payment_method_is_active event which could be used to performs some additional logic (such as determine if a customer had enabled the specific method or not):
$this->eventManager->dispatch(
'payment_method_is_active',
[
'result' => $checkResult,
'method_instance' => $this,
'quote' => $quote
]
);
Plugin
Since isAvailable is a public method, you could add a plugin to the method. Whether you use before, after, or around depends on your specific needs. However, a good place for learning how to do this is the Magento DevDocs site: http://devdocs.magento.com/guides/v2.0/extension-dev-guide/plugins.html

Magento: Blocks on home page not shown after version upgrade?

I updated my Magento from 1.9.0.1 to 1.9.2.2 recently and 2 blocks are not shown after the update. This is the code in home CMS:
{{block type="catalog/navigation" name="catalog.category" template="catalog/category/cat_list.phtml"}}
{{block type="filterproducts/latest_home_list" template="callthis/filterproducts/list.phtml"}}
Any idea why they are not there after the update? The template files are there so that can not be the reason. I debugged the code and it seems its not going into the template files so I guess thats the reason, but why isnt it going there?
Thanks!
the 1.9.2.2 version includes the SUPEE-6788 security patch, which fixes a vulnerability that allows access to private information. That means that the blocks used in CMS pages and emails must be whitelisted.
By default, only the core/template and catalog/product_new blocks types are allowed. To use other (in your case catalog/navigation and filterproducts/latest_home_list) you have to whitelist them.
To do that you simply need to include these in the permission_block table. You can do that by directly insert them in the db, or by using a setup script, which would look like:
<?php
/** #var $installer Mage_Core_Model_Resource_Setup */
$installer = $this;
$installer->startSetup();
$installer->getConnection()->insertMultiple(
$installer->getTable('admin/permission_variable'),
array(
array('variable_name' => 'xml_path/custom/variable', 'is_allowed' => 1),
)
);
$installer->getConnection()->insertMultiple(
$installer->getTable('admin/permission_block'),
array(
array('block_name' => 'catalog/navigation', 'is_allowed' => 1),
array('block_name' => 'filterproducts/latest_home_list', 'is_allowed' => 1)
)
);
$installer->endSetup();
note that the first part of this setup is to allow custom config variables, you don't need them for your current problem, I just include it for the sake of completeness.
You can find more information about what the patch do in magento's website
This is cause by one of the patch of your update.
Quick fix:
Find the table permission_block in the database
Add rows with catalog/navigation and filterproducts/latest_home_list to allow these blocks to show.
Empty your cache
More documentation abour this update:
http://magento.com/security/patches/supee-6788
http://magento.com/security/patches/supee-6788-technical-details
https://magento.stackexchange.com/questions/87214/how-to-check-which-modules-are-affected-by-security-patch-supee-6788/87262#87262

Catching product attribute update massaction event

I am trying to listen to the "catalog_product_save_before" and "catalog_product_save_after" events which are correctly triggered when I save individual product from admin interface.
But, these events are not triggered when I use the "update attributes" action for multiple product selections, from the product list grid. Is there any event which is triggered whenever a product attribute is updated ?? I know I need to write different observer function for csv upload but I need the event for grid action "Update attributes"..
Thanks in advance.
Cheers..
Got this to work out!!
I used this event : catalog_product_attribute_update_before
This give an array with changed attributes and product ids :
[attributes_data] => Array
(
[price_markup] => 10
)
[product_ids] => Array
(
[0] => 6
)
[store_id] => 0
Hope this helps ..
Cheers!!!
this might be a vague answer, I am not sure what you try to do with the attribute but maybe have a look into the Mage_Indexer module. Its bit more complex but it listens to all events or at least Magento takes care of notifying it if something is outdated or has changed.
And also if something goes funky with your data, rerunning your indexer should clean up all the data in an ideal world.

Magento Custom Report remove column

How do i remove the Period column from my custom report?
i tried with
unset($this->_columns['period']);
but its not working.
I need to start my columns like order number, order date,subtotal etc etc.
This is loaded by a custom grid of Backend of Magento, you need identify this. To find the custom block you can active backend hints and see what grid are loaded in this section.
You can active backend hints with the next Sql:
UPDATE core_config_data SET scope_id = 0 WHERE path like 'dev/debug/template_hints%';
Execute always in development enviroment. Never in production site
You can see how to active backend hints in this link, http://www.damianculotta.com.ar/2009/07/11/mostrar-phtmls-y-bloques-usados-en-el-skin-de-backend-de-magento/ sorry, this is in spanish, I don´t find this information in english :(
With this you also to know the type of block load in this grid, this is the important date. Next you need create a little module that rewrite this block.
Whit this method you can see that the next dates
Template
adminhtml\default\default\template\widget/grid.phtml
Block
Mage_Adminhtml_Block_Report_Sales_Sales_Grid
in the block you can see the next columm in the construct
$this->addColumn('period', array(
'header' => Mage::helper('sales')->__('Period'),
'index' => 'period',
'width' => 100,
'sortable' => false,
'period_type' => $this->getPeriodType(),
'renderer' => 'adminhtml/report_sales_grid_column_renderer_date',
'totals_label' => Mage::helper('sales')->__('Total'),
'html_decorators' => array('nobr'),
));
You need remove this columm in you custom block, always in separate extension.
If you don´t know how you can rewrite a block, only need said me :P
Hope help you

Anyone know how to access the magento gift card sku in transactional email template?

I had a custom utility task that allowed users to choose an image to accompany the gift card notice that is sent to the recipient when a gift card is purchased at the magento run shop. In the template, there is an assortment of variables available so you can customize the email that is sent. However, in order to add the correct image, I need to have access to the gift card sku number since my method for handling this was to simply create many gift cards and apply separate images for each one, then use javascript to swap the sku numbers when the user clicks the images. Simple enough.
In the app/code/core/Enterprise/GiftCard/Model/Observer.php file, the variables are set:
$templateData = array(
'name' => $item->getProductOptionByCode('giftcard_recipient_name'),
'email' => $item->getProductOptionByCode('giftcard_recipient_email'),
'sender_name_with_email' => $sender,
'sender_name' => $senderName,
'gift_message' => $item->getProductOptionByCode('giftcard_message'),
'giftcards' => $codeList->toHtml(),
'balance' => $balance,
'is_multiple_codes' => 1 < $goodCodes,
'store' => $order->getStore(),
'store_name' => $order->getStore()->getName(), // #deprecated after 1.4.0.0-beta1
'is_redeemable' => $isRedeemable,
);
So I could add the product sku to this array and it would be available in the template system. Problem is, I don't know where to trace this back to even know how to get the Sku. I assume it is somehow available in the $item var, but magento has no documentation on this particular case and I can't guess since there is no method for testing. The email script is activated when the card is purchased so I can't just click a "Send test" button to see what comes out in the email that is sent to the user. The preview button dismisses variables.
Anyone happen to know about this? Possibly something like $item->getSku()
Any input helps.
Thanks
That snippet is from the method Enterprise_GiftCard_Model_Observer::generateGiftCardAccounts() which is registered as a handler for sales_order_save_after event. That event is passed the same 'order' object as the "new order placed" emails have. You can experiment by altering that email template and triggering a new email by resending from the order page in admin.
$item->getSku() is almost certainly right.

Resources