Woocommerce order name value - php

For a purpose im passing woocommerce values to an array like this :
'name' => $_POST['billing_first_name'],
'phone' => $_POST['billing_phone'],
'email' => $_POST['billing_email'],
'note' => $_POST['order_comments'],
Now I'm stuck finding one of the values. The name of the ordered product.
'subject' => HERE GOES THE PRODUCT NAME VALUE,
i have been looking everywhere, tried some stuff but nothing worked so far.
I'm working in functions.php

Retrieved the name like this
// Retrieve order name
global $woocommerce;
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) {
$_product = $values['data']->post;
}
//
and then i called it like this
'subject' => $_product->post_title,

Related

creating session array in my shopping cart not functioning

hello I am trying to create a simple shopping cart for my school project the problem I have was the session array is not creating and array with information. So I can't add items on my cart. here is my code.
if(isset($_POST['add_to_cart'])) {
if(isset($_SESSION['cart'])) {
$session_array_id = array_column($_SESSION['cart'], 'id');
if(!in_array($_GET['id'], $session_array_id)) {
$session_array = array(
'id' => $_GET['id'],
'name' => $_POST['name'],
'price' => $_POST['price'],
'quantity' => $_POST['quantity']
);
$_SESSION['cart'][0]= $session_array;
}
}else {
$session_array = array(
'id' => $_GET['id'],
'name' => $_POST['name'],
'price' => $_POST['price'],
'quantity' => $_POST['quantity']
);
$_SESSION['cart'][] = $session_array;
}
the picture above is just overwriting my selection everytime I press add to cart it won't stack up new item on top of the other. so basically the session array stays 0 all the time.
Pls somebody help me figure this out thank you so much!
When you do $_SESSION['cart'][0]= $session_array; you set it always on index 0. Try
array_push($_SESSION['cart'], $session_array);
Also this is not sticking $_SESSION['cart'][] = $session_array;
Always trust in array push.

How to sum all the current product prices of a user cart together as total price

I'm working on an Online Store project using PHP and MySQLi. Most of this project is done and now I'm struggling with total price that a user must pay. This total price is showing in the cart.php page (where users can see all the product items that they have added earlier). So it must be set to the total price of all current items.
So here is the code of cart.php:
if(isset($_GET['cart_id']))
{
$cart_id = $_GET['cart_id'];
$get_add = "SELECT * FROM cart WHERE cart_id = '$cart_id'";
$run_add = mysqli_query($con,$get_add);
$cart_items = [];
while ($row_results = mysqli_fetch_array($run_add)){
$item = array(
'table_id' => $row_results['table_id'],
'cart_id' => $row_results['cart_id'],
'pro_id' => $row_results['product_id'],
'pro_title' => $row_results['product_title'],
'pro_price' => $row_results['product_price'],
'pro_img' => $row_results['product_image'],
'pro_supplier' => $row_results['product_supplier'],
'qty' => $row_results['qty'],
'cart_ip' => $row_results['cart_ip'],
);
$cart_items[] = $item;
}
foreach ($cart_items as $cart) {
echo $cart['pro_price']
}
}
The table cart structure goes like this:
see image here
So now I want to print the sum of all products as total_price, till now I tried several different ways but I could not get the result.
So if you know how to solve this question, please let me know.. I really really appreciate that...
Thanks in advance!
Assuming you need the sum of qty*product_price you could sum this way
$tot = 0;
while ($row_results = mysqli_fetch_array($run_add)){
$cart_items[] = array(
'table_id' => $row_results['table_id'],
'cart_id' => $row_results['cart_id'],
'pro_id' => $row_results['product_id'],
'pro_title' => $row_results['product_title'],
'pro_price' => $row_results['product_price'],
'pro_img' => $row_results['product_image'],
'pro_supplier' => $row_results['product_supplier'],
'qty' => $row_results['qty'],
'cart_ip' => $row_results['cart_ip'],
);
$tot += $row_results['qty']*$row_results['product_price'];
}
foreach ($cart_items as $cart) {
echo $cart['pro_price'];
}
echo $tot;

Prestashop filter products by category using WebService

I'm trying to use Prestashop WebService via PHP to filter products by categories but it seems that it's impossible.
How to make it? Must be something like this
array(‘resource’ =>’products’, ‘display’ => ‘[name]’, ‘filter[category]’ => ‘[x]');
Which Prestashop version do you use ?
I managed to get products for a specific category for v1.5.6.1 as follows:
$webService = new PrestaShopWebservice( YOUR_SITE_URL, YOUR_API_KEY, false );
$opt = array(
'resource' => 'products',
'display' => 'full',
'filter[id_category_default]' => '[8]',
'limit' => '5'
);
$xml = $webService->get($opt);
$resources = $xml->products->children();
At this stage you get a products collection. You can reach properties using standard object notation ..
$xml->categories->category->associations->products->product
foreach ( $resources as $key => $value ) :
echo $value->id; // product's identifier
echo $value->price; // product's .. guess what !
endforeach;
You should be able to see elements exposed by reaching YOUR_SITE/api/products?schema=synopsis
That's fine but I've not been able to retrieve products urls yet in order to print anchors.. Anyone ? Any suggustion ?
Complete documentation (1.5) here
Hope it will help.
EDIT
Construct products URLS on the fly
Make a first API call to retrieve categor(ies/y) you want and their
datas (url slug, ids of products they own, ...)
Make a second API call to retrieve the actual datas corresponding to ids retrieved during the first step.
Slugs are available under the link_rewrite property of items collections (like categories and products). There will be as many slugs as the total of languages that have been configured from the back-end, so you may want to loop over the link_rewrite property to get them all and build all urls.
## Initialize Prestashop API
$webService = new PrestaShopWebservice( YOUR_SITE_URL, YOUR_API_KEY, false );
## Getting category I want
$opt = array(
'resource' => 'categories',
'display' => 'full',
'filter[id]' => '[70]', # we are interested only in one category
'limit' => '1'
);
$xml = $webService->get($opt);
$ws_cat = $xml->categories->category;
$products = $ws_cat->associations->products->product;
## Gathering products ids to feed the second API call filter parameter
$productsIds = array();
foreach ( $products as $p ) {
$productsIds[] = (int)$p->id;
}
## Getting products ..
$opt = array (
'resource' => 'products',
'display' => 'full',
'filter[id]' => '['.implode('|',$productsIds).']',
'limit' => '4'
);
$xml = $webService->get($opt);
$products = $xml->products->product;
if ( count($products) ) {
$products = array();
foreach ( $products as $value ) {
$products[] = array(
'id' => $value->id
,'catalogURL' => "{$prestashop['url']}/{$ws_cat->link_rewrite->language[0]}/{$value->id}-{$value->link_rewrite->language[0]}.html";
);
# There you go ..
}
}

How to update the quantity of all items in the cart with respect to database?

I am doing a shopping cart using code-igniter. While I can do the add cart functions, I'm somewhat confused with how to update them with regards to database. All I want is when I change the item quantity in the cart and click update ,both the cart and database must be updated for that particular item.I tried to do it but couldn't get it.Can someone please enlighten me what to do?
controller code
$this->load->model('user_m');
$result['query'] = $this->user_m->get($id); // inserts coresponing item
foreach ($result['query'] as $row)
$id = $row->pid;
$qty = $a;
$quan=$row->quantity;
$price = $row->pprice;
$name = $row->pname;
$q=$quan-$a; // for remainig stock i.e total qty-user qty
$data = array(
'id' => $id,
'qty' => $qty,
'price' => $price,
'name' => $name,
'stock' =>$q
);
$this->cart->insert($data);
$this->load->model('user_m');
$result['qry'] = $this->user_m->up_cart($id,$q);
redirect($_SERVER['HTTP_REFERER']);
}
just tell me how to update pls!
when ever you add any product in cart, it will generate an uniq row ID and that will be unique for each and every product and hence you need to update quantity by that row ID
$data = array(
'rowid' => 'b99ccdf16028f015540f341130b6d8ec',
'qty' => 3
);
$this->cart->update($data);
// Or a multi-dimensional array
$data = array(
array(
'rowid' => 'b99ccdf16028f015540f341130b6d8ec',
'qty' => 3
),
array(
'rowid' => 'xw82g9q3r495893iajdh473990rikw23',
'qty' => 4
),
array(
'rowid' => 'fh4kdkkkaoe30njgoe92rkdkkobec333',
'qty' => 2
)
);
$this->cart->update($data);
Now you want to know about how you will get row id.
$cart=$this->cart->contents();
it will return whole array of cart and when you listing it on webpage, please associate row id with quantity text box to update that particular product.
please check following url:
https://ellislab.com/codeigniter/user-guide/libraries/cart.html

How do I count the number of each unique array inside a multidimentional array? PHP

I'd like to build a simple shopping cart using arrays. I need to display each unique item in the shopping cart aswell as the quantity of each item along side.
My initial cart array might look like this:
$cart=
array(
array(2003,100,"Table")
,array(2003,100,"Table")
,array(2003,100,"Table")
,array(2004,200,"Chair")
,array(2004,200,"Chair")
);
The first value in each array is the product_id, then the price & then the product name.
How do I print each unique item once aswell as the quantity of each along side?
Thanks in advance.
$new_cart = array();
foreach($cart as $product) {
if(!isset($new_cart[$product[0]])) {
$new_cart[$product[0]] = array('quantity' => 1, 'label' => $product[2], 'price' => $product[1]);
}
else {
$new_cart[$product[0]]['quantity']++;
}
}
I strongly suggest using associative arrays for this though. Your problem is the way you are storing the values. Try using something like this:
$cart_items = array(
2004 => array('quantity' => 3, 'label' => 'Leather Chair', 'price' => 200),
2901 => array('quantity' => 1, 'label' => 'Office Desk', 'price' => 1200),
);
When a user updates a quantity or adds an existing product simply increment the quantity.
You could simply iterate the array and use the product ID as key to count the amounts:
$amounts = array();
foreach ($cart as $item) {
if (!isset($amounts[$item[0]])) $amounts[$item[0]] = 0;
$amounts[$item[0]]++;
}
But it would be easier if your cart just stores the product IDs and amounts, so:
array(
2003 => 3,
2004 => 2
)
This is actually what the algorithm above is doing. But with this, you have all the information you need (product ID and amount).

Categories