Magento: How to add text before price - magento

I have a shop with configurable Products.
In the category page with all my products, I want to add text before the price.
How can I do that? I use the Modern theme.

add your text before getPriceHtml($_product, true) ?> in catalog/product/list.phtm
Something like following :
<?php echo "YOUR TEXT" ?>
<?php echo $this->getPriceHtml($_product, true) ?>

you can look in
app/design/frontend/default/yourtheme/template/catalog/product/price.phtml
if your theme in have no file use
you can look in
\app\design\frontend\base\default\template\catalog\product\price.phtml
Around line 189 you will find the following:
<span id=”product-price-<?php echo $_id ?>
<?php echo $this->getIdSuffix() ?>”>
Just add the following above the line:
<?php echo $this->__(‘Your Text Goes Here:’) ?>
so that you have something like this:
<?php echo $this->__(‘Your Text Goes Here:’) ?>
<span class=”regular-price” id=”product-price-<?php echo $_id ?>
<?php echo $this->getIdSuffix() ?>”>
Your text will be reflected before the price in both
the catalog and the product page.

In magento 2, You need to make changes in price-box.js which is placed at
/vendor/magento/module-catalog/view/base/web/js/price-box.js
Make sure you take this js in your custom theme folder and make changes on line number 22 near
priceTemplate: '<span class="price"><%- data.formatted %></span>'
Add your custom text after <span class="price">
Some thing like this,
priceTemplate: '<span class="price">Price - <%- data.formatted %></span>'
Done.

For adding label before price you need to override final_price.phtml file in your custom theme -
Core file path -
vendor/magento/module-catalog/view/base/templates/product/price/final_price.phtml
Override in your custom theme -
app/design/frontend/VendorName/ThemeName/Magento_Catalog/templates/product/price/final_price.phtml
Change code as below -
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
// #codingStandardsIgnoreFile
?>
<?php
/** #var \Magento\Catalog\Pricing\Render\FinalPriceBox $block */
/** ex: \Magento\Catalog\Pricing\Price\RegularPrice */
/** #var \Magento\Framework\Pricing\Price\PriceInterface $priceModel */
$priceModel = $block->getPriceType('regular_price');
/** ex: \Magento\Catalog\Pricing\Price\FinalPrice */
/** #var \Magento\Framework\Pricing\Price\PriceInterface $finalPriceModel */
$finalPriceModel = $block->getPriceType('final_price');
$idSuffix = $block->getIdSuffix() ? $block->getIdSuffix() : '';
$schema = ($block->getZone() == 'item_view') ? true : false;
?>
<?php if ($block->hasSpecialPrice()): ?>
<span class="special-price">
<?php /* #escapeNotVerified */ echo $block->renderAmount($finalPriceModel->getAmount(), [
'display_label' => __('Custom Label 1 : '),
'price_id' => $block->getPriceId('product-price-' . $idSuffix),
'price_type' => 'finalPrice',
'include_container' => true,
'schema' => $schema
]); ?>
</span>
<span class="old-price">
<?php /* #escapeNotVerified */ echo $block->renderAmount($priceModel->getAmount(), [
'display_label' => __('Custom Label 2 : '),
'price_id' => $block->getPriceId('old-price-' . $idSuffix),
'price_type' => 'oldPrice',
'include_container' => true,
'skip_adjustments' => true
]); ?>
</span>
<?php else: ?>
<?php /* #escapeNotVerified */ echo $block->renderAmount($finalPriceModel->getAmount(), [
'display_label' => __('Custom Label 3 : '),
'price_id' => $block->getPriceId('product-price-' . $idSuffix),
'price_type' => 'finalPrice',
'include_container' => true,
'schema' => $schema
]); ?>
<?php endif; ?>
<?php if ($block->showMinimalPrice()): ?>
<?php if ($block->getUseLinkForAsLowAs()):?>
<a href="<?= /* #escapeNotVerified */ $block->getSaleableItem()->getProductUrl() ?>" class="minimal-price-link">
<?= /* #escapeNotVerified */ $block->renderAmountMinimal() ?>
</a>
<?php else:?>
<span class="minimal-price-link">
<?= /* #escapeNotVerified */ $block->renderAmountMinimal() ?>
</span>
<?php endif?>
<?php endif; ?>
Here I have change text in below code also added in this code in last else condition as it was not present in else condition.
'display_label' => __('Custom Label 3 : '),
Thanks

