I'm a beginner in Prestashop, I'd like to modify my website. I want to delete automatically a product if the promotion end is outdated.
For the special offers (promotions), the end date is written with $product.specific_prices.to.
I want to delete the article where the date of this variable is equal to the current date. But I don't know how to do it... In the templates? For example :
{if ($smarty.now|date_format:'%Y-%m-%d %H:%M:%S' <= $product.specific_prices.to) && ($product.specific_prices.to != '0000-00-00 00:00:00')}
With a trigger in DB?
CREATE EVENT IF NOT EXISTS `Clean_End_Promotion`
DELETE FROM products
WHERE promo_date == NOW() //but I don't which table I can use
With PHP files? I don't know which way is the best.
Thanks!
What about this? Create a new custom PHP file (include init;config.php files to access database or write connection).
$today = date("Y-m-d H:i:s");
$sql = 'SELECT id_product FROM `'._DB_PREFIX_.'specific_price` WHERE to >= '.$today;
$result = Db::getInstance()->execute($sql);
Foreach ($result as $res) {
$prod = new Product($res);
$prod->delete();
}
I dont know I haven`t test it and I have never used delete function but check product class for more info. Product->delete should remove all data from all tables.
Related
I want to replace certain letters inside the product description of every product in my magento shop. There is a way to run a script via phpMyAdmin (SQL). There is a REPLACE function for that:
REPLACE( string1, string_to_replace, replacement_string )
But how would I do the rest of the code? How do I get access to the cells with the description in it?
Thanks in advance!
EDIT: More Detail:
I want to make a SQL Statement that loops through every product and change parts of the product description with the help of the REPLACE function.
You should avoid doing direct SQL on your Magento instance. If you want to update the product descriptions, you can use the catalog model for this - or even the export/import routine. Here's how to do it programmatically - create the below and put it in the root directory of your Magento website.
include_once "app/Mage.php";
Mage::init();
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$yourstring = 'The string you want to find';
$newstring = 'The string you want to replace it with';
$_productCollection = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->load();
foreach($_productCollection as $_product) {
if (strpos($_product->getDescription(),$yourstring) !== false) {
$newdescription = str_replace($yourstring,$newstring,$_product->getDescription());
$_product->setDescription($newdescription);
$_product->save();
echo 'Updated product: '.$_product->getName().'<br />';
}
}
I have an old oxid-version. I exported my old seo-data from the table "oxseo" to get the keywords and description for each article. Now i want to import these fields in my new version of the shop. My articles are already there, but not the seodata.
My first idea was to collect all the data i need from a csv-export of my old data.
For example, my output array could look something like:
$article = array();
$keywords = array();
$desc = array();
foreach($line as $l) {
$keywords[$i] = current_keyword
$desc[$i] = current_description
$oxid[$i] = current_oxid
}
So lets just assume I already have my filled array.
If i check the oxid's, they are still the same. So, from my exported CSV, picking a random OXID, looking for it in my new DB shows me the correct article.
Now my first thought was, to look in oxobject2seodata. I know that the data for the articles are stored in there, but i can't find a way to connect those, since the "oxid" from the old version is not the same as the objectId in the new version. In oxarticles, however, there is no "objectId".
Thank you in advance for any hints and tips
The field OXID in oxarticles table should match the field OXOBJECTID in oxobject2seodata table.
SELECT oa.OXID, o2s.* from oxobject2seodata o2s, oxarticles oa WHERE o2s.OXOBJECTID = oa.OXID AND oa.OXID = '[OXID-of-article]';
-- or
SELECT o2s.* from oxobject2seodata o2s WHERE o2s.OXOBJECTID = '[OXID-of-article]';
is there a way to batch update products or orders using bigcommerce api?
I need to update multiple products with just one call if possible.
If your using the PHP library which can be downloaded # https://github.com/bigcommerce/auspost-api-php
I first made a local database of products using the following calls...
$filter = array('limit' =>, 'page' =>1);
Bigcommerce::getProducts($filter);
foreach($products as $product){
$sku = $product->SKU;
$id = $product->id;
/* write some code in the loop to add all the products to your DB */
}
Once you have a DB of all your products you can make SQL call with SELECT ALL from your Database and update them with some static content like below(put below in a loop for each product)....
$UpdateQuery = array('description' => 'Some Random Text added to all descriptions')
Bigcommerce::updateProduct($Bicommerce_Product_ID,$UpdateQuery)
This would bulk add 'Some Random Text added to all descriptions' too products in your database. You need to use the Bigcommerce product ID in each loop to target it and update in 'bulk'.
Hope this is useful - can verify it works as use it myself :)
I am using the following code to load a categorys record:
$res = JTable::getInstance('category');
$res->load(array('id' => $catid));
Now I would like to load the record based on its title which whould be matched against a SQL LIKE-pattern - is it possible to do this in a simple way with JTable, or do I need $dbo?
Far as I know JTable is made to be simple and carry only one element at a time, and through the primary key. If you really want something more advanced, I recomend that you use JDatabaseQuery way.
// Get a db connection.
$db = JFactory::getDbo();
// Create a new query object.
$query = $db->getQuery(true);
// Select all articles for users who have a username which starts with 'a'.
// Order it by the created date.
$query
->select(array('a.*', 'b.username', 'b.name'))
->from('#__content AS a')
->join('INNER', '#__users AS b ON (a.created_by = b.id)')
->where('b.username LIKE \'a%\'')
->order('a.created DESC');
// Reset the query using our newly populated query object.
$db->setQuery($query);
// Load the results as a list of stdClass objects.
$results = $db->loadObjectList();
In your case, instead of "$db->loadObjectList();" you can use "$db->loadObject();" for load just one item.
Source:
http://docs.joomla.org/Accessing_the_database_using_JDatabase/3.1
I wrote a Perl script to check the data in an Oracle database. Because the query process is very complex I chose to create a VIEW in the middle. Using this view the code could be largely simplified.
The Perl code run well when I used it to query the database starting from a file, like Perl mycode.pl file_a. The Perl code reads lines from file_a and creates/updates the view until the end of the input. The results I achieved are completely right.
The problem came when I simultaneously run
perl mycode.pl file_a
and
perl mycode.pl file_b
to access the same database. According to my observation, the VIEW used by the first process will be modified by the second process. These two processes were intertwined on the same view.
Is there any suggestion to make these two processes not conflict with one another?
The Perl code for querying database is normally like this, but the details in each real query is more complex.
my ($gcsta,$gcsto,$cms) = #t; #(details of #t is read from a line in file a or b)
my $VIEWSS = 'CREATE OR REPLACE VIEW VIEWSS AS SELECT ID,GSTA,GSTO,GWTA FROM TABLEA WHERE GSTA='.$gcsta.' AND GSTO='.$gcsto.' AND CMS='.$cms;
my $querying = q{ SELECT COUNT(*) FROM VIEWSS WHERE VIEWSS.ID=1};
my $inner_sth = $dbh->prepare($VIEWSS);
my $inner_rv = $inner_sth->execute();
$inner_sth = $dbh->prepare($querying);
$inner_rv = $inner_sth->execute();
You must
Create the view only once, and use it everywhere
Use placeholders in your SQL statements, and pass the actual parameters with the call to execute
Is this the full extent of your SQL? Probably not, but if so it really is fairly simple.
Take a look at this refactoring for some ideas. Note that is uses a here document to express the SQL. The END_SQL marker for the end of the text must have no whitespace before or after it.
If your requirement is more complex than this then please describe it to us so that we can better help you
my $stmt = $dbh->prepare(<<'END_SQL');
SELECT count(*)
FROM tablea
WHERE gsta = ? AND gsto = ? AND cms= ? AND id = 1
END_SQL
my $rv = $stmt->execute($gcsta, $gcsto, $cms);
If you must use a view then you should use placeholders in the CREATE VIEW as before, and make every set of changes into a transaction so that other processes can't interfere. This involves disabling AutoCommit when you create the database handle $dbh and adding a call to $dbh->commit when all the steps are complete
use strict;
use warnings;
use DBI;
my $dbh = DBI->connect('dbi:Oracle:mydbase', 'user', 'pass',
{ AutoCommit => 0, RaiseError => 1 } );
my $make_view = $dbh->prepare(<<'END_SQL');
CREATE OR REPLACE VIEW viewss AS
SELECT id, gsta, gsto, gwta
FROM tablea
WHERE gsta = ? AND gsto = ? AND cms= ? AND id = 1
END_SQL
my $get_count = $dbh->prepare(<<'END_SQL');
SELECT count(*)
FROM viewss
WHERE id = 1
END_SQL
while (<>) {
my ($gcsta, $gcsto, $cms) = split;
my $rv = $make_view->execute($gcsta, $gcsto, $cms);
$rv = $get_count->execute;
my ($count) = $get_count->fetchrow_array;
$dbh->commit;
}
Is the view going to be the same or different?
If the views are all the same then create it only once, or check if it exists with the all_views table : http://docs.oracle.com/cd/B12037_01/server.101/b10755/statviews_1202.htm#i1593583
You can easily create a view including your pid with the $$ variable to be the pid, but it wont be unique across computers, oracle has also some unique ids, see http://docs.oracle.com/cd/B14117_01/server.101/b10759/functions150.htm, for example, the SESSIONID.
But do you really need to do this? why dont you prepare a statement and then execute it? http://search.cpan.org/dist/DBI/DBI.pm#prepare
thanks,
mike