Trying to use an action button to call function - Magento 1.9
-
06-10-2020 - |
Pregunta
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>
Solución
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();
Otros consejos
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>