Opencart where are extension status and position set? - php

Quick question about open cart, where are extension status and position set? I can see in the code
$postion = $this->config->get($extension . '_position');
and also
'status' => $this->config->get($extension . '_status')
However I can't see where these are defined?

At first, look into your extension file ( for example 'payment/free_checkout.php') and search for something like that
$this->model_setting_setting->editSetting( 'free_checkout', $this->request->post);
This is where settings stored into database ( you can go deeper into setting model, if you want )
After that, open admin/index.php and look at lines 38 - 48.
You can see, system gets data from database and store data into config object.

In Opencart Positions are used by Modules. They are set in admin unders extensions/modules. When you click save - it saves them to the database table oc_settings under the modulename_module
When Opencart is launched - in INDEX.php there is this code
// Settings
$query = $db->query("SELECT * FROM " . DB_PREFIX . "setting WHERE store_id = '0' OR store_id = '" . (int)$config->get('config_store_id') . "' ORDER BY store_id ASC");
foreach ($query->rows as $setting) {
if (!$setting['serialized']) {
$config->set($setting['key'], $setting['value']);
} else {
$config->set($setting['key'], unserialize($setting['value']));
}
}
it creates the config - a massive array of all the settings for the current store id.
Afterwards the position controllers (column_left.php, column_right.php, content_top.php and content_bottom.php) go through the extensions in the table oc_extension and finds all the modules that will need to be shown.
then it goes through this massive array CONFIG and collects the settings for these modules - this all is stored in a array $module_data
then the controller uses these settings to basicly launch every controller of the module that should be shown. It passes the controller route and $settings for every module in a foreach loop and in result gets a render of the modules.
You can access the config anywhere in the php file - it can be another controller, a model or even tpl file.
$this->config->get(...)
If you want - you can go directly to the databes oc_settings and get the data from there with these functions
$this->load->model('setting/setting'); // remeber to always load the model.
$this->model_setting_setting->editSetting( 'free_checkout', $this->request->post);
Hope this helps.
Also you can use this module to expend the number of positions for your opencart SUPER easy
Extra positions Unlimited

Related

OpenCart - PHP - Check if a product (download) is already purchased

