0

I'm trying to add an action button in M1 dashboard.

I want the button to call the function I've created, and display a warning message.

The first snippet is what I started with and I'm struggling to get it working (second snippet).

I started off with this example:

<strong class="action_button">
<a class="icon_list preview_grid" title="<?php echo $this->__('Delete') ?>" onclick="return confirm('<?php echo $this->__('Are you sure want to delete').'?'; ?>')" 
href="<?php echo Mage::getBaseUrl() . "booking/booking/delete/"; ?>id/<?php echo $_product->getId(); ?>"  ><?php echo $this->__('Delete') ?></a>
/strong>

and then modified a bit to get the below, but can't get it working.

perhaps someone can help. Many thanks

<strong class="action_button">
<a class="icon_list calender" target="_blank" title="Copy"  onclick="return confirm('<?php echo $this->__('Are you sure want to copy').'?'; ?>')" 
href=$this->__<?php echo Mage::helper(‘function’)->Duplicate_frontend(); ?> ><?php echo $this->__('Copy') ?></a>
</strong>

Here is my function code:

Duplicate_frontend.xml:

<?xml version=“1.0”?>
<config>
<modules>
<Duplicate_frontend_Function>
<active>true</active>
<codePool>local</codePool>
</Duplicate_frontend_Function>
</modules>
</config>

config.xml:

<?xml version=“1.0”?>
<config>
<modules>
<Duplicate_frontend_Function>
<version>1.0.0</version>
</Duplicate_frontend_Function>
</modules>
<global>
<helpers>
<function>
<class>Duplicate_Function_Helper</class>
</function>
</helpers>
</global>
</config>

Data.php:

class Duplicate_Function_Helper_Data extends Mage_Core_Helper_Abstract
public function Duplicate_frontend() //change the function name
{
$productId = $observer->getEvent()->getProduct()->getId();
$productObject = Mage::getModel('catalog/product');
$_product = $productObject->load($productId);
$newProduct = $_product->duplicate();
//new product status is disabled - to view in the frontend you will need to set the status as enabled
$newProduct->setStatus(1);
$newProduct->setName('Duplicate-' . $_product->getName());
$newProduct->setSku('value-' . $productId);
$newProduct->setWebsiteIds($_product->getWebsiteIds());
//set the product stock and qty to display product in the frontend
//while creating duplicate product, it will update the new product to have qty 0 and out of stock
$stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($newProduct->getId());
if ($stockItem->getId() > 0 && $stockItem->getManageStock())
{
$qty = 100;
$stockItem->setQty($qty);
$stockItem->setIsInStock((int)($qty > 0));
$stockItem->save();
}
$newProduct->getResource()->save($newProduct);
//automate reindexing - to display the product in the frontend
$indexers = Mage::getSingleton('index/indexer')->getProcessesCollection();
foreach ($indexers as $indexer)
{
$indexer->reindexEverything();
}
}

Revised action button code. The function is performed upon page load (not good), Need to action the function upon click/confirm. How should I change this code?

<strong class="action_button">
<a class="icon_list calender" target="_blank" title="Copy"  onClick="return confirm('<?php echo $this->__('Are you sure want to copy').'?'; ?>')" href="<?php echo Mage::helper('DuplicateModule')->duplicatefunction(); ?>" ><?php echo $this->__('Copy') ?></a>
</strong>
  • I think I got the issue , can you provide me your full directory path config.xml – Murtuza Zabuawala Feb 6 '17 at 5:11
  • Which config.xml would you like? my app/etc/config.xml, or my theme config.xml, or different? Just want to be sure I provide the correct file – Dan Feb 6 '17 at 10:18
  • your module confix.xml path – Murtuza Zabuawala Feb 6 '17 at 10:25
  • Ok, I think this is what you are asking for: pastebin.com/HhjLvwde I don't have a custom module exclusively for the function, just want to utilize the existing theme module. Would I need to use a custom module? Thanks – Dan Feb 6 '17 at 10:51
  • Hi Murtuza, I think I misunderstood what you were requesting. The path of my module.xml is: ~/magento/app/code/local/Duplicate/Function/etc/config.xml I hope that is the info you needed. Let me know otherwise – Dan Feb 7 '17 at 15:54
1

