Opencart 3.0.3.7 Trying to access array offset on value of type bool - opencart

I'm running Opencart 3.0.3.7 on PHP 7.4 and I'm getting this message in the error log:
PHP Notice: Trying to access array offset on value of type bool in /home/site/public_html/catalog/model/extension/module/so_filter_shop_by.php on line 313
PHP Notice: Trying to access array offset on value of type bool in /home/site/public_html/catalog/model/extension/module/so_filter_shop_by.php on line 314
The code is :
foreach($query->rows as $result)
{
$data = $this->model_catalog_product->getProduct($result['product_id']);
$price = $this->tax->calculate($data['price'], $data['tax_class_id'], $this->config->get('config_tax'));
if ((float)$data['special']) {
$price = $this->tax->calculate($data['special'], $data['tax_class_id'], $this->config->get('config_tax'));
}
$price = $this->currency->format($price, $this->session->data['currency']);
if ($this->language->get('decimal_point') == ',') {
$price = trim(str_replace(',', '.', $price));
}
else {
$price = trim(str_replace(',', '', $price));
}
$price = trim(str_replace($currencies, '', $price));
$data['price_soFilter'] = $price;
$product_data[] = $data;
}
return $product_data;
}
Could anyone suggest a solution to resolve this error.

you have to check if the $price NOT NULL try something like this:
if(is_null($price))
{
$price = '';
} else {
$price = $this->tax->calculate($data['price'], $data['tax_class_id'], $this->config->get('config_tax'));
}

You can try:
if(!empty($price)) {
$price = $this->tax->calculate((float)$data['price'], $data['tax_class_id'], $this->config->get('config_tax'));
} else {
$price = '';
}

Related

A new entity was found through the relationship?

I have a handle code
if ($this->request->isMethod('POST') && $valid) {
$em = $this->getDoctrine()->getManager();
$formData = $form->getData();
$staffValue = $formData['staff'];
$campaignDetailFilter = $modelCampaignDetail->getRepository()->countDataByCampaignDetailId($formData['campaignDetail']);
$total = count($campaignDetailFilter);
$totalStaff = count($formData['staff']);
foreach ($campaignDetailFilter as $valDetailId) {
$detailDataEntity = $modelDetailData->getEntity($valDetailId['id']);
$batchSize = $total / $totalStaff;
$i = 0;
foreach ($staffValue as $staffVal) {
$detailDataEntity->setStaff($staffVal);
$em->persist($detailDataEntity);
if (($i % $batchSize) === 0) {
$em->flush();
$em->clear();
}
++$i;
}
$em->flush();
$em->clear();
}
}
But when I give $i = 0 it gets an error: A new entity was found through the relationship...that was not configured to cascade persist operations for entity.
You should remove $em->clear().
if (($i % $batchSize) === 0) {
$em->flush();
}

How can I get email from Google People API?

I am using Google people API client library in PHP.
After successfully authenticating, I want to get email
Help me to find out the problem from my code below.
Can i use multiple scopes to get email, beacuse when i m using different scope it gives me error
function getClient() {
$client = new Google_Client();
$client->setApplicationName('People API');
$client->setScopes(Google_Service_PeopleService::CONTACTS);
$client->setAuthConfig('credentials.json');
$client->setAccessType('offline');
$client->setPrompt('select_account consent');
$str = file_get_contents('credentials.json');
$json = json_decode( $str, true );
$url = $json['web']['redirect_uris'][0];
if (isset($_GET['oauth'])) {
$auth_url = $client->createAuthUrl();
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
} else if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
$redirect_uri = $url;
header('Location: ' . filter_var($redirect_uri,FILTER_SANITIZE_URL));
} else if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
$people_service = new Google_Service_PeopleService($client);
} else {
$redirect_uri = $url.'/?oauth';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
return $people_service;
}
$optParams = array(
'pageSize' => 100,
'personFields' => 'addresses,ageRanges,biographies,birthdays,braggingRights,coverPhotos,emailAddresses,events,genders,imClients,interests,locales,memberships,metadata,names,nicknames,occupations,organizations,phoneNumbers,photos,relations,relationshipInterests,relationshipStatuses,residences,sipAddresses,skills,taglines,urls,userDefined',
);
$people_service = getClient();
$connections = $people_service->people_connections-
>listPeopleConnections('people/me', $optParams);

