Can not understanding PHP associative array code difference in below codes - php

I have this code in my Shopping Cart. I got this from a site. I am using this and code is working fine but i can not understand the difference between the first code and the second code. It seems both are same to me. But if i use only the first code in the place of second code then my cart doesn't work right. So can anyone tell me what is the difference between them??
First code:
$cart = [
$id => [
'name' => $product->name,
'quantity' => 1,
'price' => $product->price,
'photo' => $product->image
]
];
Second code:
$cart[$id] = [
'name' => $product->name,
'quantity' => 1,
'price' => $product->price,
'photo' => $product->image
];

The first code replaces, whatever was in $cart before, with a new array. This means that any previous value of $cart is forgotten.
The second code only adds a new key-value pair into the array, or replaces that key-value pair if it already existed. This means that other key-value pairs will not be forgotten.
I assume the $id is a product identifier. The first code will only allow one product in your cart, whereas the second code allows many different products.

Related

How to edit option keys value in Gloudemans/Shoppingcart for laravel?

I'm using this package in order to use shopping cart in my project.
When I'm adding some data to cart im using following code. and its working fine.
Cart::instance(session('email'))->add($part_number.$variation,$part_number,$quantity,$price,
['capa' => $variation, 'from'=>$from, 'ship_via' => $item->shipping_method,'code'=>$code,'paint_price'=>$paint_price, 'shipping_price' => $shipping_price]);
Initially when I'm adding an item shipping_price will be 0, in Checkout page, I want to manipulate this shipping_price key. but this following code is not working.
Cart::instance(session('email'))->update($row_id,['shipping_price' => $shipping_price]);
However when I want to edit the price key of the cart like this:
Cart::instance(session('email'))->update($row_id,['price' => $cart->qty * $cart->price + $shipping_price]);
its totally working fine. So basically I want to edit the shipping_price which is in the options array of that Cart's Content(). Anyone who worked with Gloudemen, any kind of help is appreciated.
I hope is better:
$item = Cart::get($rowId);
$option = $item->option->merge(['color' => 'red', 'size' => 'XL']);
Cart::update($rowId, ['qty' => 60, 'price' => 299, 'options' => $option]);

What are the specifics for Laravel Query Builder's updateOrInsert function?

This function is not in the Laravel documentation, I have found it in the source code however I am not completely sure how it should be used. For example, if I am working with products, I want to either insert or update a product in the database based on its UPC. If a product with the same UPC exists, update it with the new values. If not, insert the new values as a new product. How should this be done using this function?
Thank you!
Insert or update a record matching the attributes, and fill it with values.
updateOrInsert(array $attributes, array $values = [])
https://laravel.com/api/master/Illuminate/Database/Query/Builder.html#method_updateOrInsert
DB::table('products')->updateOrInsert(
[
'upc' => $request->get('upc'),
],
[
'upc' => $request->get('upc'),
'name' => $request->get('name'),
'vendor' => $request->get('vendor'),
'description' => $request->get('description')
]
);

Elasticsearch. Data update and keeping history of previous values of fields

Part of my mapping is:
"current_price" => ["type" => "float"],
"price_history" => [
"type" => "nested",
"properties" => [
"date" => ["type" => "date"],
"value" => ["type" => "float"]
]
As you can see I keep in storage current price of goods and all the previous values. First thing I would like to notice is when I create goods in a very first time, I have no history, of course. That's why when I create goods, I do not use price_history at all, although it exists in my mapping.
$params = [
'index' => config('storesettings.esIndex'),
'type' => config('storesettings.esType'),
'id' => $id,
'body' => [
...
"current_price" => $request->get('current_price'),
...
]
];
When I edit goods, I change the price. In this case I need to move the current price to archive, moving it to price_history field. And then I need to replace current name. The question is about price_history field. I get previous value ($goods['_source']['price_history']) then I add to this array current_name. Everything is fine when I already have some history. But if I have not, then I get the error 'Undefined index: price_history'. In this case I should do checking: if(isset($goods['_source']['price_history'])). Is it normal? In relational databases I would have an empty array, but in Elasticsearch I haven't and I must do array level (so to speak) checking. How to handle such cases? Maby I should add an epmty array to price_history when I create goods?..

MongoDB does not seem to want to push to array

I am making an online shopping cart, and I having huge issues pushing add to cart to items
$collection->update(
array('session' => $_SESSION["redi-Shop"],
array('$push'=>
array('items'=> $_POST["item"])
)));
When the customer selects their first item to add to the cart it works fine
$collection->insert(
array('session' => $_SESSION["redi-Shop"],
'status' => "cart",
'items' =>$_POST['item']));
but after the first item is added it does not allow me to add any more.
Please any advice would be helpful.
When you insert it the first time, the items field is not array (probably a string).
According to the mongodb $push doc :
The operation will fail if the field specified in the $push statement
is not an array.
Change your insert operation to :
$collection->insert(
array(
'session' => $_SESSION["redi-Shop"],
'status' => "cart",
'items' => array($_POST['item'])
));
Then run your update query.

Magento duplicating options

I'm generating custom options for products in magento with the following:
$options = array();
$options = array(
'title' => 'Select Options',
'type' => 'radio',
'is_require' => 1,
'sort_order' => 0,
'values' => array()
);
$options['values'][] = array(
'title' => $customAttributeString,
'price' => 0.00,
'price_type' => 'fixed',
'sku' => $uniqueId,
'sort_order' => '1'
);
$id = Mage::getModel('catalog/product')->getIdBySku($sku);
$product = Mage::getModel('catalog/product')->load($id);
if(!$product->getOptionsReadonly()) {
$product->setProductOptions(array($options));
$product->setCanSaveCustomOptions(true);
$product->save();
}
I have this running in a loop, with a different SKU everytime and when I run my loop once, it generates custom options for the first product just fine, the second product has its own custom options, and the first products custom options, and the third product has custom options for all three, etc.. could anyone give me some insight on why this is happening?
Sorry for the late response but as Magento manage the product_option as a Singleton, you need to reset it on each iteration :
Mage::getSingleton('catalog/product_option')->unsetOptions();
Hope this helps.
Guillaume
$product->setProductOptions(array($option));
Notice, that you're setting not $options but $option (without "s" at the end). Maybe it intersects with some of your variables not shown in code snippet.
Also
$options = array();
is useless here, just remove it
It would be nicer, if you include iteration cycle and initialization of $customAttributeString, $sku, $uniqueId to you code sample there.
Mage::getSingleton('catalog/product_option')->unsetOptions();
Work fine just before the loop iteration
Atif

Categories