I' am using Joomla 3, I have module called "Who we Are" and it's being displayed on position "top_row2". I' am trying to get this modules ID and modules Name.
After Searching I found few solutions which doesn't seem to work for me.
Solution 1
jimport( 'joomla.application.module.helper' );
$module = JModuleHelper::getModule('Who we Are');
echo $module->id;
Solution 2
jimport( 'joomla.application.module.helper' );
$module = JModuleHelper::getModules('Who we Are');
echo $module->id;
//Note the "s" in getModules
Solution 3
global $module;
$module->id;
$module->title;
I' am using this solutions on the override PHP files of this Module.
Location:: templates\corporate_response\html\mod_mymodule_item.php
You can define your own unique Class under "Module Class Suffix" and in your module override item page do a conditional to check which module is being rendered into the page
This might help explain calling modules in overrides.
To call a Joomla! module with the title in a template override:
jimport('joomla.application.module.helper');
$modules = JModuleHelper::getModules('position');
foreach($modules as $module) {
echo '<h3>' . $module->title . '</h3>';
echo JModuleHelper::renderModule($module);
}
This will call the module id.
echo $module->id;
This will call the module title in H3, adjust to what you need.
echo '<h3>' . $module->title . '</h3>';
This part will render the module.
echo JModuleHelper::renderModule($module);
You can get all modules by position and then select by title like this:
$module = false;
if ($modules = JModuleHelper::getModules('top_row2')) {
foreach ($modules as $module2) {
if ($module2->title == 'Who we Are') {
$module = $module2;
break;
}
}
}
if ($module) {
printf(
'Found module with name "%s" and id "%d"',
$module->name,
$module->id
);
} else {
echo "Found no matching module.";
}
If someone ever has the same question:
Within the modules code you can simply use the variable $module which contains all information about this specific module like id, title, position, ...
Related
I'm trying to write a magento modulo for importing products from a csv file. I would like to use Magmi to achieve the import. I tried the following code but it doesn't work.
namespace My\Module\Controller\Adminhtml\Home;
require_once(dirname(__FILE__) . "/../../../../../../../lib/magmi/inc/magmi_defs.php");
require_once(dirname(__FILE__) . "/../../../../../../../lib/magmi/integration/inc/magmi_datapump.php");
class Import extends \Magento\Backend\App\Action{
public function execute()
{
// getting data from a casv file
for ($items as $item){
$dp = Magmi_DataPumpFactory::getDataPumpInstance("productimport");
$dp->beginImportSession("default", "xcreate");
$item = "product field array";
$run = $dp->ingest($item);
$dp->endImportSession();
}
}
}
I receive errors on the Magmi classes "class not found". I tried also different code but the only way a think it can works is using class file named, e.g. Datapump.php with e defined class named Datapump. But I cannot rewrite all files of magmi to make it works, so maybe I'm doing something wrong.
Though not an ideal solution. But you can try this.
1) Make below changes in file magmi/integration/inc/magmi_datapump.php
Change the class name from 'Magmi_DataPumpFactory' to 'Magmi_DataPump'
This is needed because Magento 2 interprets Factory keyword in a different way.
2) Now create an autoloader file
app/code/Vendor/Module/magmi-autoloader.php
<?php
$mapping = array(
'Magmi_Defs' => BP . '/magmi/inc/magmi_defs.php"',
'Magmi_DataPump' => BP . '/magmi/integration/inc/magmi_datapump.php'
);
spl_autoload_register(function ($class) use ($mapping) {
if (isset($mapping[$class])) {
require $mapping[$class];
}
}, true);
3) Now change relative paths in below file to absolute path. Since while running magmi classes through Magento, it will be not able to read paths relative to magmi directory.
magmi/inc/magmi_defs.php
<?php
// change below define variables to their absolute paths according to your project's root directory
define("MAGMI_BASEDIR", dirname(dirname(__FILE__)));
define("MAGMI_INCDIR", MAGMI_BASEDIR . '/inc');
define("MAGMI_INTEGRATION_INCDIR", MAGMI_BASEDIR . '/integration/inc');
define("MAGMI_PLUGIN_DIR", MAGMI_BASEDIR.'/plugins');
define("MAGMI_ENGINE_DIR", MAGMI_BASEDIR . '/engines');
.
.
//other code
.
.
4) Now change your controller file as below
<?php
namespace Vendor\Module\Controller\Adminhtml\Home;
require BP.'/app/code/Vendor/Module/magmi-autoloader.php';
class Import extends \Magento\Backend\App\Action
{
public function execute()
{
// getting data from a csv file
$dp = \Magmi_DataPump::getDataPumpInstance("productimport");
// move this outside the loop to prevent mysql max_connection error
$dp->beginImportSession("default", "xcreate");
for ($items as $item){
$item = "product field array";
$run = $dp->ingest($item);
}
$dp->endImportSession();
}
}
I want to show a level2 categories only in product details page in virtuemart,but there is no specific product details page
You will need a component like Metamod and use a recipe like:
$vm = JomGenius("virtuemart"); // need this at the start of every rule
if ($vm->check( "pagetype = productdetails" ) ) { return 99; // module id }
Without Metamod you could use the following:
if (JRequest::getVar('view')=='productdetails') {
jimport('joomla.application.module.helper');
$module = &JModuleHelper::getModule('mod_custom','Your module name');
echo JModuleHelper::renderModule($module);
}
Good Luck!
I need to use the greeting as "Hello customer_firstname" in Invoice emails.
In the Invoice email template file invoice_new.html file, the following line is written, however it is showing the full name of the customer.
Hello, {{htmlescape var=$order.getCustomerFirstname()}
Try
{{var order.getCustomerFirstname()}}
<h4>Ficou com dúvidas? {{var order.getCustomerFirstname()}}</h4>
I found a solution where I modified the class Mage_Sales_Model_Order a little and added a new Method called "getCustomerOnlyFirstName" as shown below ::
The class "Mage_Sales_Model_Order" can be found in app\code\core\Mage\Sales\Model\Order.php path..
public function getCustomerOnlyFirstName()
{
$name = trim($this->getCustomerName());
$pos = strpos($name," ");
if($pos !== false) /// FirstName can be extracted
{
$name = trim(substr( $name, 0, $pos ));
}
return $name;
}
And in Email templates ( invoice_new.html and invoice_new_guest.html ), I had to write the following lines to get the things done ...
Hello, {{htmlescape var=$order.getCustomerOnlyFirstName()}}
It worked perfectly.
Try
<p class="greeting">{{trans "%name," name=$order.getBillingAddress().getFirstname()}}</p>
When I am trying to set the custom message for an error under from_validation library in CodeIgniter using the following statement..
$this->form_validation->set_message("is_unique", "%s already registered. Try a different %s");
Instead of getting Username already registered. Try a different Username I'm getting Username already registered. Try a different users.username
While the documentation says If you include %s in your error string, it will be replaced with the "human" name you used for your field when you set your rules.
Please get me out of the trap.
Looking at your problem it seems that the language file you are using does not have an index user.username. Define it in the user language file and see what happens.
Here is the Reference to language
Codeigniter Language Class
And
Codeigntier Language Helper
I faced same problem , while i am searching i found this answer which mentioned the line in Codeigniter that builds the error message (located in file system/libraries/form_validation.php) near line 673:
// Build the error message
$message = sprintf($line, $this->_translate_fieldname($row['label']), $param);
I edited it to :
// Build the error message
if($rule == 'is_unique') { //my code here
$vars_num = substr_count($line, '%s'); //my code here
for($i = 0; $i < $vars_num; $i++){ //my code here
$params[] = $this->_translate_fieldname($row['label']); //my code here
} //my code here
$message = vsprintf($line, $params); //my code here
} else { //my code here
$message = sprintf($line, $this->_translate_fieldname($row['label']), $param);
}
and it works fine for me .
i know it is not best practice to edit system libraries but maybe it will help you.
Try this:
$this->form_validation->set_message("is_unique", "{field} already registered. Try a different {field}");
You can set Custom Message with Message library of codigniter
For Example
in Model Coding Like this
if(duplicate_user_name ==true){
$this->messages->add('$user already registered. Try a different $user ', 'error');
}
and Print this message with following View
$messages = $this->messages->get();
if (is_array($messages)):
foreach ($messages as $type => $msgs):
if (count($msgs > 0)):
foreach ($msgs as $message):
echo ('<div class="' . $type .'">' . $message . '</div>');
endforeach;
endif;
endforeach;
endif;
So I have been working on a Magento module that hides the price when the user is not logged in. I got it working thanks to #AlanStorm but I just want to make sure it I'm going for the best approach.
What I did is setting a different template for the *catalog_product_price_template* block and from there I did all the logic
<?php $_message = Mage::getStoreConfig('catalog/pricehideconfig/title');
$_enabled = Mage::getStoreConfig('catalog/pricehideconfig/active');
$_current_template = Mage::getBaseDir('design')
. '/frontend/'
. Mage::getSingleton('core/design_package')->getPackageName() . '/'
. Mage::getSingleton('core/design_package')->getTheme('frontend') .'/'
. 'template/catalog/product/price.phtml';
$_default_template = Mage::getBaseDir('design') . '/frontend/base/default/template/catalog/product/price.phtml';
?>
<p>
<?php if ( $_enabled && !($this->helper('customer')->isLoggedIn()) ) { ?>
<?php echo $_message; ?>
<?php } else {
if (file_exists($_current_template)){
include $_current_template;
} else{
include $_default_template;
}
} ?>
</p>
However, two parts seems really unnatural
Calling the 'original' or default template code for the price doesnt feel right, does Magento provides any function to do this, call the default template within a template, while checking if the template exists in the current package and then revert back to the default if none?
I think the template should be used for presentation only, so the variables assignment should be moved to a block instead, but i can't really do that since I'm just setting the template and not extending the *Mage_Catalog_Block_Product_Price_Template*
I don't really understand the code above !!
if you want to hide price from non-logged in customers the easiest and best way i have used is :
The Module will be Only one Block and the config.xml
Extend - Rewrite class Mage_Catalog_Block_Product_Price
class Namespame_ModuleName_Block_Product_Price extends Mage_Catalog_Block_Product_Price
{
// and Override _toHtml Function to be
protected function _toHtml()
{
if(!$this->helper('customer')->isLoggedIn()){
$this->getProduct()->setCanShowPrice(false);
}
if (!$this->getProduct() || $this->getProduct()->getCanShowPrice() === false) {
return '';
}
return parent::_toHtml();
}
}
This works perfectly without adding more codes to view/template/layout !
If you still want to set Template you can do it also as :
class Namespame_ModuleName_Block_Product_Price extends Mage_Catalog_Block_Product_Price
{
// and Override _toHtml Function to be
protected function _toHtml()
{
if(!$this->helper('customer')->isLoggedIn()){
$this->setTemplate('mymodule/price_template.phtml');
}
if (!$this->getProduct() || $this->getProduct()->getCanShowPrice() === false) {
return '';
}
return parent::_toHtml();
}
}
Thanks