Related

Retrieve the image in codeigniter

I have upload controller to upload images. file_view is the html file where I used to upload my image and upload_success is the file where I used to retrieve file. In the upload_success file output appears like this
file_name: images.jpg
file_type: image/jpeg
file_path: C:/xampp/htdocs/Test/uploads/
full_path: C:/xampp/htdocs/Test/uploads/images.jpg
raw_name: images
orig_name: images.jpg
client_name: images.jpg
file_ext: .jpg
file_size: 10.65
is_image: 1
image_width: 218
image_height: 232
image_type: jpeg
image_size_str: width="218" height="232"
Can someone help me to view the image using tag?
Controller
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Upload_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index(){
$this->load->view('file_view', array(
'error' => ' '
));
}
public function file_view()
{
$this->load->view('file_view', array(
'error' => ' '
));
}
public function do_upload()
{
$config = array(
'upload_path' => "./uploads/",
'allowed_types' => "gif|jpg|png|jpeg|pdf",
'overwrite' => TRUE,
'max_size' => "2048000", // Can be set to particular file size , here it is 2 MB(2048 Kb)
'max_height' => "768",
'max_width' => "1024"
);
$this->load->library('upload', $config);
if ($this->upload->do_upload()) {
$data = array(
'upload_data' => $this->upload->data()
);
$this->load->view('upload_success', $data);
} else {
$error = array(
'error' => $this->upload->display_errors()
);
$this->load->view('file_view', $error);
}
}
}
?>
File_view
<?php echo $error;?>
<!-- Error Message will show up here -->
<?php echo form_open_multipart( 'upload_controller/do_upload');?>
<?php echo "<input type='file' name='userfile' size='20' />"; ?>
<?php echo "<input type='submit' name='submit' value='upload' /> ";?>
<?php echo "</form>"?>
upload_success
<h3>Your file was successfully uploaded!</h3>
<!-- Uploaded file specification will show up here -->
<ul>
<?php foreach ($upload_data as $item => $value):?>
<li>
<?php echo $item;?>:
<?php echo $value;?>
</li>
<?php endforeach; ?>
</ul>
<p>
<?php echo anchor('upload_controller/file_view', 'Upload Another File!'); ?>
</p>
Try this:
<h3>Your file was successfully uploaded!</h3>
<!-- Uploaded file specification will show up here -->
<ul>
<?php foreach ($upload_data as $item => $value):?>
<li>
<?php echo $item;?>:
<img src="<?php echo $value;?>">
</li>
<?php endforeach; ?>
</ul>
<p>
<?php echo anchor('upload_controller/file_view', 'Upload Another File!'); ?>
</p>
try this :
<h3>Your file was successfully uploaded!</h3>
<!-- Uploaded file specification will show up here -->
<ul>
<li>
<img src="<?php echo base_url('/uploads/').$upload_data['file_name'] ?>">
</li>
</ul>
<p>
<?php echo anchor('upload_controller/file_view', 'Upload Another File!'); ?>
</p>
Simply do this to view file
<h3>Your file was successfully uploaded!</h3>
<img src="<?php echo upload_data['full_path']; ?>">
<?php echo anchor('upload_controller/file_view', 'Upload Another File!'); ?>
Hope so this will help you.
Finally I found the answer
<h3>Your file was successfully uploaded!</h3>
<!-- Uploaded file specification will show up here -->
<ul>
<li>
</li>
<img alt="Your uploaded image" src="<?=base_url(). 'uploads/' . $upload_data['file_name'];?>">
</ul>
<p>
<?php echo anchor('upload_controller/file_view', 'Upload Another File!'); ?>
</p>

