Re-declaring a function in a child theme - php

P.S. Hi Admin, I tried looking for the solution but could not see the same issue anywhere!
Hi all,
I have a child theme. I have a function which is wrapped in if !function_exists as below. The file in is includes folder. The function is in wp-content/themes/themename/includes/alias-function.php
if (!function_exists('jem_render_buy_fee ')) {
function jem_render_buy_fee() {
$fee=(ea_get_option('order_commission_buyer'))?ea_get_option('order_commission_buyer'):'';
if($fee){
?>
<div class="jem-commission-fee">
<span><?php _e($fee.'% commission fee included', 'themes'); ?></span>
</div>
<?php
}
}
}
Because it is in includes folders but wrapped in function_exists, so I declare the function in functions.php as below. This file is in wp-content/themes/themename-child/functions.php
function jem_render_buy_fee() {
$fee=(ea_get_option('order_commission_buyer'))?ea_get_option('order_commission_buyer'):'';
if($fee){
?>
<div class="jem-commission-fee">
<span><?php _e($fee.'% GST inclusive', 'themes'); ?></span>
</div>
<?php
}
}
I am getting the error:
Your PHP code changes were rolled back due to an error on line 1873 of file wp-content/themes/themename/includes/alias-function.php. Please fix and try saving again.
Cannot redeclare jem_render_buy_fee() (previously declared in wp-content/themes/themename-child/functions.php:215)
Why am I getting this? The error is wrapped in function_exist.
I can see its loading the function.php, but then it should ignore the existing one in the file.
Thank you in advance.

In your check for function existence, there's a space behind the function name.
!function_exists('jem_render_buy_fee ')
Without this space it should work fine if the order af calling code is correct.
First Call:
function jem_render_buy_fee() {
$fee=(ea_get_option('order_commission_buyer'))?ea_get_option('order_commission_buyer'):'';
if($fee){
?>
<div class="jem-commission-fee">
<span><?php _e($fee.'% GST inclusive', 'themes'); ?></span>
</div>
<?php
}
}
Then call:
<?php
if (!function_exists('jem_render_buy_fee')) { //Space removed from 'jem_render_buy_fee '
function jem_render_buy_fee() {
$fee=(ea_get_option('order_commission_buyer'))?ea_get_option('order_commission_buyer'):'';
if ($fee): ?>
<div class="jem-commission-fee">
<span><?php _e($fee.'% commission fee included', 'themes'); ?></span>
</div>
<?php endif;
}
}

Related

Yii: from a view, how do I found out where a variable came from?

