How to make an Abandoned Cart in PrestaShop - prestashop

We just run into an issue and we don't find the answer for the question.
We'd like to test a function that is related to the abandoned cart status, but we don’t know how to abandon a cart, even though we put some items on the cart but it stays non ordered, and if we log out it still doesn’t update the status.
We kinda lost, and we don’t find the answer to this question. (How to abandon a cart is prestashop)

Just older carts not converted to order appear like abandoned.
By DB, substract 1 year to your desired cart, like this:
UPDATE ps_cart
SET
date_add = date_add(date_add, INTERVAL -1 YEAR),
date_upd = date_add(date_upd, INTERVAL -1 YEAR)
WHERE id_cart = your_id_cart;

As you may have seen from Prestashop core this is one of the functions used by it to get abandoned carts.
public static function getAbandonedCarts($date_from, $date_to)
{
return Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(
'
SELECT COUNT(DISTINCT id_guest)
FROM `' . _DB_PREFIX_ . 'cart`
WHERE `date_add` BETWEEN "' . pSQL($date_from) . '" AND "' . pSQL($date_to) . '"
AND NOT EXISTS (SELECT 1 FROM `' . _DB_PREFIX_ . 'orders` WHERE `' . _DB_PREFIX_ . 'orders`.id_cart = `' . _DB_PREFIX_ . 'cart`.id_cart)
' . Shop::addSqlRestriction()
);
}
It does perform a check inside the table "cart" about the date obviously. But, more importantly, it does check if there is a record of an order related to that entity cart. If a record in table "orders" does not exist it means we have an "abandoned cart".
You can find the above function inside the class "AdminStatsControllerCore" which is, most likely, the logic to get "abandoned carts" inside the back office (Orders -> Shopping Carts).
Hopefully, I made it more clear to you how Prestashop handles abandoned carts.

Related

How to get session ID in sql in joomla

I'm working with vcharts in Joomla and trying tom implement charts based on who is logged in. Sadly vcharts only allows me to use SQL for queries and not PHP so How will I get the session ID then.
Based on the SO question Variables added to vchart joomla sql query not picked up (that exposes a relevant portion of the vcharts manual which joomla users must ordinarily pay to see...) it seems the placeholder that you can use in your SQL is:
{loggedinuserid}
So, you might write something like
SELECT * FROM #__users WHERE id = {loggedinuserid}
Of course this is just a guess because I've never used vcharts, nor have I seen the actual docs.
If it doesn't like the #__ prefix placeholder, then I guess you'll need to manually replace it like this:
SELECT * FROM lmnop_users WHERE id = {loggedinuserid}
All this said, I have had some experience with the Plotalot extension and it's pretty good with great documentation.
I find this approach: with joomla api
<?php
$link = mysqli_connect("localhost", "joomla_usr", "geheim", "joomla_db");
define('_JEXEC', 1);
define('JPATH_BASE', dirname(__FILE__));
define('DS', DIRECTORY_SEPARATOR);
require_once(JPATH_BASE . DS . 'includes' . DS . 'defines.php');
require_once(JPATH_BASE . DS . 'includes' . DS . 'framework.php');
require('libraries/joomla/factory.php');
$mainframe = JFactory::getApplication('site');
$mainframe->initialise();
$session = JFactory::getSession();
$query_user = "SELECT name, username, email FROM joomla_users WHERE id=(SELECT userid FROM joomla_session WHERE session_id='" . $session->getId() . "')";
$result_user = mysqli_query($link, $query_user);
$row_user = mysqli_fetch_array($result_user);
?>
Maybe you can use the query direct to access the mysql database from your Platform. Or embed PHP in any way.

How to build a $wpdb query

I am trying to build a query that will help me rank SearchWP search results by total_sales (in a WooCommerce shop).
SearchWP has a simple filter that helps injecting additional sql into the search algorithm and adding weight as desired.
The following example lets recent posts rank higher:
$sql .= " + ( IF( UNIX_TIMESTAMP( {$wpdb->prefix}posts.post_date ) > UNIX_TIMESTAMP( {$time_ago} ), {$additional_weight}, 0 ) )";
So I tried to do the same thing by using the total_sales column from the postmeta table to increase the weight of search results, but it doesn't work.
$sql .= " + {$wpdb->prefix}postmeta.total_sales";
Obviously I wonder what the solution is.
But I also want to know a way how to build and test such a $wpdb query successfully myself. A link to a good tutorial would be appreciated.
I found the solution by following this SearchWP documentation. It is another filter provided by the SearchWP plugin: https://searchwp.com/docs/kb/using-custom-field-prioritize-search-results/
Simply drop both filters into functions.php and change the line
$my_meta_key = 'my_search_priority';
to
$my_meta_key = 'total_sales';
It works like a charm

Magento Bulk update attributes