Currency convert in category page

I want show in category multiple curency price
i have a code tike this
$this->currency->convert($price, 'RUB', 'CNY'),
Where to put this in caregory controller for working?
I resolve my problem like this
if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) {
$price = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
$price_2 = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')), 'CNY');
} else {
$price = false;
$price_2 = false;
}
Maybe someone have better way to make.

how to add product to cart in opencart

Below is add to product code . But I am not getting where the values are storing . Kindly help to find out solution for this . I want to know logic behind this code
public function add($product_id, $qty = 1, $option = array(), $recurring_id = 0) {
$this->data = array();
$product['product_id'] = (int)$product_id;
if ($option) {
$product['option'] = $option;
}
if ($recurring_id) {
$product['recurring_id'] = (int)$recurring_id;
}
$key = base64_encode(serialize($product));
if ((int)$qty && ((int)$qty > 0)) {
if (!isset($this->session->data['cart'][$key])) {
$this->session->data['cart'][$key] = (int)$qty;
} else {
$this->session->data['cart'][$key] += (int)$qty;
}
}
}
The product details with options are stored in $key = base64_encode(serialize($product));. Where $this->session->data['cart'][$key] contains the number of quantity added by the customer.
For more details check the getProducts() function on the same page. Where you can find
foreach ($this->session->data['cart'] as $key => $quantity) {
....
$product = unserialize(base64_decode($key));
....
}

Regex to validate information (Letter & Number only)

Regex to validate information
I tried the following:
if(preg_match("/[A-Za-z0-9]+/", $ingame_name) == TRUE){
header("Location: newitem.php?username=". $ingame_name ."&email=". $email);
} else {
$invalidusername = '<font color=red>Oops, invalid username! Username
may only contain numbers and letters.</font><br>';
}
Which didn't work, also tried flipping the statements still didn't work...
And my final attempt
if ($_REQUEST['do'] == 'submit')
{
$ingame_name= trim($_POST['ingame_name']);
$email = trim($_POST['email']);
if(eregi("/[A-Za-z0-9]+/", $ingame_name))
{
$invalidusername = '<font color=red>Oops, invalid username! Username may only contain numbers and letters.</font><br>';
$ingame_name = 'invalid';
}
if (eregi("^[a-zA-Z0-9_]+#[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", $email))
{
$invalidemail = '<font color=red>Oops, seems that you have an error with your email format.</font></br>';
$email = 'invalid';
}
if ( $ingame_name = 'invalid' || $email = 'invalid')
{
/* do nothin */
}
else
{
header("Location: item.php?username=". $ingame_name ."&email=". $email);
}
}
Nothing seems to work,
Try this:
<?php
$valid_submit = ($_REQUEST && isset($_REQUEST['do']) && $_REQUEST['do'] == 'submit') ? true : false;
if ($valid_submit) {
$ingame_name= ($_POST && isset($_POST['ingame_name'])) ? trim($_POST['ingame_name']) : '';
$email= ($_POST && isset($_POST['email'])) ? trim($_POST['email']) : '';
$invalidusername = '';
$invalidemail = '';
if(!preg_match("/^[A-Za-z0-9]+$/", $ingame_name)) {
$invalidusername = '<font color=red>Oops, invalid username! Username may only contain numbers and letters.</font><br>';
$ingame_name = 'invalid';
}
// This is a much more efficient method to validate email addresses
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$invalidemail = '<font color=red>Oops, seems that you have an error with your email format.</font></br>';
$email = 'invalid';
}
if ( $ingame_name != 'invalid' && $email != 'invalid') {
$location = 'item.php?username=' . $ingame_name . '&email=' . $email;
header("Location: $location");
}
else {
//echo $invalidusername . $invalidemail;
}
}