Change your second code snippet from

<strong class="action_button"> <a class="icon_list calender" target="_blank" title="Copy" onclick="return confirm('<?php echo $this->__('Are you sure want to copy').'?'; ?>')" href=$this->__<?php echo Mage::helper(‘function’)->Duplicate_frontend(); ?> ><?php echo $this->__('Copy') ?></a> </strong>

To

<strong class="action_button"> <a class="icon_list calender" target="_blank" title="Copy" onclick="return confirm('<?php echo $this->__('Are you sure want to copy').'?'; ?>')" href="<?php echo Mage::helper(‘function’)->Duplicate_frontend(); ?>" ><?php echo $this->__('Copy') ?></a> </strong>

There is a typo mistake in your href

EDIT

First, update your file name from Duplicate_frontend.xml to Duplicate_Function.xml and in that file update module name as Duplicate_Function
Same way in your config.xml update Duplicate_frontend_Function to Duplicate_Function
The thing you are doing wrong here is you are using your helper function directly in your href which is wrong, Create a controller action and put controller action's url to your href data just like the example you have put from core Magento
In that controller action, call your helper function and your issue will get resolved.

<strong class="action_button"> <a class="icon_list calender" target="_blank" title="Copy" onclick="return confirm('<?php echo $this->__('Are you sure want to copy').'?'; ?>')" href="<?php echo YOUR_CONTROLLER_ACTION_PATH; ?>" ><?php echo $this->__('Copy') ?></a> </strong>

In your Controller file, use below code

Mage::helper('function')->Duplicate_frontend();
|improve this answer|||||
  • Thanks Jaimin, The function is called successfully, But, it is actioned upon page load, and not upon the click/confirm. Can you tell me what I'm doing wrong? code I am using is added above ^^ – Dan Feb 13 '17 at 8:21
  • The function is being called on page load? There is no code in your question which works on page load, there is only a button and you have used its click event to run function. Are you sure this is the only code you have? – Jaimin Sutariya Feb 13 '17 at 8:40
  • Hi Jaimin, I can't find what is wrong. Yes the page is loading the function without waiting for the confirm. In Chrome dev console, its shows the href a blank: eg. <a class="icon_list calender" target="_blank" title="Copy" onclick="return confirm('Are you sure you want to copy?')" href="" >Copy</a> I guess the echo is loading the function, regardless of whether the button is clicked to confirm or not. Is there another way to achieve this? – Dan Feb 15 '17 at 20:48
  • Please check my edit to the answer – Jaimin Sutariya Feb 16 '17 at 2:58
  • Thanks Jaimin, I got it working! ended up moving my code into the contoller (no longer need the helper), and modified the href to add the product id variable. <a class="icon_list calender" target="_self" title="Copy" onclick="return confirm('<?php echo $this->__('Are you sure you want to copy').'?'; ?>')" href="<?php echo Mage::getBaseUrl() . "duplicatelisting/duplicate/copy"; ?>/id/<?php echo $_product->getId(); ?>" ><?php echo $this->__('Copy') ?></a> – Dan Feb 19 '17 at 18:58
1

there were many things that is wrong in your code ,First I notice that your module file name is Duplicate_frontend.xml

instead of Duplicate_Function.xml

and code for that file would be.

<?xml version=“1.0”?>
<config>
<modules>
<Duplicate_Function>
<active>true</active>
<codePool>local</codePool>
</Duplicate_Function>
</modules>
</config>

and in your config.xml code would be

<?xml version=“1.0”?>
<config>
<modules>
<Duplicate_Function>
<version>1.0.0</version>
</Duplicate_Function>
</modules>
<global>
<helpers>
<function>
<class>Duplicate_Function_Helper</class>
</function>
</helpers>
</global>
</config>
|improve this answer|||||
  • Awesome, thanks Murtuza, this was a big help. Got the function structure working, I now just have a problem with the when the function is loaded. – Dan Feb 13 '17 at 8:26
  • @Dan I am busy right now let you know full detail answer at tonight – Murtuza Zabuawala Feb 13 '17 at 8:28
  • Thanks for your help Murtuza, finally got it working. I think the answer from Jaimin helped with finally getting it to do exactly what I needed. – Dan Feb 19 '17 at 19:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.