In a Yii project passed to me, there's a function that creates (or shows?) a textbox when the button/link Comment is clicked. From there, the user can create Comments, which will be displayed in a row.
I'm trying to see if I can create an edit comment function, so I thought I could go about this by copying the Comment function - it'll show a textbox, and the user can input the new text in there. And instead of adding a new comment, it will edit the existing one.
But I hit a snag, as apparently the view.php makes use of a variable that I couldn't for the life of me figure out how to use in _comments.php - the file responsible for displaying the individual comments, afaik.
Here's the code for view.php:
</script>
<?php
$this->breadcrumbs=array('Forums'=>array('index'),$model->title,);
?>
<?php
if(Yii::app()->user->hasFlash('message')):
echo '<script>alert("'.Yii::app()->user->getFlash('message').'");</script>';
endif;
?>
<?php $starter = Persons::model()->findByAttributes(array('party_id'=>$model->party_id));?>
<div id="forum_main_box">
<div class="comment-icon-textbox">
<?php echo CHtml::ajaxLink('Comment',array('forum/callcommentform'),
array(
'update' => '#render_div'.$model->id,
'data'=>array('id'=>$model->id),
)); ?>
</div>
<?php endif; ?>
<div id="forum_comment_headerbox">
</div>
<div>
<?php
$this->widget('zii.widgets.CListView',
array(
'dataProvider'=>$dataProvider,
'itemView'=>'_comments',
'summaryText'=>'',
));
?>
<div id="render_div<?=$model->id?>" class="comment-form">
</div>
</div>
</div>
Of that code, below is the Comment link I spoke of:
<?php echo CHtml::ajaxLink('Comment',array('forum/callcommentform'),
array(
'update' => '#render_div'.$model->id,
'data'=>array('id'=>$model->id),
)); ?>
<?php } ?>
This block displays the list of comments, and what (I assume to be) the space where the textbox will pop up when the above Comment is clicked:
<?php
$this->widget('zii.widgets.CListView',
array(
'dataProvider'=>$dataProvider,
'itemView'=>'_comments',
'summaryText'=>'',
));
?>
<div id="render_div<?=$model->id?>" class="comment-form">
</div>
Notice that both makes use of $model. It first appeared in the code as $model->title.
And here's a shortened version of the _comments.php, which is used for the comment rows and the comment box.
<?php $comment = $data; ?>
<div class="other-member-comment-box">
<?php $person=Persons::model()->findByAttributes(array('party_id'=>$comment->party_id)); ?>
<?php
$country=Lookup_codes::model()->findByAttributes(array('id'=>$person->country));
$location = empty($country) ? '' : ' - '.$country->name;
// $model->title;
?>
<?php if (Yii::app()->user->id == $person->party_id || Yii::app()->partyroles->isAdmin()) {
?>
<p class="admin-commands">
<?php echo CHtml::link(CHtml::encode('Edit'),array('forum/editcomment','reply'=>$data->id,'topic'=>$data->content_id)); ?>
<?php echo CHtml::ajaxLink('EditTestComment',array('forum/callcommentform'),array('update' => '#render_div'.$model->id,'data'=>array('id'=>$model->content_id),)); ?>
<?php echo CHtml::link(CHtml::encode('Delete'),array('forum/delete','reply'=>$data->id,'topic'=>$data->content_id),array('confirm'=>'Are you sure you want to delete this item?')); ?>
<div id="render_div<?=$model->id?>" class="comment-form">
</div>
</p>
<?php } ?>
</div>
Under <p class="admin-commands">, there's a EditTestComment link which is a straight up copy of the Comment code from the view.php. This doesn't work, of course, because of this:
2016/04/07 10:24:03 [error] [php] Undefined variable: model
Where'd $model come from in view.php? Because putting in the same line ($model->title) anywhere in _comments.php just breaks it further.
EDIT: Here's the CallComment part of the controller:
public function actionCallCommentForm($id='')
{
$topic=Forum::model()->findByPk($id);
$this->renderPartial('_commentform', array(
'forum'=>$topic,
'model'=>new Comment,
//'view'=>array('view','id'=>$id),
'view'=>'view',
));
}
$model variable is coming from your controller initially. It is an instance of a Comment class which gets passed to the view via $this->render('view', array('model'=>$whatever)). This line will make $whatever available in the forum/view.php file under the name of $model. Now since you are dealing with partial views you have to track it because it is possible that the same $model variable will be passed to another partial view with $this->renderPartial('_comment', array('whatever'=>$model)) and now this will be accessible in partial view as $whatever.

A variable issue not showing on the front end

Here is the code I have:
in the php file:
if($special>0){
$lease_price = (($special/1000)*38);
} else {
$lease_price = (($price/1000)*38);
}
$lease_price = $this->currency->format($lease_price);
and in the front end tpl file:
<p>
<i class="fa fa-chevron-down"></i>
<b>Lease To Buy Price:</b>
<span><?php if($price>500){ ?>
<?php echo $lease_price; ?>
<?php } else { echo 'NA'; } ?></span>
</p>
Now I believe this works, but I think I'm entering the php code in the wrong area of the php document which is causing the variable $lease_price to not work.
Here's a link to a pastebin of my php file, where would I enter the code above? http://pastebin.com/bTPtvgUQ
Thanks for the help

Running a function of a class from a file displayed by that class

I have a class called CouponGenerator. This class has two functions:
public function generateCoupon() {
// CODE HERE
}
and
public function loadCoupon() {
require_once(dirname(__FILE__).'/templates/couponDiv.php');
}
Inside the couponDiv.php file I have some markup:
<?php
$coupon = new CouponGenerator();
?>
<div class="fbsc_container" id="outer_div">
<div class="fbsc_container" id="fbsc_content">
<h4 class="fbsc_text" id="fbsc_header"> [HEADER] </h4>
<p class="fbsc_text" id="fbsc_inside_text"> <?php $coupon->generateCoupon(); ?> </p>
</div>
</div>
Sadly $coupon->generateCoupon(); does not activate the generateCoupon() function for me at all. I am not sure if I am calling it correctly from inside of the couponDiv.php file.
How could I make it work correctly?
I solved it, forgot to put 'echo' before calling the function.
Case closed.

