As above, I've got a strange issue where every other product I add isn't added to the cart or quote in Magento.
It's repeatable, in that I can add one product, then the next one doesn't add (but I do get a status messaging saying it was added), and then the next done after that adds as it should do.
It's not specific to a product, as if I add the same product a second time it will add as expected.
The only thing we have that's a little different is an observer called on the sales_quote_add_item event, but that's just changing the pricing:
public function update_book_price(Varien_Event_Observer $observer) {
//get the admin session
Mage::getSingleton('core/session', array('name'=>'adminhtml'));
if (!$quoteitem = $observer->getQuoteItem()){
$quoteitem = $observer->getItem();
}
if (!$item = $observer->getEvent()->getQuoteItem()){
$item = $quoteitem;
}
$quote = $item->getQuote();
$product = $item->getProduct();
$price = Mage::helper('users')->getCustomerProductPrice(false,Mage::getModel('catalog/product')->load($product['product_id']),false,true,$_POST['qtys'][$product['product_id']]);
echo "Price : $price \n";
//print_r($price);
$price = $price;
if(!$quoteitem->setOriginalCustomPrice($price)) {
echo "Couldn't set price";
}
else {
echo "price updated, save";
try {
$quoteitem->getProduct()->setIsSuperMode(true);
var_dump($quoteitem->save());
}catch(Exception $e){
echo $e->getMessage() . "\n";
}
}
return $this;
}
I've checked at a DB level, and the missing items are not even in the DB - sales_flat_quote_item has no record of them, but the 'working' items are in as normal.
Has anyone experienced anything like this before, or can anyone suggest where I might start investigating? Thanks!
You don't have to save quote item after changing price.
Delete var_dump($quoteitem->save()); and try again.
Related
I was assigned a Magento 1.9.3 project that would take in products in JSON format, and save customer group prices. However, even though there are no errors, nothing gets saved. The group prices won't show in the admin panel nor in the catalogue as expected.
Why is this? I've looked up every single article or stackexchange cases concerning group prices, but none of these seem to solve my problem.
Currently using the catalog_block_product_list_collection event.
$group_prices = $theProduct->getData('group_price');
if (is_null($group_prices)) {
$attribute = $theProduct->getResource()->getAttribute('group_price');
if ($attribute) {
$attribute->getBackend()->afterLoad($theProduct);
$group_prices = $theProduct->getData('group_price');
}
}
if(!is_array($group_prices)){
$group_prices = array();
$theProduct->addData(array('group_price' => $group_prices));
$theProduct->save();
}
$new_price = array(array (
'website_id'=>Mage_Core_Model_App::ADMIN_STORE_ID,
'customer_group_id'=>$customerGroupId,
'price'=> $singleProduct->Price));
$group_prices = array_merge($group_prices, $new_price);
$currentStore = Mage::app()->getStore()->getId();
try{
Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID);
// First Delete all the group prices of product
$theProduct->setData('group_price', array());
$theProduct->save();
// Again save the old prices and new prices
$theProduct->setData('group_price', $group_prices);
$theProduct->save();
} catch(Exception $e) {
echo $e->getMessage();
}
Mage::app()->getStore()->setId($currentStore);
And yet, no group prices are saved when checking in the admin panel. Any help is appreciated.
For customer group field use cust_group instead of customer_group_id.
$new_price = array(array (
'website_id'=>Mage_Core_Model_App::ADMIN_STORE_ID,
'cust_group'=>$customerGroupId,
'price'=> $singleProduct->Price));
For details see:
app/code/core/Mage/Catalog/Model/Product/Attribute/Backend/Groupprice/Abstract.php line 299
there is condition:
if ($hasEmptyData || !isset($data['cust_group']) || !empty($data['delete'])) {
continue;}
I know this is a dump/basic question but I'm stuck and could use some help being a newbie.
What I'm Trying to Achieve: I want to have a foreach loop to get all of the product names in my users cart.
Problem: The foreach loop stops after the second iteration (if there are three things in the cart and I dump, only 2 are shown(the first and second)).
I know what a foreach loop does. I think my problem lays in my variable names but I tried messin around with them to no avail.
if (is_null($cart) || $cart->getSubmitted(true)) {
$cart = new UserCart();
// since new cart no need to check for duplicate product quantity
// add product to cart
$this->addFlash('notice', 'Creating a new cart because one didnt exist for the user before.');
}
else {
$quantity = new Quantity();
//If the cart is set
$getProductsInCurrentUsersCart = $cart->getQuantities(); //All Products In Users Cart (ARRAY COLLECTION/PERSITENT COLLECTION)
foreach ($getProductsInCurrentUsersCart as $key => $value) {
dump($getProductsInCurrentUsersCart);
$getProductsInCurrentUsersCart = $value->getProduct()->getName(); //SHOULD BE ALL PRODUCTS IN CART
if ($getProductsInCurrentUsersCart === $quantity->setProduct($productBeingAddedToCart)->getProduct()->getName()) {
$this->addFlash('notice', 'Comparission was TRUE.');
$quantity->setQuantity($quantity->getQuantity() + 1);
}
else {
$quantity->setQuantity(1);
$quantity->setProduct($productBeingAddedToCart);
$this->addFlash('notice', 'Comparisson was FALSE.');
} //ENDS IF/ELSE
} //EXECUTING ONCE??????????
$cart->setTimestamp(new \DateTime()); // Set Time Product was Added
// $quantity->setQuantity(1); // Set Quantity Purchased
$cart->setSubmitted(false); // Set Submitted
$cart->setUser($this->getUser()); // Sets the User ONCE
$cart->addQuantity($quantity); // Add Quantity ONCE
$quantity->setUserCart($cart); // Create a UserCart ONCE
$em->persist($productBeingAddedToCart);
$em->persist($cart);
$em->persist($quantity);
$em->flush();
$this->addFlash('notice', 'The product: '.$productBeingAddedToCart->getName().' has been added to the cart!');
}
Any help is really appreciated!
$getProductsInCurrentUsersCart = $value->getProduct()->getName();
Here you are redefining the list variable inside the loop. Try with $getProductsInCurrentUsersCart2 and also change it below this line. See if that solves it or not. Then come up with a better name :)
I am am trying to configure a magento store . I have a couple of ghost products that appear on the Front end. These Ghost products have no name, no image , and aren't found under my back-end product list. However their add to button is active and works and also has a wishlist URL in the form of.
example.com/wishlist/index/add/product/4/form_key/p3jZL1nym3j4XeNl/
I think I got myself into this mess after trying to duplicate a few products. How do I trace and get rid of these ghost products. Im using absolute template.
Do you using the flat tables? If yes then try to reindex it.
P.S. check the product ID: 4 (in the backend).
If you wanna delete products with blank(null) product name & images
You can try
$products=Mage::getResourceModel('catalog/product_collection')->addAttributeToSelect('*')->load();
foreach($products as $key => $pId)
{
$product=Mage::getModel('catalog/product')->load($pId);
if($product->getName()=='' && $product->getMediaGalleryImages()=='')
{
$product = Mage::getModel('catalog/product')->load($pId)->delete();
echo "successfully deleted product with ID: ". $pId ."<br />";
}
else{ echo "Could not delete product with ID: ". $pId ."<br />"; }
}
You can also try deleting products which dont have imgs using
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->getSelect()
->joinLeft(
array('_gallery_table' => $collection->getTable('catalog/product_attribute_media_gallery')),
'e.entity_id = _gallery_table.entity_id',
array()
)
->where('_gallery_table.value IS NULL');
$collection->delete();
In Prestashop, I've created a custom form where I show a list with all products and the user can fill the corresponding quantities. By submitting the form, I clear the cart and fill it with the new values and finally redirect to the checkout page.
Everything's working fine, but only when the cart already exists. In a case of an empty cart (cart_id==null), I cannot add the products. I tried with several ways to create a $cart but I didn't manage to do so.
I don't get any exceptions, the code is executed without errors, it's just that at the end, in the checkout page, the cart remains empty; I repeat, only when the cart was already empty. In the case of a cart with products in it, then the process is just perfect!
I would appreciate some help on this.
Here is my small controller that gets the products and quantities from the form and adds them in the cart:
include('../../config/config.inc.php');
include('../../header.php');
// Filling the array of products
$products = array();
foreach ($_POST as $key => $value)
if (substr($key, 0, 9) === 'quantity_')
$products[substr($key, 9)] = $value;
// First of all, we remove all products
$prods = $cart->getProducts();
foreach ($prods as $prod)
$cart->deleteProduct($prod['id_product']);
// Adding the new products
foreach ($products as $product_id => $quantity)
if ($quantity > 0)
$cart->updateQty($quantity, $product_id);
// Redirecting to the checkout page.
header("Location: " . $_POST['redirect']);
exit();`
Thank you in advance!
I had the same problem with Prestashop not correctly creating a new cart upon calling the CartCore in many different ways - neither context or direct calls did work out.
Found this gem from someone else over here:
// get cart id if exists
if ($this->context->cookie->id_cart)
{
$cart = new Cart($this->context->cookie->id_cart);
}
// create new cart if needed
if (!isset($cart) OR !$cart->id)
{
$cart = new Cart();
$cart->id_customer = (int)($this->context->cookie->id_customer);
$cart->id_address_delivery = (int) (Address::getFirstCustomerAddressId($cart->id_customer));
$cart->id_address_invoice = $cart->id_address_delivery;
$cart->id_lang = (int)($this->context->cookie->id_lang);
$cart->id_currency = (int)($this->context->cookie->id_currency);
$cart->id_carrier = 1;
$cart->recyclable = 0;
$cart->gift = 0;
$cart->add();
$this->context->cookie->id_cart = (int)($cart->id);
$cart->update();
}
This is working for me now. As you see it goes a little more in depth than just asking the context to retrieve or create a new cart. hth.
You probably need to include this line when you are outside of a class
$context = Context::getContext();
And then add a cart variable that defines cart attributes ( in ur case assigns a id )
$cart = $context->cart; // gets the cart id or creates a new one
Should work fine now
BR's ( dont forget to accept the answer if it helps you :) )
As a complement to previous posts.
If you set that configuration : Configuration::updateValue('PS_CART_FOLLOWING', 1) (display the last cart of the customer instead of creating a new one each time).
You will always get the cart id $this->context->cart->id at NULL when trying to create a new cart at the customer's first log in (after creating a new account) using the following code:
if (is_null($this->context->cart)) {
$this->context->cart =
new Cart($this->context->cookie->id_cart);
}
I manage to solve the problem by adding some code to after the invalid cart creation.
/**
* Manage the possible errors when trying to create
* a new cart for a customer.
*
* The new cart is saved in the current context ($this->context->cart).
*/
private function createCart()
{
if (is_null($this->context->cart)) {
$this->context->cart =
new Cart($this->context->cookie->id_cart);
}
if (is_null($this->context->cart->id_lang)) {
$this->context->cart->id_lang = $this->context->cookie->id_lang;
}
if (is_null($this->context->cart->id_currency)) {
$this->context->cart->id_currency = $this->context->cookie->id_currency;
}
if (is_null($this->context->cart->id_customer)) {
$this->context->cart->id_customer = $this->context->cookie->id_customer;
}
if (is_null($this->context->cart->id_guest)) {
if (empty($this->context->cookie->id_guest)){
$this->context->cookie->__set(
'id_guest',
Guest::getFromCustomer($this->context->cookie->id_customer)
);
}
$this->context->cart->id_guest = $this->context->cookie->id_guest;
}
if (is_null($this->context->cart->id)) {
$this->context->cart->add();
$this->context->cookie->__set('id_cart', $this->context->cart->id);
}
}
After initializing the cart that way, I'm now able to add products to the cart with no problem.
Good success everyone
I tried to update product's attribute called barcode as follows.
It update the product but cleared tier price of that product. Please help me.
$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $prod_sku);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
if ($product) {
$product->setBarcode($bar_code);
$product->save();
}
if(! $product->save()){
makes little sense as the ! is telling if NOT TRUE product must have been saved....but a successfull saved product is returning $this which is seen as true for the IF-clause without the expression mark.
I guess your problem is completely related to another technical problem...
EDIT: There seems to be some weird problem have a look at this...sounds strange but give it a try Magento product tier prices are deleted on product images update
I got an answer from Marius for my question in magento statckoverflow. Thanks Marius.I'm going to add it here for reference.
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$id = Mage::getModel('catalog/product')->getIdBySku($prod_sku);
$product = Mage::getModel('catalog/product')->load($id);
if ($product) {
$product->setBarcode($bar_code);
if(! $product->save()){
$productId = $product->getId();
echo "product_Id :: ".$productId." - Product sku :: ".$product->getSku()."<br />";
}else{
echo "not saved";
}
}