Regarding OpenCart store selling digital downloads.
I am trying to add a script to the product page to warn the customer if that specific product (download) is already purchased and exists in Account>Downloads.
The reason is to avoid customers purchasing the same product twice.
Appreciate your help.
Edit:
I have tried getting the product IDs of all orders by a customer using a SQL query and that works fine externally but inside OpenCart I am facing issues.
A query such as:
SELECT product_id
FROM ocuk_order_product
WHERE order_id IN (
SELECT order_id
FROM ocuk_order
WHERE customer_id = 'xxxx'
)
My main problem is not being sure how to get similar results in OpenCart Product page. (Which pages and path exactly and where inside the files)
Tried this post as well: Opencart get product downloads
But it is not exactly working on product page (product.php)
Opencart Version 3.0.2.0
Ok I'll take a stab at this but let's clarify a few things:
1) DB_PREFIX is simply a php constant that's declared in your config.php file. Based on the query you provided it's probably equal to ocuk. The point is, you write queries using this constant and they become portable across installations regardless of the users' chosen database prefix.
2) Opencart is based on the MCVL (an extension of MVC which includes language files) structure. The place to execute queries is in the model. The place to call model methods is in the controller. The controller passes output to the view and often uses language variables from language files.
So what I would do is write function and put it in your product model – in this case that's catalog/model/catalog/product.php. This function is called a method since it's now part of your model class. The function will output the rows in your table that a logged in customer has purchased the given product. It's also important to check (a) that the customer is logged in and (b) that the orders you are querying against are real orders - with an order_status_id > 0. We can go into that but suffice to say that you always need to check the order_status_id if you want to know about actual completed orders. Such a method could look something like this:
public function getOrderProductHistory($product_id) {
$result = [];
if ($customer_id = $this->customer->getId()) {
$sql = "
SELECT *
FROM " . DB_PREFIX . "order o
JOIN " . DB_PREFIX . "order_product op USING (order_id)
WHERE o.order_status_id > 0
AND o.customer_id = '" . (int)$customer_id . "'
";
$query = $this->db->query($sql);
$result = $query->rows;
}
return $result;
}
Now in your controller (on a product page that's catalog/controller/product/product.php) you can call this method to get results like this:
$order_product_history = $this->model_catalog_product->getOrderProductHistory($product_id);
And then do something based on the output:
if ($order_product_history) {
$data['has_been_ordered'] = true;
} else {
$data['has_been_ordered'] = false;
}
Now $has_been_ordered is a boolean that you can access in your view to display a message to the customer. There's still a bit more code for you to write but hopefully this helps get you off to a good start.

Get access to $item->getNumberOfDownloadsUsed() function on Download.php

I need to get the number of downloads bought and used, that's two methods that are available on $item object. But I can't instance $item on the file that I'm working:
app/code/core/Mage/Downloadable/Helper/Download.php
On this file, I need to retrieve the number of boughts and used of the download purchased.
I'm also trying to get the order ID or at least the link hash to identify that number of boughts/used downloads with the unique id of the purchase.
For example, I try this:
Mage::getModel('downloadable/link_purchased_item')
->load($this->getOrderItem()->getOrder()->getId(), 'order_id');
But $this->getOrderItem() is not available on Download.php file. I was trying this:
Mage::getModel('downloadable/link_purchased_item')->getCollection()
->addFieldToFilter('order_item_id', $this->getOrderItem()->getId());
But obviously getOrderItem() is unavailable.
Fatal error: Call to undefined method Mage_Downloadable_Helper_Download::getOrderItem() in /[...]/app/code/core/Mage/Downloadable/Helper/Download.php on line 135
But I'm able to use the customer singleton to retrieve client data like this:
$cliente = Mage::getSingleton('customer/session')->getCustomer();
So on this file I'm able to access others methods, but I'm unable to get the following details:
Number of downloads bought
Number of downloads used
Order ID or Link hash ID.
So please, I'm requesting how to get the current order instance and / or the current link instance, on the Download.php related with the file downloading.
Thank you!
You are doing it wrong, you can not get any object like this $this->getOrderItem() without set/declaring them. As you are trying to get order_id and item_id in the helper, which you are using on a custom page redirected from customer downloadable products. Here is what you have to do
Step - 1
From customer My Downloadable products, in the redirect url you have to pass that products order_id and item_id as parameter. You can get them from below code
$orderId = $_item->getPurchased()->getOrderId();
$itemId = $_item->getId();
Step -2
Now in your template file while using your helper function pass this order_id and item_id to method parameter. Like below code
$orderId = $this->getRequest()->getParam('order_param');
$itemId = $this->getRequest()->getParam('item_param');
//Your helper function
Mage::helper('your_helper')->yourMethod($orderId, $itemId);
Step - 3
In your helper file you can use below code
public function yourMethod($orderId, $itemId)
{
$linkPurchasedItem = Mage::getModel('downloadable/link_purchased_item')
->load($orderItem, 'order_id');
$LinkPurchaseOrderItemId = Mage::getModel('downloadable/link_purchased_item')->getCollection()
->addFieldToFilter('order_item_id', $itemId);
}
Note: Do not make any changes in core files, instead override them

Wordpress custom query

I have a wordpress blog. I created a db table which stores dictionary information and I want to publish this data from a URL . (For ex: "myblogaddress.com/mytest.php")
I have been researching for 2 days but nothing works I tried.
In my page; I use the php code shown in blow.
<?php
global $wpdb;
$words = $wpdb->get_results("SELECT * FROM $wpdb->words")
echo $words[0]->ENG;
?>
I wonder that;
- Which directory does my php page to be into ?
- What I need to do (other config, permission etc.) to do what I want.
Regards.
If you're loading it from a standalone PHP file (ie not from within your WordPress theme), you'll have to call wp-load.php to initialise the WordPress variables (including $wpdb). Have a look at this answer, including the comment about only needing wp-load.php.
I'd consider using a relative path (what that would be would depend on where you put your page relative to WordPress) rather than using $_SERVER['DOCUMENT_ROOT'];, but that's just a personal preference.
EDIT
Rereading after seeing your comment, I've just realised $wpdb->words probably won't exist. Try
$words = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix . "words")
instead. That'll generate the table name correctly as wp_words. Of course, you'll need to populate it the same way.

