Allow only one product in cart - joomla

Running joomla 3.4.8 and VM 3.0.12
I need to allow only one product in cart. I mean, if the client add a product to the cart and then if he/she wants to add another product don't allow it showing error message like " you can't have more than one product in cart"

You can alter the add() function in the cart controller page to achieve it. You can write code something like this
$cart = VirtueMartCart::getCart();
$prdata = $cart->cartProductsData;
$qty = 0;
foreach($prdata as $pdata)
{
$qty = $qty + $pdata['quantity'];
}
if ($cart) {
$virtuemart_product_ids = vRequest::getInt('virtuemart_product_id');
$error = false;
if($qty>=1)
{
$msg = vmText::_('you can't have more than one product in cart');
$type = 'warning';
}
else
{
$cart->add($virtuemart_product_ids,$error);
Use the same logic in updatecart() function in the same page.
If you using Fancypopup addtocart, then use the code in addJS function instead of add() function.

Related

Laravel session

I have done projects laravel shopping cart when clicking on the product detail page will open and click the add to cart sends the id to CartController # index. I then access the database and retrieve product information and I put on the session. Then I bought other products but my older products lost in session when I turn the page. Who can help me thanks
Probably you don't add this to existed array in session, but replace it.
Should do it this way:
$products = Session::get('products', array()); // get existed products or empty array
$products[] = $newProduct; // add new product to list
Session::put('products', $products); // put all products to session
this is my code for cart
public function addItem($ids,Request $request){
$data = Product::find($ids)->toArray();
$name =$data['name'];
$price = $data['price'];
$cart = $request->session()->get('cart');
if($cart==null){
$cart['quantity'][$ids] = 1;
$cart['price'][$ids] = $price;
$cart['name'][$ids] = $name;
$request->session()->put('cart', $cart);
}else{
if(array_key_exists($ids,$cart['quantity'])){
$cart['quantity'][$ids] += 1;
$cart['price'][$ids] = $price * $cart['quantity'][$ids];
$cart['name'][$ids] = $name;
}else{
$cart['quantity'][$ids] = 1;
$cart['price'][$ids] = $price;
$cart['name'][$ids] = $name;
}
$request->session()->put('cart', $cart);
}
}

Magento Multiple Wishlist creation programmatically

I know how to add products to a customer's wishlist programmatically however it only adds to one wishlist. I have the multiple wishlist option set to enabled however I do not know how to create a new wishlist instead of merging products into the existing wishlist.
public function submitQuote(Mage_Adminhtml_Model_Session_Quote $quote)
{
$currentQuote = $quote->getQuote();
$customer = $currentQuote->getCustomer();
$items = $currentQuote->getAllVisibleItems();
//$wishlist = Mage::helper('wishlist')->getWishlist();
//Mage::register('wishlist', $wishlist);
$wishlist = Mage::getModel('wishlist/wishlist');
$curretDate = date('m/d/Y', time());
$wishlist->setCustomerId($customer->getId());
$wishlist->setName('Quote ' . $curretDate)
->setVisibility(false)
->generateSharingCode()
->save();
foreach ($items as $item)
{
$productId = $item->getProductId();
$product = Mage::getModel('catalog/product')->load($productId);
$buyRequest = $item->getBuyRequest();
$result = $wishlist->addNewItem($product, $buyRequest);
if(is_string($result))
{
Mage::throwException($result);
}
$wishlist->save();
}
//Mage::unregister('wishlist');
}
I've had this problem before. To add a product to a customer's wishlist you need to start a wishlist model and call the addNewItem method passing the product object. Here is how I do it.
$customer = Mage::getModel('customer/customer');
$wishlist = Mage::getModel('wishlist/wishlist');
$product = Mage::getModel('catalog/product');
$customer_id = 1;
$product_id = 1;
$customer->load($customer_id);
$wishlist->loadByCustomer($customer_id);
$wishlist->addNewItem($product->load($product_id));
Hope this helps!
EDITED:
$customerid= "YOUR CUSTOMER ID "; // Modify This
$customer=Mage::getModel("customer/customer")->load($customerid);
$wishlist=Mage::getModel("wishlist/wishlist")->loadByCustomer($customer,true);
Check for reference this: app/code/core/Mage/Wishlist/Model/Wishlist.php
LATEST EDIT:
I see that your problem is here:
$result = $wishlist->addNewItem($product, $buyRequest);
FINAL EDIT:
Then create another function and add the items and wishlist you want to add it to (as parameters). This should add items to the wishlist that you have passed as an argument when calling the function. Its the only solution I can come up with. Hope this helps!
I just stumbled across this question because I'm trying to do something similar - your code part helped me to create a new wishlist, and I've managed to do the rest so thought I'd share.
This code will;
Create a new wishlist with the name 'Quote dd/mm/YY h:m:s'
Load the wishlist collection for the current user, ordered by highest ID first
Load the newest wishlist
Add all basket items to it
Redirect the user to the new wishlist view.
public function addToQuoteAction() {
if(Mage::getSingleton('customer/session')->isLoggedIn()) {
$customerData = Mage::getSingleton('customer/session')->getCustomer();
$customerId = $customerData->getId();
} else {
Mage::getSingleton('core/session')->addError($this->__('Please login to use this feature'));
return $this->_redirectUrl($this->_getRefererUrl());
}
// Create the wishlist
$newWishlist = Mage::getModel('wishlist/wishlist');
$curretDate = date('d/m/Y h:m:s', time());
$newWishlist->setCustomerId($customerId);
$newWishlist->setName('Quote ' . $curretDate)
->setVisibility(false)
->generateSharingCode()
->save();
// Find the newly create list and load it
$wishlistCollection = Mage::getModel('wishlist/wishlist')->getCollection()
->addFieldToFilter('customer_id',$customerId)->setOrder('wishlist_id');;
$firstWish = $wishlistCollection->getFirstItem();
$wishlist = Mage::getModel('wishlist/wishlist')->load($firstWish->getWishlistId());
$cart = Mage::getModel('checkout/cart')->getQuote();
//getAllItems
foreach ($cart->getAllVisibleItems() as $item) {
$productId = $item->getProductId();
$buyRequest = $item->getBuyRequest();
$result = $wishlist->addNewItem($productId, $buyRequest);
$wishlist->save();
}
Mage::getSingleton('core/session')->addSuccess($this->__('All items have been moved to your quote'));
return $this->_redirectUrl(Mage::getBaseUrl().'wishlist/index/index/wishlist_id/'.$firstWish->getWishlistId().'/');
}

How can I retrieve shopping cart contents, shipping details and ancillary fees (tax, discounts, etc) for my custom payment method?

I'm tasked to write a custom payment method for Magento CE, and tinkered with it for the last couple of weeks. Although I'm an experienced developer, this was my first serious brush with php and Magento itself.
Please note this a web payment gateway, so I'm using
public function getOrderPlaceRedirectUrl() { ... }
In my Payment Method Model to redirect the customer to the external url successfully.
The issue that kept me stuck for a full day is how to retrieve checkout shopping cart contents, shipping details and ancillary fees (tax, discounts, etc). This info needs to be sent to the payment method API.
The code I've been using in my Payment Method Model is something like this:
$order_id = Mage::getSingleton("checkout/session")->getLastRealOrderId();
$order = Mage::getModel('sales/order')->loadByIncrementId($order_id);
$oBillingAddress = $order->getBillingAddress(); //this works ok
$total = number_format($order->getBaseGrandTotal(), 2, '', ''); //this too
/* The following code won't work */
$oShippingAddress = $order->getShippingAddress(); // is unset!?
$oShippingAddress->getSameAsBilling(); //HOW can I check this?
$amount = array();
$quantity = array();
$sku = array();
$description = array();
$cart_items = $order()->getAllVisibleItems();
foreach ($cart_items as $item) {
$amount[] = number_format($item->getPrice(), 2, '', ''); //ok
$quantity[] = $item->getQtyToInvoice(); // is empty...
$sku[] = $item->getSku(); // nothing either??
$description[] = $item->getName(); //this is working
}
Please, wizards of Magento, tell what am I doing wrong here?
Magento dev has been very frustrating, mainly for its lack of straightforward documentation. I'm sure it's very customizable and what not, but the abuse of php's magic functions and it's cumbersome structure has been challenging - at the least.
I think you need to get the quote. Something like this should work:
$quote = Mage::getSingleton('checkout/session')->getQuote();
$items = $quote->getAllVisibleItems();
foreach ($items as $item) {
$amount[] = number_format($item->getPrice(), 2, '', ''); //ok
$quantity[] = $item->getQtyToInvoice(); // is empty...
$sku[] = $item->getSku(); // nothing either??
$description[] = $item->getName(); //this is working
}
If you still need the order, let me know..

Magento : Reorder item with custom price, price always 0

I have some product that has custom prices. Depending on the option selected, there is a formula applied that add fees to the product, so the price is never the same. The problem I have is that when you reorder, the price of the reordered product is always 0.
In sales/controllers/OrderController , in the function reorder,there is this :
$order = Mage::registry('current_order');
$items = $order->getItemsCollection();
foreach ($items as $item) {
try {
$cart->addOrderItem($item);
...
If I add these lines , I’m able to retrieve the custom price, but I can’t find a way to edit the item so that is the price being added in the reorder.
$options = $item->getProductOptions();
$options = $options['info_buyRequest'];
$customPrice = $options['custom_price'];
There is what I have tried (in the loop, before $cart->addOrderItem($item) ), without success.
$item->setSpecialPrice($customPrice);
$item->setCustomPrice($customPrice);
$item->setOriginalPrice($customPrice);
$item->setBaseOriginalPrice($customPrice);
$item->setBaseCost($customPrice);
$item->setBaseRowInvoiced($customPrice);
$item->setRowInvoiced($customPrice);
$item->save();
Any help?
Several possibilities. I'd try an event observer for the checkout_cart_product_add_after event.
// observer method:
public function checkoutCartProductAddAfter(Varien_Event_Observer $observer)
{
$action = Mage::app()->getFrontController()->getAction();
if ($action->getFullActionName() == 'sales_order_reorder')
{
$buyInfo = $observer->getQuoteItem()->getBuyRequest();
if ($customPrice = $buyInfo->getCustomPrice())
{
$observer->getQuoteItem()->setCustomPrice($customPrice)
->setOriginalCustomPrice($customPrice);
}
}
}

Check whether a product is in the wishlist or not

I'm working on a Magento theme, and I need to build a function that can check to see whether or not a product has been added to the user's wishlist.
Magento has a "Mage_Wishlist_Helper_Data" helper class, but I have no idea how to build a check-if-already-in-wishlist function. Basically I need to use Magento's wishlist feature to build a favorites list. I want to add a special class to the "add to wishlist" link if the particular product was already added to the user's favorites.
<?php $wishlist = Mage::getModel('wishlist/item')->load($_product->getId(),'product_id');
if($wishlist->getId())
//product is added
echo "Added! - Product is in the wishlist!";
else
//add product to wishlist
echo "<a href='".$this->helper('wishlist')->getAddUrl($_product) ."'>Add This?</a>";
;?>
Since collections are lazy loaded, I am assuming you can do something such as:
$_product = ...; // some product object you already have
$_productCollection = Mage::helper('wishlist')->getProductCollection()
->addFieldToFilter('sku', $_product->getSku());
if($_productCollection->count() > 0) {
// User already has item in wishlist.
}
You can do similar filtering on other fields, but SKU should be sufficient in this case.
I'm only getting back to this project now, but I took Daniel Sloof's suggestion, and it worked perfectly using the function below:
public function isInWishlist($item)
{
$_productCollection = Mage::helper('wishlist')->getProductCollection()
->addFieldToFilter('sku', $item->getSku());
if($_productCollection->count()) {
return true;
}
return false;
}
This checks to see whether or not a product has been added into the current user's Wishlist. I called it my template file like this:
if ($this->helper('wishlist')->isInWishlist($_product)) :
I found this solution after checked select query of Mage::helper('wishlist')->getWishlistItemCollection(). I hope this solution help to someone.
/**
* Check customers wishlist on identity product.
* #param Mage_Catalog_Model_Product $_product
* #return bool
*/
private function _isInWishlist($_product)
{
$_productCollection = Mage::helper('wishlist')->getWishlistItemCollection()
->addFieldToFilter('product_id', $_product->getId());
if ($_productCollection->count()) {
return true;
}
return false;
}
I solved this by the below function. I Hope this may help some one.
function checkInWishilist($_product){
Mage::getSingleton('customer/session')->isLoggedIn();
$session = Mage::getSingleton('customer/session');
$cidData = $session->isLoggedIn();
$customer_id = $session->getId();
if($customer_id){
$wishlist = Mage::getModel('wishlist/item')->getCollection();
$wishlist->getSelect()
->join(array('t2' => 'wishlist'),
'main_table.wishlist_id = t2.wishlist_id',
array('wishlist_id','customer_id'))
->where('main_table.product_id = '.$_product->getId().' AND t2.customer_id='.$customer_id);
$count = $wishlist->count();
$wishlist = Mage::getModel('wishlist/item')->getCollection();
}
else {
$count="0";
}
if ($count) :
return true;
else:
return false;
endif;
}
Since #alexadr.parhomenk s' solution causes undesired results, here's a smaller method that does the trick:
/**
* Check customers wishlist on identity product.
* #param int $productId
* #return bool
*/
public function isInWishlist($productId)
{
$ids = Mage::registry('wishlist_ids');
if (!$ids) {
$productCollection = Mage::helper('wishlist')->getWishlistItemCollection();
$ids = $productCollection->getColumnValues('product_id');
Mage::register('wishlist_ids', $ids);
}
return in_array($productId, $ids);
}

Resources