Get only first element from database - php

I want to show bought "products names" in prestashop orders page.
For prestashop 1.6 i already have a code that works, but using the same code on prestashop 1.7 give me duplicate orders ...
public function getProductsName($id_order, $tr)
{
$products=Db::getInstance()->executeS(
'SELECT product_name FROM '._DB_PREFIX_.'order_detail
WHERE id_order='.(int)$id_order);
$str='';
foreach($products as $val) {
$str.=substr($val['product_name'],0,35).'| ';
}
$str=trim($str,'|');
return $str;
}

Ok i found a sulution. Apart from the script i posted above i added to AdminOrdersController.php this piece of sql:
a.id_order AS product_name,
and added to product_name array:
'callback' => 'getProductsName',
and retouched the script to remove the last |:
see updated script in first post.
all changes are in AdminOrdersController.php
Thanks for all.
Final result: http://shrani.si/f/28/pH/2jxmMrf7/qq-photo20190704132806.jpg

Related

Selecting and using JSON array values

I'm currently working on a shopping application but i stumbled upon a problem. The problem is that i can't select the JSON values from my "shopping cart". See the following code and description.
So by using add to cart buttons and such i'm creating the shopping cart. The shopping cart is actually a JSON object. An example of the cart:
{"cartItems":{"2":{"id":"2","name":"Blouse","price":26.99,"size":"M","quantity":"3","total_product_price":80.97},"5":{"id":"5","name":"Bedrukte Zomerjurk","price":30.5,"size":"L","quantity":"4","total_product_price":122}},"totalPrice":202.97,"totalItems":2,"customerSelect":{"id":"1","firstname":"John","lastname":"TestStefans"}}
As you can see the design of my JSON cart is:
cart:{"cartItems":{"id":{ product information }}}
The problem now is trying to select the values like the "name" and "price". This due to the "id"{ segment. But i need that piece for removing one item by id from the cart.
So my question is:
How would i be able to select all the product information and create an foreach for placing the information in the database / email template. I've been trying this but this only gave me the first product:
$cart_encode = json_encode($_SESSION['cart']['cartItems']);
$cartDecode = json_decode($cart_encode);
// Adding the product items
foreach ($cartDecode as $key => $cartItem) {
$resource->associations->cart_rows->cart_row->id_product = $cartItem->{"id"};
$resource->associations->cart_rows->cart_row->id_product_attribute = 1;
$resource->associations->cart_rows->cart_row->id_address_delivery = 1;
$resource->associations->cart_rows->cart_row->quantity = $cartItem->{"quantity"};
}
Take note that i'm using XML for database input. For the email template i've tried:
$testName = $_SESSION['cart']['cartItems']['name'];
$testPrice = $_SESSION['cart']['cartItems']['price'];
$testQuantity = $_SESSION['cart']['cartItems']['quantity'];
$testTotal = $_SESSION['cart']['cartItems']['total_product_price'];
$testProduct = array(
"Name:" => $testName,
"Price:" => $testPrice,
"Quantity" => $testQuantity,
"Total" => $testTotal
);
Iknow that the id number is missing but i cant dynamicly avoid that layer.
I hope that my question is clear
As always. Thanks in advance!

Displaying Label in opencart based on Category

I am trying to add labels on product list page based on special conditions. I have buy one get one free sale, so When user visits buy 1 get 1 free category, he should be able to see the label on the products(in my case, I have bogo.png image). Everything worked fine with the modifications I did until I searched on the store front for a product, I get undefined variable error.
2015-01-09 18:26:58 - PHP Notice: Undefined index: bogo in catalog/view/theme/YourTheme/template/product/product_collection.tpl on line 26
I did google search for the problem and browsed opencart forums for days without any luck. So here is what I did until now. On Category.php in catalog/controller file, I added under this array$this->data['products'][]= array(
'bogo' => $bogo,
And added this condition under getProducts specifying if the category id is the category id for the buy 1 get 1 free category set bogo to true.$results = $this->model_catalog_product->getProducts($data);
if($category_id==977){
$bogo = true;
}
else{
$bogo = false;
}
and on product_collection.tpl file, I did this change.
<pre><code>
<?php if( $product['bogo'] ) { ?>
<span class="product-label-bogo2"><img src='bogo.png'></span>
<?php } else if ($product['special']) { ?>
<span class="product-label-special"><span><?php echo $this->language->get( 'text_sale' ); ?></span></span>
<?php } ?>
</code></pre>
Everything is fine if I go to that category it displays the label perfectly, the problem is I get that above error only when I search for anything on store front.
Please note that before rating the question negatively, I am not familiar at all with php and I did my best with research for hours to solve this.
you need to add that piece of code
if($category_id==977){$bogo = true;}else{$bogo = false;}
$this->data['products'][]= array('bogo' => $bogo,
to controller/product/search.php => ControllerProductSearch#index
BTW: you will need the add the above code in every controller file that makes use of product/product_collection.tpl

How to update "inventory_level" of product by sku id in Bigcommerce?

I am trying to update the inventory level of a products but unfortunately not getting success.Here is my code.
I want to update product's "inventory_level" but enable to do so..
<?php
require "bigcommerce.php";
use Bigcommerce\Api\Client as Bigcommerce;
Bigcommerce::configure(array(
'store_url' => 'https://store-nos85a.mybigcommerce.com/',
'username' => 'admin',
'api_key' => '4b7c4bba19f290a728e00be6ae7133cda71f477b'
));
Bigcommerce::setCipher('RC4-SHA');
Bigcommerce::verifyPeer(false);
$product = Bigcommerce::getProduct(76);
print_r($product->skus);
foreach($product->skus as $sku)
{
if($sku->id==5)
{
$fields = array("inventory_level"=>112);
Bigcommerce::updateProduct(76,$fields);
}
echo "id : ".$sku->id;
echo " Invntory Level: ".$sku->inventory_level."<br/>";
echo " SKU : ".$sku->sku."<br/>";
}
?>
Here Bigcommerce::updateProduct(76,$field); is not working.
No need to download ALL products, why not retrieve JUST the product that has the sku you need?
GET: /api/v2/products?sku=sku-that-I'm-interested-in
parse the response for the id (that's the product ID for the product with that SKU)
Then do a PUT using that product ID.
I am trying to do the same thing (different programming language). I have a call into their support. It seems like you still must reference the product_id.
You cannot update simply by SKU. You must know the product_id. I got around this by requesting all products and loading the response into an array (product_id, sku, etc.). I then read thru the array, get the quantity from our system using the SKU, and post (PUT) the inventory_level using the associated product_id.
IMHO this is a huge oversight on their part. Who cares what the product_id is on the website?
I have asked for an enhancement that allows updating by SKU.

Where does Magento Set a Quote Item's Price?

Whenever you load the cart page in Magento, the following code is run
$cart->init();
$cart->save();
One side effect of this is that the prices for any items in the cart are updated if the price of the product has been updated. This actually updates the entry in sales_flat_quote_item. I'm trying to track down where in code the price is updated on each quote item, and where each quote item is saved.
I know the myrid locations it could be set. I'm hoping someone knows where it actually is set. Magento 1.7x branch specifically, although info from all versions is welcome.
Dug this one up myself. So there's this
#File: app/code/core/Mage/Sales/Model/Quote.php
foreach ($this->getAllAddresses() as $address) {
...
$address->collectTotals();
...
}
which leads to this
#File: app/code/core/Mage/Sales/Model/Quote/Address.php
public function collectTotals()
{
Mage::dispatchEvent($this->_eventPrefix . '_collect_totals_before', array($this->_eventObject => $this));
foreach ($this->getTotalCollector()->getCollectors() as $model) {
$model->collect($this);
}
Mage::dispatchEvent($this->_eventPrefix . '_collect_totals_after', array($this->_eventObject => $this));
return $this;
}
The getTotalCollector object returns a sales/quote_address_total_collector object, which loads a series of collector models from global/sales/quote/totals and calls collect on them. The sub-total collector's collect method ultimately calls this
#File: app/code/core/Mage/Sales/Model/Quote/Address/Total/Subtotal.php
protected function _initItem($address, $item)
{
//...
if ($quoteItem->getParentItem() && $quoteItem->isChildrenCalculated()) {
$finalPrice = $quoteItem->getParentItem()->getProduct()->getPriceModel()->getChildFinalPrice(
$quoteItem->getParentItem()->getProduct(),
$quoteItem->getParentItem()->getQty(),
$quoteItem->getProduct(),
$quoteItem->getQty()
);
$item->setPrice($finalPrice)
->setBaseOriginalPrice($finalPrice);
$item->calcRowTotal();
} else if (!$quoteItem->getParentItem()) {
$finalPrice = $product->getFinalPrice($quoteItem->getQty());
$item->setPrice($finalPrice)
->setBaseOriginalPrice($finalPrice);
$item->calcRowTotal();
$this->_addAmount($item->getRowTotal());
$this->_addBaseAmount($item->getBaseRowTotal());
$address->setTotalQty($address->getTotalQty() + $item->getQty());
}
//...
}
and this is where the quote item gets it's price set/rest.
From a high level, the code that starts the whole process are lines 464 and 465 of Mage_Checkout_Model_Cart :
$this->getQuote()->collectTotals();
$this->getQuote()->save();
The new product price is being set against the quote in Mage_Sales_Model_Quote_Address_Total_Subtotal in the _initItem method. You will see $item->setPrice in the the if / else statement starting at line 104
If you are trying to do custom price changes on products in the cart, rather than extend and modify core classes, I use an observer sales_quote_save_before. It works great if you are trying to customize pricing (especially when I have products that can be custom length).

Code Igniter - carts library random works

I’m using a cart library to get orders in my web bookstore. But when I call a addCart function on one of my book it’s works, but not all the time. Please, help
There is my model function:
function get_books_by_ID($id)
{
$this->db->where('BOOK_ID', $id);
$query = $this->db->get('books');
return $query;
echo vardump($query);
}
Controller:
function addCards($id=1)
{
$query = $this->Kategorie_model->get_books_by_ID($id);
if($query->num_rows() > 0)
{
$item = $query->row();
$data = array(
'id' => $item->BOOK_ID,
'qty' => 1,
'price' => $item->BOOK_Price,
'name' => $item->BOOK_Title
);
$this->cart->insert($data);
}
}
View:
<tr>
<td class="color"><b>Cena: </b><?php echo $data->BOOK_Price;?>zł</td>
<td class="border" id="koszyk" ><?php echo anchor('ksiegarnia/addCards/'.$data->BOOK_ID, 'Koszyk'); ?></td>
</tr>
UPDATE:
vardump is nothing necessary. I want to use var_dump. But the problem is related with adding items to the session with carts library. I have a bookstore, and when I call a addCarts function, sometimes items is added to Carts, and cart function total() and total_items displaying it, but sometimes when I call function, nothing is happened. The items not add into carts. I don't now why this thing have a place. Why the carts library works randomly?
I just ran into this issue and it seems that in the codeigniter cart library the insert function checks the product(s) id and name against a regex only allow alpha-numeric, dashes, underscores and periods
Adjust the regex to what may come up:
$this->cart->product_id_rules = '.a-z0-9_';
$this->cart->product_name_rules = '.\:-_ a-z0-9';
In my case it would randomly add thing to carts too. If you turn on logging and check you'll be able to see that the name or id may contain invalid chars
i am doing as you did, and like you said, the cart is randomly works,
it seems can only contain 3 product in cart when i add another product, the product i added wont get into cart..
i am so hopeless, i get mind to change cart libraries with 3rd party
it is because i did not setting session to save to database, like in the tutorial (cart & session) the cart need session to write to database in order to work.
when i set session write to database, the problem solved
you can try...

Categories