How to display customer id in header.tpl on Opencart 2?
if ($this->customer->isLogged()) {
$data['customer_id'] = $this->customer->getId(); // customer ID
$data['customer_fname'] = $this->customer->getFirstName(); // customer email
}
doesn`t work.
In header.php file add this two variables
if ($this->customer->isLogged()) { // <-- This line is around 52
$this->load->model('account/wishlist');
$data['text_wishlist'] = sprintf($this->language->get('text_wishlist'), $this->model_account_wishlist->getTotalWishlist());
$data['customer_id'] = $this->customer->getId(); // <-- add this variable
$data['customer_fname'] = $this->customer->getFirstName(); // <-- and this variable
} else {
$data['text_wishlist'] = sprintf($this->language->get('text_wishlist'), (isset($this->session->data['wishlist']) ? count($this->session->data['wishlist']) : 0));
}
In header.tpl file you can use the variables like this, must be inside the if ($logged) block
<?php if ($logged) { ?>
<?php echo $customer_id; ?>
<?php echo $customer_fname; ?>
<?php } ?>
Tested on OC 2.2.0.0
Related
Is it possible to use only the OC Event system for these overrides?
I need to override the core index function of the product controller. I want to edit the core file and add this line:
$data['quantity'] = $product_info['quantity'];
to the function index() in "class ControllerProductProduct extends Controller"
and then I need add this line in product.twig file:
<span>{{ quantity }}</span>
I want to use only the OC Event system, not ocmod or vqmod.
It is real?
Thanks for help.
First you need to add your event into database, you can use addEvent function inside your module install function, like this:
public function install(){
$this->load->model('setting/event');
$this->model_setting_event->addEvent('my_module', 'catalog/view/product/product/after', 'extension/module/my_module/edit_product_page');
}
Or add it manually:
INSERT INTO `oc_event` (`code`, `trigger`, `action`, `status`) VALUES ('my_module', 'catalog/view/product/product/after', 'extension/module/my_module/edit_product_page', 1);
Now create this file:
catalog\controller\extension\module\my_module.php
And its contents:
<?php
class ControllerExtensionModuleMyModule extends Controller {
public function edit_product_page(&$route = '', &$data = array(), &$output = '') {
if (isset ($this->request->get['product_id'])) {
$product_info = $this->model_catalog_product->getProduct($this->request->get['product_id']);
if ($product_info) {
// Display it before <li>Product Code:
$find = '<li>' . $data['text_model'];
$replace = '<li>Quantity: ' . $product_info['quantity'] . '</li>' . $find;
$output = str_replace($find, $replace, $output);
}
}
}
}
Result:
MacBook
Brands Apple
Quantity: 929
Product Code: Product 16
Reward Points: 600
Availability: In Stock
I run Laravel 5.4.
I have those routes :
<?php
Route::get('/users/{name}', 'UsersController#showByText')->where('name', '[\w]+');
Route::get('/users/{id}', 'UsersController#showById')->where('id', '[\d]+');
?>
And my controllers :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class DashboardController extends Controller
{
function showByText($name)
{
return DB::table('users')->where('name', 'LIKE', $name)->get();
}
function showById($id)
{
return DB::table('users')->where('id', $id)->get();
}
}
?>
Question :
Could I end up with one single method, to be able to do :
<?php
Route::get('/users/{mixed}', 'UsersController#show');
?>
And my controller would look like :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class DashboardController extends Controller
{
function show()
{
if( Request::match('mixed', '[\w]+') )
{
return DB::table('users')->where('name', 'LIKE', $mixed)->get();
}
else if( Request::match('mixed', '[\d]+') )
{
return DB::table('users')->where('id', $mixed)->get();
}
}
}
?>
I know Request::match() does not exists, and knowing that Route::pattern('id', '[\d]+'); will force my method show to be compliant only for digits. I would like to know if there is a shorthand for preg_match() with the Request component.
Is it possible to retrieve the template name inside of a template (the .phtml thingy)? I can get the ViewModel's template with
echo $this->viewModel()->getCurrent()->getTemplate();
but that doesn't work on partials (obviously). So how can I retrieve the template's name currently being rendered?
You could do this like this:
class Module
{
public function onBootstrap (MvcEvent $e)
{
$eventManager = $e->getApplication ()
->getEventManager ();
$eventManager->attach (
MvcEvent::EVENT_RENDER,
function (MvcEvent $e)
{
$layout = $e->getViewModel ();
$view = reset ($layout->getChildren ());
$view->template1 = $view->getTemplate ();
});
}
and then in the view:
<?php
echo $this->template1;
?>
Simple, but highly effective, solution:
$where_am_i = __FILE__;
I want to redirect a URL in the form of
http://a_domain_name.com/2013/08/link_name/feed to http://a_domain_name.com/link_name/feed. I am a WordPress beginner and finding it hard to do this . Whats the best way to do this ?
I tried using the safe redirection plugin with http://domain_name.com/%year%/%monthnum%/%postname%/feed to http://domain_name.com/%postname%/feed but it's not working
The Proper Way
Log into wordpress admin panel and go to the settings>permalinks page.
From here, select the second to last option, ie:
http://domain.com/blogname/blog/sample-post/
Alternatively, you can create a redirect file. This involves hacking up wordpress a bit. I wouldn't recommend this if you're not comfortable with wordpress & php. To do this, first:
Open the setting>permalinks page in the admin panel:
Select the last option in the list (custom) and enter something like this:
/redirect.php?p=%post_id%&a=%author%&c=%category%
You'll also need to change the two optional forms below to:
Category base
/redirect.php?c=
Tag base
/redirect.php?t=
Now, let's create a redirect php file. Place this in /yourblogname/blog/
<?php
$total=0;
$string = 'http://www.yourdomain.com/';
$fetch = '';
if (isset($_GET['p'])&&!is_null($_GET['p']))
{
$p = $_GET['p'];
if ($total ==0) {
$string.='?p='.$p;
} else {
$string.='&p='.$p;
}
$total++;
}
if (isset($_GET['t'])&&!is_null($_GET['t']))
{
$t = str_replace('/','',$_GET['t']);
if ($total ==0) {
$string.='?tag='.$t;
} else {
$string.='&tag='.$t;
}
$total++;
}
if (isset($_GET['a'])&&!is_null($_GET['a']))
{
$a = $_GET['a'];
if ($total ==0) {
$string.='?a='.$a;
} else {
$string.='&a='.$a;
}
$total++;
}
if (isset($_GET['s'])&&!is_null($_GET['s']))
{
$s = $_GET['s'];
if ($total ==0) {
$string.='?s='.$s;
} else {
$string.='&s='.$s;
}
$total++;
}
if (isset($_GET['c'])&&!is_null($_GET['c']))
{
$c = str_replace('/','',$_GET['c']);
if ($total ==0) {
$string.='?category_name='.$c;
} else {
$string.='&category_name='.$c;
}
$total++;
}
echo '<head><meta http-equiv="Refresh" content="0; URL='.$string.'">';
?>
<style>
html {
background:black;
color:white;
}
</style>
</head>
<body>
<div id=cont>
<p align=center><div style='font-size:72px;text-align:center;' ><?php echo "redirecting..."; ?></p></div>
</div>
</body>
We're not done yet. We need to adjust the page we're redirecting to now... (index.php)
if (!isset($_GET['p'])) {
if (!isset($_GET['category_name']) && !isset($_GET['tag']) && !isset($_GET['s']) && !isset($_GET['search']) ) {
include('newhome.php'); //frontpage with no special redirect
} else {
include('listabbrposts.php'); //a list of abbreviated posts for browsing
}
} else {
include('entry.php'); // a page with a single article
}
For a working example, you can check out my page where I use this technique: http://www.artfuladvection.com click on a topic or tag from the left to see the redirect.
Everything else works fine except for the location. The actual city/state does not come across. Instead the word Array is displayed. My field type in the database is text but I've tried every field type through mysql. The HTML file is JSON where it comes to the field names. I am very new at this so any help is appreciated.
This is my php file:
<? ob_start(); ?>
<?php
define('FACEBOOK_APP_ID', '');
define('FACEBOOK_SECRET', '');
// No need to change function body
function parse_signed_request($signed_request, $secret) {
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
// decode the data
$sig = base64_url_decode($encoded_sig);
$data = json_decode(base64_url_decode($payload), true);
if (strtoupper($data['algorithm']) !== 'HMAC-SHA256') {
error_log('Unknown algorithm. Expected HMAC-SHA256');
return null;
}
// check sig
$expected_sig = hash_hmac('sha256', $payload, $secret, $raw = true);
if ($sig !== $expected_sig) {
error_log('Bad Signed JSON signature!');
return null;
}
return $data;
}
function base64_url_decode($input) {
return base64_decode(strtr($input, '-_', '+/'));
}
if ($_REQUEST) {
$response = parse_signed_request($_REQUEST['signed_request'],
FACEBOOK_SECRET);
/*
echo "<pre>";
print_r($response);
echo "</pre>"; // Uncomment this for printing the response Array
*/
$name = $response["registration"]["name"];
$email = $response["registration"]["email"];
$gender = $response["registration"]["gender"];
$prosecutor = $response["registration"]["prosecutor"];
$location = $response["registration"]["location"];
// Connecting to Database
$con = mysql_connect("my_hosting_site","Database","password");
if (!$response)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("Database", $con);
mysql_query("INSERT INTO my_table (name, email, gender, prosecutor, location)
VALUES ('$name', '$email', '$gender', '$prosecutor', '$location')");
mysql_close($con);
}
$URL="https://www.facebook.com";
header ("Location: $URL");
?>
<? ob_flush(); ?>
Facebook
Basically, it's an array.
[location] => Array
(
[name] => Delhi, India
[id] => 1.1604xxxxxx286E+14
)
Use this for getting location stored in your db, like I have written below:
$location = $response["registration"]["location"]["name"];