I am missing the SQL out of this to Bulk update attributes by SKU/UPC.
Running EE1.10 FYI
I have all the rest of the code working but I"m not sure the who/what/why of
actually updating our attributes, and haven't been able to find them, my logic
is
Open a CSV and grab all skus and associated attrib into a 2d array
Parse the SKU into an entity_id
Take the entity_id and the attribute and run updates until finished
Take the rest of the day of since its Friday
Here's my (almost finished) code, I would GREATLY appreciate some help.
/**
* FUNCTION: updateAttrib
*
* REQS: $db_magento
* Session resource
*
* REQS: entity_id
* Product entity value
*
* REQS: $attrib
* Attribute to alter
*
*/
See my response for working production code. Hope this helps someone in the Magento community.
While this may technically work, the code you have written is just about the last way you should do this.
In Magento, you really should be using the models provided by the code and not write database queries on your own.
In your case, if you need to update attributes for 1 or many products, there is a way for you to do that very quickly (and pretty safely).
If you look in: /app/code/core/Mage/Adminhtml/controllers/Catalog/Product/Action/AttributeController.php you will find that this controller is dedicated to updating multiple products quickly.
If you look in the saveAction() function you will find the following line of code:
Mage::getSingleton('catalog/product_action')
->updateAttributes($this->_getHelper()->getProductIds(), $attributesData, $storeId);
This code is responsible for updating all the product IDs you want, only the changed attributes for any single store at a time.
The first parameter is basically an array of Product IDs. If you only want to update a single product, just put it in an array.
The second parameter is an array that contains the attributes you want to update for the given products. For example if you wanted to update price to $10 and weight to 5, you would pass the following array:
array('price' => 10.00, 'weight' => 5)
Then finally, the third and final attribute is the store ID you want these updates to happen to. Most likely this number will either be 1 or 0.
I would play around with this function call and use this instead of writing and maintaining your own database queries.
General Update Query will be like:
UPDATE
catalog_product_entity_[backend_type] cpex
SET
cpex.value = ?
WHERE cpex.attribute_id = ?
AND cpex.entity_id = ?
In order to find the [backend_type] associated with the attribute:
SELECT
  backend_type
FROM
  eav_attribute
WHERE entity_type_id =
  (SELECT
    entity_type_id
  FROM
    eav_entity_type
  WHERE entity_type_code = 'catalog_product')
AND attribute_id = ?
You can get more info from the following blog article:
http://www.blog.magepsycho.com/magento-eav-structure-role-of-eav_attributes-backend_type-field/
Hope this helps you.

Increment user postcount upon posting topic/reply

I'm really new to SQL and looked around the web for help on incrementing values but couldn't find something that works for me. What I'm trying to do is increment the value of user_posts when a reply/topic is posted. The user_posts is in users (forums>users>user_posts). I don't know how to get the specific user and then add to the value of his/her posts. I'm working off of a very small and basic example, which is how I have posts working. I was thinking I could add it to the SQL where the topic is sent to the DB but, like I said, can't figure out how. I don't really have any code to provide to start with because I really don't know where to start. I believe it uses UPDATE, but that's all I can guess. If you aren't quite sure what it is I want even after reading all of this, ask and I'll try to clarify.
Here's what it looks like with part of the working post code and a suggested code that doesn't seem to work.
VALUES ('" . $_POST['reply-content'] . "',
NOW(),
" . mysql_real_escape_string($_GET['id']) . ",
" . $_SESSION['user_id'] . ");
UPDATE users SET user_posts=1 WHERE user_id=1";
You can write
UPDATE Users
SET PostCount = PostCount + 1
WHERE UserId = #id
Something like: select postcount from users where user_id = <userid>; followed by update users set postcount=<new_postcount> where user_id = <userid>;. Some details: http://www.w3schools.com/sql/sql_update.asp
I wouldn't worry about optimizing your queries to your database until you experience problems handling your load. :)

Adding more information to Magento packingslip or Invoice PDF

How can i add additional information to the Magento Packingslip PDF. I am using integrated label paper, so i would like to add repeat the customers delivery address at the footer and also, some details like total quantity of items in the order and the cost of the items in the order. I am currently modifying local files in: Mage/Sales/Model/Order/Pdf/ but I have only managed to change to font.
EDIT:
Okay, i have made good progress and added most of the information i need, however, i have now stumbled across a problem.. I would like to get the total weight of all items in each order and Quantity.
I am using this code in the shipment.php:
Under the foreach (This is useful in case an order has a split delivery - as you can have one order with multiple "shipments" So this is why I have the code after here:
foreach ($shipment->getAllItems() as $item){
if ($item->getOrderItem()->getParentItem()) {
continue;
...
then I have this further down:
$shippingweight=0;
$shippingweight= $item->getWeight()*$item->getQty();
$page->drawText($shippingweight . Mage::helper('sales'), $x, $y, 'UTF-8');
This is great for Row totals. But not for the whole shipment. What I need to do is have this bit of code "added up" for each item in the shipment to create the total Weight of the whole shipment. It is very important that I only have the total of the shipment - not the order as I will be splitting shipments.
Almost at the end of getPdf(...) in Mage_Sales_Model_Order_Pdf_Shipment you will find the code that inserts new pages (lines 93-94 in 1.5.0.1)
if($this->y<15)
$page = $this->newPage(....)
happening in a for-loop.
You can change the logic here to make it change page earlier to make room for your extra information and then add it before the page shift. You should also place code after the for-block if you want it to appear on the last page as well.
Note: You should not change files in app/code/code/Mage directly. Instead, you should place your changed files under app/code/local/Mage using the same folder structure. That way your changes won't accidently get overwritten in an upgrade.
This should get you the total number of items in your order (there might be a faster way but don't know it off the top of my head):
$quote = Mage::getModel('sales/quote')->load($order->getQuoteId());
$itemsCount = $quote->getItemsSummaryQty();
For invoice PDF
at app/code/local/Mage/Sales/Model/Order/Pdf/Items/Invoice/Default.php
add this before $lineBlock
$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $this->getSku($item), array('weight'));
$lines[][] = array(
'text' => 'Weight: '. $product->getData('weight')*1 .'Kg/ea. Total: ' .$product->getData('weight')*$item->getQty() . 'Kg',
'feed' => 400
);