how to show my account link in header after successfully login in magento 2.0

Homepage
I would like to show My account after login.
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('Magento\Customer\Model\Session');
if($customerSession->isLoggedIn()) {
// display your link here
}
----------------------from controller-------------------
$this->_objectManager->get('Magento\Customer\Model\Session');
if($customerSession->isLoggedIn()) {
// display your link here
}
We can do that easily Suppose We need to show only My-Account link after sign In
We have to override the authorization.phtml file in custom theme and we can put our logic based on our requirement.
app/design/frontend/Namespace/Customtheme/Magento_Customer/templates/account/link/ authorization.phtml
After overriding this file we can put our login -
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/** #var \Magento\Customer\Block\Account\AuthorizationLink $block */
$objectManagerlogin = \Magento\Framework\App\ObjectManager::getInstance();
$baseurl = $objectManagerlogin->get('Magento\Store\Model\StoreManagerInterface')->getStore(0)->getBaseUrl();
$dataPostParam = '';
if ($block->isLoggedIn()) {
$dataPostParam = sprintf(" data-post='%s'", $block->getPostParams());
}
?>
<?php if($block->isLoggedIn() && $baseurl || $block->isLoggedIn() ) : ?>
<li class="authorization-link" >
Sign Out
</li>
<li class="authorization-link custom-top-link-myaccount-mobile" >
My Account
</li>
<?php else : ?>
<li class="authorization-link" data-label="<?= $block->escapeHtmlAttr(__('or')) ?>">
<a <?= /* #noEscape */ $block->getLinkAttributes() ?><?= /* #noEscape */ $dataPostParam ?>>
<?= $block->escapeHtml($block->getLabel()) ?>
</a>
</li>
<?php endif; ?>
I hope it will work let me know if you have any issue

Yii2 Unable append Ajax success to CKeditor

I was trying to create a newsletter module using Yii2 basic.
This is my scenario,
If a predefined template is available, I have to select that template.
If template is selected the subject and content should be loaded automatically.
For this I am using an Ajax.My Ajax is working perfectly and I appended the newsletter subject with Ajax success,Problem occurred when I tried to append the newsletter content.Hence I am using CKeditor.
My Form
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use app\modules\admin\models\NewsletterTemplates;
/* #var $this yii\web\View */
/* #var $model app\modules\admin\models\Letter */
/* #var $form yii\widgets\ActiveForm */
?>
<div class="letter-form form_style " >
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'letter_template_id')->dropDownList(
ArrayHelper::map(NewsletterTemplates::find()->all(),'newsletter_temp_id','newsletter_temp_subject'),
['prompt' => 'Select','class'=>'form-contol','onchange'=>'
$.post( "'.Yii::$app->urlManager->createUrl('admin/letter/temp?id=').'"+$(this).val(), function( data ) {
var message = data.split("::");
//alert(message[1]);
$( "#letter-letter_sub" ).val( message[0] );
$( "#letter-letter_content" ).val( message[1] );
});'
]);
?>
<?php //echo $form->field($model, 'letter_template_id')->textInput(['class'=>'form-contol']) ?>
<?= $form->field($model, 'letter_to')->textInput(['class'=>'form-contol']) ?>
<?= $form->field($model, 'letter_sub')->textInput(['class'=>'form-contol']) ?>
<?= $form->field($model, 'letter_content')->textarea(['class'=>'ckeditor']) ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Send' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
Can anyone help with this......
Thanks in advance....
Hi Guys I found out the answer
I just replace the instances of the CKeditor.
<?= $form->field($model, 'letter_template_id')->dropDownList(
ArrayHelper::map(NewsletterTemplates::find()->all(),'newsletter_temp_id','newsletter_temp_subject'),
['prompt' => 'Select','class'=>'form-contol','onchange'=>'
$.post( "'.Yii::$app->urlManager->createUrl('admin/letter/temp?id=').'"+$(this).val(), function( data ) {
var message = data.split("::");
$( "#letter-letter_sub" ).val( message[0] );
CKEDITOR.instances["letter-letter_content"].destroy(true);
CKEDITOR.replace( "letter-letter_content" );
var editor = CKEDITOR.instances[ "letter-letter_content" ];
editor.setData(message[1]);
});'
]);
?>
Thank you for your supports