PHP function not found with require_once in html

I'm trying to execute a function in a file called 'function.php' into <?php ?> with a require_once, in HTML lines..
accueil.php (C:\wamp\www\e-commerce\MusicStore\accueil.php) :
<?php
require_once('http://localhost/e-commerce/MusicStore/templates/function.php');
?>
<!DOCTYPE html>
<html>
<body>
<?php
get_header();
?>
</body>
....
</html>
and function.php (C:\wamp\www\e-commerce\MusicStore\templates\function.php ):
<?php
function get_header()
{
echo "<header class=\"header\">
<div class=\"banniere\">
<img src=\"http://localhost/e-commerce/MusicStore/img/logo.png\" alt=\"banniere\"/>
</div>
</header>";
}
?>
I got " Fatal error: Call to undefined function get_header() ".
But if I put the echo outside of the function, it works.. This is only into the function It doesn't work.
Does anybody know how to resolve this? I'm trying to clear my website with adding PHP function to structure my html page.
EDIT : The double '{' is a mistake on Stackoverflow but isn't in the file.
you pls try this
<?php
require_once('templates/function.php');
?>
and replace function.php because it has extra{
<?php
function get_header()
{
echo "<header class=\"header\">
<div class=\"banniere\">
<img src=\"http://localhost/e-commerce/MusicStore/img/logo.png\" alt=\"banniere\"/>
</div>
</header>";
}
?>
Function has 2 opening braces {
Here is the fixed code:
function get_header() {
echo "<header class=\"header\">
<div class=\"banniere\">
<img src=\"http://localhost/e-commerce/MusicStore/img/logo.png\" alt=\"banniere\"/>
</div>
</header>";
}
I am assuming folder location of your accueil.php is same as function.php
<?php
require_once('function.php');
?>
If its different then try relative path. If you can tell about locations of your both files you will get better help here.
The better approach is:
function get_header() {
{
return "your code here";
}
echo get_header();
The function is not a very good place to echo from

Don't display if attribute is string(0)

I'm using the following section of code to display some icons on our Magento store with the idea being if there is nothing added in the icons section it shouldn't display, for some reason this isn't working...it is displaying a division as if something is there but there is actually nothing.
<?php
if($_helper->productAttribute($_product,($_product->geticons()), 'icons') !== null):
?>
<div class="product-icons">
<?php echo $_helper->productAttribute($_product,($_product->geticons()), 'icons') ?>
</div>
<?php endif; ?>
It needs to show Icons if they are coded in the attribute field and then hide the division if there is nothing added.
I've worked out that the code is returning a value of string(0) what do I need to change in my coding to get the desired effect?
Here's the thing you need, and you don't need call the same functionality twice to get the empty result. Define your variable and check if it is empty (null, undefined or false) or not
<?php $icons = $_helper->productAttribute($_product,($_product->getIcons()), 'icons');?>
<?php if(!empty($icons)):?>
<div class="product-icons">
<?php echo $icons;?>
</div>
<?php endif;?>
this could be even better solution as it wont call the helper unless there are icons defined but you first have to try it out on your codebase.
<?php if($_product->getIcons()):?>
<div class="product-icons">
<?php echo $_helper->productAttribute($_product,($_product->getIcons()), 'icons') ?>
</div>
<?php endif; ?>
and please check if it isn't a typo and it really is:
$_product->geticons()
or should it be
$_product->getIcons()
Something like:
<?php
if($_helper->productAttribute($_product,($_product->geticons()), 'icons'))
{
echo "<div class=\"product-icons\">";
echo $_helper->productAttribute($_product,($_product->geticons()), 'icons');
echo "</div>";
}
?>
would be better!
try
<?php
if(!empty($_helper->productAttribute($_product,($_product->geticons()), 'icons')))
{
echo "<div class=\"product-icons\">";
echo $_helper->productAttribute($_product,($_product->geticons()), 'icons');
echo "</div>";
}
?>

Categories