EGL Group Permissions Add/Manage

For those of you that have dealt with EGL (Elite Gaming Ladders) Tournament & Ladders script... I'm having a problem figuring out how to add new group permissions for customized mods I've made. I've created a new type of "General Rules" mod that I would like to store the permission as shown:
case "select_genrules";
if($group['genrules_manager']=="yes"){
include("./includes/rules.php");
select_genrules();
}else{
return warning("<b>" . LANG_WARNING_NO_PERMISSIONS_TO_ACCESS_PAGE . " </b>");
}
break;
where "genrules_manager" formatted the same as all of the others is added to the group_permissions set. I added it to the groups_permissions table and it actually does show in the list of permissions in the ACP under the category "Misc" but I would like to add "Lang" so that it doesn't just say "genrules_manager" etc... Any help as to how to access/modify those keys in the admincp/includes/addmembergroup.php and admincp/includes/managemembergroup.php would be greatly appreciated!
This is actually found in groups.php under the functions addmembergroup and editmembergroup. You will need to add a key that links to the "lang" you have written in admincp/language/english.php like so:
if($key=='genrules_manager'){$descr='' . LANG_GRO_PERMISSIONS_DESCR_GENRULES_MANAGER . ''; $site .= group_radio($key, $value, $descr);}
It has to be done in both functions in order for you to see when adding and editing. Simply place it inline with the keys under whichever category you'd like...

how to make queries to db in custom script using zen cart?

hi i have a custom script that I call with ajax to retrieve some db info but for some reason it will not allow me to make the calls from this file. yet when i put the code in a page in the templates diretory lets say tpl_products_all_default.php they run fine. what do i need to do to be able to run queries from a custom script?
$sql = "select products_model from products where products_model = :productMdel:";
$sql = $db->bindVars($sql, ':productMdel:', 'C021', 'string');
$result = $db->Execute($sql);
if ($result->RecordCount() > 0) {
echo 'Model number = ' . $result->fields['products_model'];
} else {
echo 'Sorry, no record found for product number ' . $theProductId;
}
I may have an answer for you, though I'll admit it's not an optimal setup as it requires your custom file to be placed into the root directory.
If your custom file is being placed in the root (/your_custom_file.php) you can do the following to get access to the $db with the following require statement:
require('includes/application_top.php');
This will initialize all of the globals, and also call the includes/initsystem.php, which will spin through the values in the autoloader and include each script. The auto_loader can be viewed at includes/auto_loaders/config.core.php. In v1.5, you can see it finally includes the init_database.php script on lines 81-82. The init_database.php file finally initializes $db.
I initially ran into the same issue you had, and almost missed this setup because I had originally added my custom files to a custom directory like /my_module_extensions/my_file.php which failed. It seems the application_top.php loads everything with relative paths, so when executing under a directory other than the root, it would fail.
I hope this helps!
EDIT: Originally thought you were talking about an admin customization. I reworded this to relate to the public side. This also works from the admin side, if you need to extend the admin console.
Not true you can use an include from anywhere.
such as:
require_once('inc/php/application_top.php');
or
require_once('http://example.com/inc/php/application_top.php');

Categories