Prevent Observer to be called twice - magento

I have an observer that fires on catalog_controller_product_view event. It works fine, but when I add product to cart Magento produces another event and then redirects back to product view and here my observer is called once again.
I want to prevent this behavior. I tried to log whole $observer Object on first product view and after redirect from the cart, but they are absolutely the same.
Any idea, how to prevent Observer to be called twice on this event?
P.S. I'm using Magento 1.9.2

I'm not sure how good my workaround is, but it works.
$coreSession = Mage::getSingleton('core/session');
if ($event_name == 'catalog_controller_product_view' && $coreSession->getData("test") == 1){
$coreSession->setData("test", 0);
return;
}
// view
if ($event_name == 'catalog_controller_product_view'){
$coreSession->setData("test", 0);
// Do stuff
}
// add_to_cart
elseif ($event_name == 'checkout_cart_add_product_complete'){
$coreSession->setData("test", 1);
// Do stuff
}

Related

Xamarin, how can I gain a completed state from a command?

I have a ListView and am using the SelectedItem to call a command and show a modal view.
However, I have an issue where the user can tap multiple times on the listview row and multiple modal views are shown before the view has loaded. Granted this only happens on slower devices.
This is is caused because the command doesn't have any call back.
I wouldn't normally paste code here, but in this case I thought it was more descriptive to provide a screen shot.
I've looked into the AsyncCommands but these seem to be used more to handle errors.
I'm currently thinking about a subscribe approach which is triggered when the modal is exited, however I think there must be another way I haven't thought of.
Can you try using a Boolean as IsSelected & make IsSelected true when user clicks an item from list & change your condition in setter as below. When your operations are done reset the flag.
This was, there wont not be any duplication of modals. This is what I understood from your question, if there's something else, please let me know.
if( _locationAssetSelected || !IsSelected )
{
IsSelected = true;
_locationAssetSelected = value;
..... //your code
_locationAssetSelected = null;
IsSelected = false;
}
You could move the logic to the command to make sure it doesn't execute multiple times. This is a snippet on the sample/template Forms app which uses this method as a command. Lock seems unnecessary.
async void OnItemSelected(Item item)
{
//lock (selectLock)
//{
if (item == null || selectionOn)
return;
selectionOn = true;
//}
System.Diagnostics.Debug.WriteLine($"{item.Text} selected");
// This will push the ItemDetailPage onto the navigation stack
await Shell.Current.GoToAsync($"{nameof(ItemDetailPage)}?{nameof(ItemDetailViewModel.ItemId)}={item.Id}");
selectionOn = false;
}

Hide text from one magento store

I have two stores created, retail and wholesale. I created a static block visible in both the stores. Without logging in, i want to hide a line when somebody opens the wholesale page using css display:none.
I need to a php code that would detect different stores so i can add css class. I've seen examples for logged in users but not for stores.
Example code that i'm looking for:
<?php
if (store = wholesale) then
else if…
end if
?>
Anyone?
use this
$storeCode = Mage::app()->getStore()->getCode();
if($storeCode == 'default')
{
/* your code here */
};
You can apply store condition as per below :
if (Mage::app()->getStore()->getId() == YOUR STORE VIEW ID HERE)
{
//apply your custom code here
}
or
if (Mage::app()->getStore()->getCode() == YOUR STORE VIEW CODE HERE)
{
//apply your custom code here
}
Apply your store id or code and put them in condition accordingly.

Wicket Panel not refreshed when swapped in via an AJAX request

I have a Panel that I initially replace with another panel (a spinning circle saying "loading") and after a while in an AJAX event replace back. So what I do is:
#Override
protected void onConfigure() {
if (!content.isLoaded()) {
tmp = new LoadingCircle(getId(), "Loading...");
this.replaceWith(tmp);
}
}
public void onLoaded(AjaxRequestTarget target) {
if (tmp != null) {
tmp.replaceWith(this);
tmp = null;
}
target.add(this);
}
This works, so the panel is shown back in the page. However, this panel has some subpanels with some labels and they are not refreshed. I see the updated labels when I reload the page but not immediately after showing the whole panel with AJAX.
I notice that the onConfigure and onBeforeRender methods are called only once - when the panel is created but not again when it is actually shown for the first time using AJAX. The same for the subpanels, so that would explain why they are not refreshed. But the question is - why is the panel not refreshed (the updated model values are not used) when it is added to the AJAX request target?
EDIT: I think this may be relevant: https://issues.apache.org/jira/browse/WICKET-5107. Still, I wonder what I should change in my code to make it work?
IMO it looks like you should use an AjaxCallDecorator instead to do what you are trying to do with the spinner.

Primefaces commandButton and ajax

I use a tabView component with many tabs. In many of them, I have form which are submitted by primefaces commandButton component.
By default, PF commandButton using ajax mode but when I submit my form, my page seems to be fully loaded and my tabView component lost its index view (index 0 is rendered).
Is that normal behaviour please ?
I though that I would stay in the same index because it's ajax...
Looks like there is some naming container (p:tabView maybe) that you better assign an id to it , so instead of getting prefix like j_idt16 (which could vary from time to time) you will get myTab0 , myTab1 etc prefix...
for example <p:tabView id="myTab"
Another thing you could do to be on the safe side is checking if the element exists before trying to select it with jquery and access its value, like this
if($('#j_idt16\\:register_location_choice_2_input').length > 0){
//some code here
}
Ok, my problem is the JS validateRegisterForm function. When I remove it, it works but I need it...
I use it to check if validation form can be launched.
function validateRegisterForm(){
if($('#j_idt16\\:register_location_choice_2_input').attr('checked')){
if($('#j_idt16\\:register_galaxies_input').val() == 0){
var galaxie = MUST_CHOOSE_GALAXY;
alert(galaxie.charAt(0).toUpperCase() + galaxie.slice(1));
return false;
}
if($('#j_idt16\\:register_solar_systems_input').val() == 0){
var ss = MUST_CHOOSE_SOLAR_SYSTEM;
alert(ss.charAt(0).toUpperCase() + ss.slice(1));
return false;
}
if($('#j_idt16\\:register_positions_input').val() == 0){
var position = MUST_CHOOSE_POSITION;
alert(position.charAt(0).toUpperCase() + position.slice(1));
return false;
}
}
return true;
}
So how can I check fields values before sending and allowing or not validation form with ajax please ?
EDIT :
Ok, I solved my problem by launching validation inside my JS function with button type passed to button not submit and using remoteCommand component :
My JS function :
function validateRegisterForm(){
if(...)
validateForm();
}
And my remoteCommand :
<p:remoteCommand name="validateForm" actionListener="#{login.registerAccount()}"/>

Magento event name to observe

I need to add some code to Magento (1.6.2) to be executed when an orders status becomes Complete.
In our system, this happens when the order is "Shipped" - i.e. the "Ship" button is clicked, and the shipping info is saved.
I have hunted (obviously in the wrong places) to try and find what that event would be called, so that I can add an observer to watch for it firing, and then run my code.
Can anyone tell me what the name of this event would be (if it exists as an observable event) please?
Cheers!
I too find event hunting to be a bit of a dark art. In this case I would try sales_order_save_before and then check in a handler like this:
function onSalesOrderSaveBefore(Varien_Event_Observer $observer)
{
$order = $observer->getOrder();
if (($order->getData('status') == 'complete')
&& ($order->getOrigData('status') != 'complete')) {
// then order has just been completed
}
}
One possible solution is to create a custom module that override this controller
/app/code/core/Mage/Adminhtml/controllers/Sales/Order/ShipmentController.php
Then add your custom code or create you own custom event in public function saveAction()

Resources