CStarRating shows radio group instead of stars when update?

I have edited Comment module from a tutorial to review module.
In this module i using CStarRating widget. When view, submit, delete rate post it OK. but when i click edit/update, edit old post and click update to edit rate post then star don't show, instead this radio group ???
Here is code:
CommentList.php
<?php
/** #var CArrayDataProvider $comments */
$comments = $model->getCommentDataProvider();
$comments->setPagination(false);
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$comments,
'itemView'=>'comment.views.comment._view'
));
$this->renderPartial('comment.views.comment._form', array(
'comment'=>$model->commentInstance
));
_view.php
<?php
?>
<nav id = "atext" >
<div>
<span class="ext-comment-head">
wrote on
<span class="ext-comment-date">
<?php echo Yii::app()->format->formatDateTime(
is_numeric($data->createDate) ? $data->createDate : strtotime($data->createDate)
);
?>
</span>
</span>
<span class="ext-comment-options">
<?php if (!Yii::app()->user->isGuest && (Yii::app()->user->id == $data->userId)) {
echo CHtml::ajaxLink('delete', array('/comment/comment/delete', 'id'=>$data->id), array(
'success'=>'function(){ $("#ext-comment-'.$data->id.'").remove(); }',
'type'=>'POST',
), array(
'id'=>'delete-comment-'.$data->id,
'confirm'=>'Are you sure you want to delete this item?',
));
echo " | ";
echo CHtml::ajaxLink('edit', array('/comment/comment/update', 'id'=>$data->id), array(
'replace'=>'#ext-comment-'.$data->id,
'type'=>'GET',
), array(
'id'=>'ext-comment-edit-'.$data->id,
));
} ?>
</span>
<p id = "apa">
<br>
</p>
<table id = "atable">
<td id = "test123">
<?php
$model=new Comment;
$a=$data->id;
$colArr=$model->attributes;
for ($i = 0; $i < count($colArr); $i++) {
$key=key($colArr);
$val=$colArr[$key];
next($colArr);
if (($key<> 'average') and($key<>'id') and ($key<>'message')
and ($key<>'userId') and ($key<>'createDate') ) {
$a=$a+100;
echo $data->getAttributeLabel($key).' ('.$data->$key.')';
$this->widget('CStarRating',array(
'name'=>'Comment['.$a.']',
'value'=>$data->$key,
'minRating'=>1, //minimal value
'maxRating'=>5,//max value
'starCount'=>5, //number of stars
'readOnly'=>true,
));
}
echo "<br>";
}
?>
</td>
<td id = "average-teacher">
<p>
<?php echo nl2br(CHtml::encode($data->message)); ?>
</p>
</td>
</table>
<br style="clear: both;"/>
</div>
</nav>
</div>
_form.php
<br>
<br>
<?php if (Yii::app()->user->isGuest) {
?><div class="ext-comment-not-loggedin">
Sorry, you have to login to leave a comment.
</div><?php } else { ?>
<div id="ext-comment-form-<?php echo $comment->isNewRecord ? 'new' : 'edit-'.$comment->id; ?>" class="form" >
<nav id = "comment-a">
<?php $form = $this->beginWidget('CActiveForm', array(
'id'=>'ext-comment-form',
'action'=>array('/comment/comment/create'),
'enableAjaxValidation'=>false
)); ?>
<?php /** #var CActiveForm $form */
echo $form->errorSummary($comment); ?>
<div class="row">
<?php echo $form->labelEx($comment,'message'); ?>
<?php echo $form->textArea($comment,'message',array('rows'=>3, 'cols'=>76)); ?>
<?php echo $form->error($comment,'message'); ?>
</div>
<?php
$model=new Comment;
$colArr=$model->attributes;
for ($i = 0; $i < count($colArr); $i++) {
$key=key($colArr);
if (($key<> 'average') and ($key<>'id') and ($key<>'message') and ($key<>'userId')
and ($key<>'createDate') ){
$this->widget('CStarRating',array(
'name'=>'Comment['.$key.']',
'value'=>$model->$key,
'minRating'=>1, //minimal value
'maxRating'=>5,//max value
'starCount'=>5, //number of stars
));
echo " ".$model->getAttributeLabel($key)."<br>";
echo "<br>";}
next($colArr);
}
?>
<div class="row buttons">
<?php if ($comment->isNewRecord) {
echo "<br>";
echo $form->hiddenField($comment, 'type');
echo $form->hiddenField($comment, 'key');
echo CHtml::ajaxSubmitButton('Submit',
array('/comment/comment/create'),
array(
'replace'=>'#ext-comment-form-new',
'error'=>"function(){
$('#Comment_message').css('border-color', 'red');
$('#Comment_message').css('background-color', '#fcc');
}"
),
array('id'=>'ext-comment-submit' . (isset($ajaxId) ? $ajaxId : ''))
);
} else {
echo CHtml::ajaxSubmitButton('Update',
array('/comment/comment/update', 'id'=>$comment->id),
array(
'replace'=>'#ext-comment-form-edit-'.$comment->id,
'error'=>"function(){
$('#Comment_message').css('border-color', 'red');
$('#Comment_message').css('background-color', '#fcc');
}"
),
array('id'=>'ext-comment-submit' . (isset($ajaxId) ? $ajaxId : ''))
);
}
echo "<br>";
?>
</div>
<?php $this->endWidget() ?>
</nav>
</div><!-- form -->
<?php } ?>
Hope someone can help me :( And sorry about my english :(

Displaying Specific Magento Categories From Category ID

As of yet, I haven't managed to find anything online that already caters for what I'm trying to achieve. I simply want to call in specific categories to a list but I want to be able to define which categories by ID, so for example, I would like to be able to call them in like something such as the below:-
{{block type="catalog/navigation" name="catalog.category" template="developer/extension/script.phtml" ids="3,6,17,143,57"}}
I'm already displaying a list of sub categories in various places based on the parent category ID but in instances where there are hundreds of sub categories, it isn't always practical to display all of them, so I'm wondering if the existing script can possibly be tweaked to only include specific categories as per above?
<?php
//gets all sub categories of parent category 'cat-id-4'
$cats = Mage::getModel('catalog/category')->load(4)->getChildren();
$catIds = explode(',',$cats);
$categories = array();
foreach($catIds as $catId) {
$category = Mage::getModel('catalog/category')->load($catId);
$categories[$category->getName()] = array(
'url' => $category->getUrl(),
'img' => $category->getImageUrl()
);
}
ksort($categories, SORT_STRING);
?>
<ul>
<?php if($category->getIsActive()): ?>
<?php foreach($categories as $name => $data): ?>
<li>
<?php echo $name; ?>
</li>
<?php endforeach; ?>
<?php endif; ?>
</ul>
If anyone could advise how I could possibly achieve this please, that would be fantastic - Thanks in advance.
This should work with your given CMS block code:
<?php
$catIds = explode(',', $this->getIds()); //<-- ONLY CHANGE MADE
$categories = array();
foreach($catIds as $catId) {
$category = Mage::getModel('catalog/category')->load($catId);
$categories[$category->getName()] = array(
'url' => $category->getUrl(),
'img' => $category->getImageUrl()
);
}
ksort($categories, SORT_STRING);
?>
<ul>
<?php if($category->getIsActive()): ?>
<?php foreach($categories as $name => $data): ?>
<li>
<?php echo $name; ?>
</li>
<?php endforeach; ?>
<?php endif; ?>
</ul>

Resources