How to change cart for users in prestashop? - prestashop

I need to create a link that change's a user's cart with one that is expired.
this is what i tried, but it doesn't work
<?php
if (!defined('_PS_ADMIN_DIR_'))
define('_PS_ADMIN_DIR_', (getcwd().'/../../') );//prima del require_once
require_once(dirname(__FILE__).'/../../config/config.inc.php');
...
$this->context->cookie->id_cart = 6;
$this->context->cart = new Cart(6);
$this->context->cookie->write();
$this->context->cookie->update();
Tools::redirect('index.php?controller=order');

what about : $this->context->cart->id = 6 ?

Related

Pagination with PDO prepare statement

I am using David Carr's pagination class successfully for a whole-table call (i.e. where I'm calling all the rows in the table without filtering). After including my config and the class, I accomplish it this way:
$pages = new Paginator('3','p');
$stmt = $dbh->query('SELECT count(id) FROM sermons');
$row = $stmt->fetch(PDO::FETCH_NUM);
$total = $row[0];
//pass number of records to
$pages->set_total($total);
$results = $dbh->query('SELECT * FROM sermons ORDER BY date_preached DESC '.$pages->get_limit()); ?>
<div class="paging"><p><?php echo $total; ?> sermons found</p> <?php echo $pages->page_links();?></div>
<?php
foreach($results as $row)
Etc.
That works great, but now I'm trying to implement it for the result of user filtering via forms, and because I can't use an unnamed placeholder in the query, I'm trying to do it with a prepare (which I'm guessing is more secure anyway):
$pages = new Paginator('2','p');
$series = isset($_POST['series']) ? $_POST['series'] : false;
try {
$stmt = $dbh->prepare("SELECT count(id) FROM sermons WHERE series = ?");
$stmt->execute(array($series));
$row = $stmt->fetch(PDO::FETCH_NUM);
$total = $row[0];
//pass number of records to
$pages->set_total($total);
$results = $dbh->prepare("SELECT * FROM sermons WHERE series = ? ".$pages->get_limit());
$results->bindParam(1, $series, PDO::PARAM_STR);
$results->execute(array($series));
// the call
echo $total; ?> sermons found</p> <?php echo $pages->page_links();
When I first get to the results page from the form, it looks like everything is working properly. The call correctly identifies the number of sermons found for the search result, and displays the first two (or however many I set in the $pages statement). But when I click to another page result, no sermons display, the count is gone, and my !isset(found_rows) echo returns a zero as well.
I was originally thinking the issue was that at first I was trying to do this without binding. That is questionable, since without the pagination I can display the page without binding. In any case, I've been trying that with both bindValue and bindParam, but with no luck.
Am I mishandling something in the prepare statement? Or what?
Note: I have the results page displaying fine without the pagination with this:
$series = isset($_POST['series']) ? $_POST['series'] : false;
try {
$results = $dbh->prepare("SELECT * FROM sermons WHERE series = ?");
$results->execute(array($series));
// Etc.

How to add Product in Cart in Prestashop 1.6.7

I am trying to add product in Cart in Prestashop 1.6.7.
But it doesn't working.
Below is my code:
$context=Context::getContext();//new Cart();
$id_cart=$context->cookie->__get('id_cart');
$lan_id = $this->context->language->id;
$cur_id = $this->context->currency->id;
$products_ids=16571;
$cart=new Cart($id_cart);
$cart->id_currency=$cur_id;
$cart->id_lang=$lan_id;
$cart->updateQty(1,$products_ids, null, false);
But above code is not working.
Somehow i manage my code as per below:
global $context;
$context=Context::getContext();//new Cart();
$id_cart=$context->cookie->__get('id_cart');
$lan_id = $context->cookie->__get('id_lang');
$cur_id = $context->cookie->__get('id_currency');
$products_ids=16571;
$cart=new Cart($id_cart);
$cart->id_currency=$cur_id;
$cart->id_lang=$lan_id;
$this->context->cart->update();
$cart->updateQty(1,16571);
But there is one error for carrier
There are no carriers available that deliver to this address.
Try adding the following code at the end of your code.
$this->context->cart->save();
$this->context->cookie->id_cart = (int)$this->context->cart->id;
$this->context->cookie->write();
$this->context->cart->autosetProductAddress();
Hook::exec('actionAuthentication');
CartRule::autoRemoveFromCart($this->context);
CartRule::autoAddToCart($this->context);
I add some static Carrier ID in
classes/Cart.php
in getPackageList function at line no. approx. 1892
if (empty($product['carrier_list'])) {
$product['carrier_list'] = array(
'0' => 10,
'1' => 11
);
}

Shopify API getting order by name or order_number

Im using a plugin for CakePHP to make the calls to obtain certain orders. I can call all orders with certain fields, but I was wondering how would I have to make the call to get the orders with a certain name or order_number? Here is the source for the call to Shopify. Its already authenticated and everything:
public function call($method, $path, $params=array())
{
if (!$this->isAuthorized())
return;
$password = $this->is_private_app ? $this->secret : md5($this->secret.$this->ShopifyAuth->token);
$baseurl = "https://{$this->api_key}:$password#{$this->ShopifyAuth->shop_domain}/";
$url = $baseurl.ltrim($path, '/');
$query = in_array($method, array('GET','DELETE')) ? $params : array();
$payload = in_array($method, array('POST','PUT')) ? stripslashes(json_encode($params)) : array();
$request_headers = in_array($method, array('POST','PUT')) ? array("Content-Type: application/json; charset=utf-8", 'Expect:') : array();
$request_headers[] = 'X-Shopify-Access-Token: ' . $this->ShopifyAuth->token;
list($response_body, $response_headers) = $this->Curl->HttpRequest($method, $url, $query, $payload, $request_headers);
$this->last_response_headers = $response_headers;
$response = json_decode($response_body, true);
if (isset($response['errors']) or ($this->last_response_headers['http_status_code'] >= 400))
throw new ShopifyApiException($method, $path, $params, $this->last_response_headers, $response);
return (is_array($response) and (count($response) > 0)) ? array_shift($response) : $response;
}
private function shopApiCallLimitParam($index)
{
if ($this->last_response_headers == null)
{
return 0;
}
$params = explode('/', $this->last_response_headers['http_x_shopify_shop_api_call_limit']);
return (int) $params[$index];
}
...and the code that makes the GET call:
// I only want the id and title of the collections
$fields = "fields=name,id,status,financial_status,fulfillment_status,billing_address,customer";
// get list of collections
$custom_collections = $this->ShopifyAPI->call('GET', "/admin/orders.json", $fields);
$this->set('collections', $custom_collections);
I think I'm missing the place where I can put the conditions for the call to get certain orders. I've already read the API documentation but can't seem to get the answer.
I've tried putting the ?name=%231001 on the url after .json to try and get the order #1001, but it brings back a empty array.
Then I tried ?order_number=1001 but it brings me every order with as well 1001 D: This is really confusing, Could anyone help me?
Thanks in advance.
Well I found out that you can actually get the order using the name or order_number. Its another property that is not listed on the documentation for some reason. But in the URL, if your using another language, all you have to add in the GET is admin/order.json?name=%2310001&status=any this is to get the order 10001 so just add the order_number after the %23. I saw this on a forum in Shopify university, I was just implementing this wrong on my code. If your using the CakePhp shopify plugin like me all I did was add on the $field the ?name=%23". number ."&status=any";
Ill leave the code here:
$this->layout = 'main';
$order_number = "18253";
$fields = "name=%23". $order_number ."&status=any";
$order = $this->ShopifyAPI->call('GET', "/admin/orders.json", $fields);
if (!empty($order)) {
$this->set('order', $order);
} else {
$this->Session->setFlash('<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button> No existe el numero de orden ingresado.','default',array('class' => 'alert alert-danger alert-dismissible', 'type' => 'alert'));
}
Hope this helps someone :P

How to get all products of a Shopify shop?

In my theme development, I don't find the way to get all the products of my shop.
Although, I can retrieve all the collections with the variable collections (exemple: {% for c in collections %}).
Check this url: https://help.shopify.com/en/themes/customization/collections/change-catalog-page
Like magic... all your products...
Get all products at once or to run a query(API Request) for all products in shopify store :
using this app is more managed -> https://github.com/phpish/shopify_private_app-skeleton so, my solution below is based on this app or you can relate the solution with your solution as well
<?php
session_start();
require __DIR__.'/vendor/autoload.php';
use phpish\shopify;
require __DIR__.'/conf.php';
$shopify = shopify\client(SHOPIFY_SHOP, SHOPIFY_APP_API_KEY, SHOPIFY_APP_PASSWORD, true);
try
{
$products = $shopify('GET /admin/products/count.json', array('published_status'=>'published'));
$totalproducts = $shopify('GET /admin/products/count.json', array('published_status'=>'published'));
$limit = 50;
$totalpage = ceil($totalproducts/$limit);
for($i=1; $i<=$totalpage; $i++){
$products = $shopify('GET /admin/products.json?'.$limit.'=50&page='.$i, array('published_status'=>'published'));
foreach($products as $product){
//do anything at once for all the products in store
}
}
}
catch (shopify\ApiException $e)
{
//
}
Summary : The idea is to retrieve with page=x as parameter. after calculating the number of pages we will have with specified limit i.e 50 at one time fetch.

Issue with retrieving Magento Frontend Session

I am trying to retrieve the customer's login status from Flex application using AMF call to the Magento Customer API :
Mage::app('default');
$session = Mage::getSingleton('customer/session', array('name'=>'frontend') );
$sessId= $session->getSessionId();
if($session->isLoggedIn()) {
$name = "Hi ". Mage::getModel('customer/session')->getCustomer()->getName();
return 'true' . $name;
}
else{
return 'false ' . $sessId;
}
Only the PHP session ID is returned:
PHPSESSID=i5s1gcemc6r8uquadc4rsk9ou5
But the user is logged into the below ID
frontend=3qdcimcdp7nq4bi8jlovqmnq61
Let me know if I am missing something here.
Use the following code to get the customer ID
Mage::getSingleton('core/session', array('name' => 'frontend'));
$customer = Mage::getSingleton('customer/session',array('name' => 'frontend'));
echo $customerId = $customer->getCustomer()->getId();