I'm designing a custom product page with a button that when clicked I need to have an alert come up with a "Yes" or "No" option.
If "Yes" is selected I then need the following to happen.
Add another product into the cart based on the products quantity i.e. between 1 & 2 items add product A between 3 & 4 Items product B, between 5 & 12 product C and so on.
Any idea of the best way to accomplish this?
It has to be a alert style popup (ajax popup preferred) cannot be a checkbox on the product page.
Thanks!
So I've come across a solution to my problem... I'm using a Simple Modal (That TheBlackBenzKid hinted me to) that I'm either going to call from a custom button or with the add to cart button. This in turn will redirect to a php page that will redirect to the cart. For the php page I'll just include the code to put a item into the cart from there anyone could figure out how to customize it to there own needs.
<?php
// Include Magento application (URL to Mage.php)
require_once ( "app/Mage.php" );
umask(0);
//specified quantity my own variable I'm using for quantities
$spqty = 9;
// Initialize Magento
Mage::app("default");
// You have two options here,
// "frontend" for frontend session or "adminhtml" for admin session
Mage::getSingleton("core/session", array("name" => "frontend"));
$session = Mage::getSingleton("customer/session");
// get the current Magento cart
$cart = Mage::getSingleton('checkout/cart');
if ($spqty <= 2) {
// insert item to cart where "98" is the product ID (NOT SKU!) Where "2" is the quantity
$cart->addProduct(98, array('qty' => 2));
} elseif ($spqty >= 4 ){
// you can add multiple products at the same time by adding this line multiple times
$cart->addProduct(96, array('qty' => 3));
}
// save the cart
$cart->save();
// very straightforward, set the cart as updated
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
// redirect to index.php
header("Location: index.php/checkout/cart");
I also found some of this information from this guys blog I'll link to the article
How to add a product from an external site into Magento
I'm happy to answer any questions on this...
This is not the best answer, but code to get you started:
You could make the cart function use:
<input type="button" onClick="javascript:nValidateForm();"/>
And your form code:
<form name="m2mform" id="m2mform" method="post" onSubmit="javascript:nValidateForm();">
And then just call an external JavaScript in your page XML headers and add it to cart so that JS file will always be checked and validate the popup.
Related
How can I display tier price on cart page in Magento?
I understand that the method getTierPriceHtml() only works on List and Detail page, so we cannot use that on cart page.
I'm editing /template/checkout/cart/item/default.phtml
you want tier price in cart page also.I think this is useful for your functionality
you can directly use this code in checkout/cart/item/default.phtml: after the $_item = $this->getItem(); (in line 28)....
**$tierPrice = $this->getLayout()->createBlock(
'catalog/product_view',
'product.tierprices',
array(
'product_id' => $_item->getProductId()
)
);**
**$tierPrice->setTemplate('catalog/product/view/tierprices.phtml');**
**echo $tierPrice->getTierPriceHtml();**
you can create your own style template that file also you can use it in place of catalog/product/view/tierprice.phml like catalog/product/view/tierprice1.phml
I have a little experience in magento. All my products have custom size in option.
All products have different sizes and different prices.
Customer adds one product with quantity 5 to cart. So 5 products of this size are added to cart. When the customer adds another product with different size all products in cart change to this size.
How can i prevent this behavior?
Unless you're doing this programmatically (that is writing the code), there's no way to do this.
When Magento adds a product, it first looks into the quote / shopping cart to see if one already exists. If one does, it pulls that one and adds to the quantity. There is no way to turn this off.
Programmatically, you very manually add an item to a shopping cart. This is how...
$cart = Mage::getSingleton("checkout/cart");
foreach ($products_to_add as $product_id => $custom_options) {
$product = Mage::getModel("catalog/product")->load($product_id);
$options = new Varien_Object(array("options" => $custom_options,
"qty" => 1));
// some products may result in multiple products getting added to cart
// I beleive this pulls them all and sets the custom options accordingly
$add_all = $product->getTypeInstance(true)
->prepareForCartAdvanced($options, $product, Mage_Catalog_Model_Product_Type_Abstract::PROCESS_MODE_FULL);
foreach ($add_all as $add_me) {
$item = Mage::getModel('sales/quote_item');
$item->setStoreId(Mage::app()->getStore()->getId());
$item->setOptions($add_me->getCustomOptions())
->setProduct($add_me);
$item->setQty(1);
$cart->getQuote()->addItem($item);
}
}
// when done adding all the items, finally call save on the cart
$cart->save();
I'm building a commercial website, with a shopping cart.
On most pages (i.e - product page, category page), I want to display the cart contents on a sidebar, which will get updated via AJAX when an item is added to the cart.
On the "display cart" page I want to show a full version of the contents.
Obviously, it seems logical to use the same model and functions to get and/or update the cart, but send the data to a different view (sidebar or full cart), depending on the caller page.
The question is, in the cart model, how can I detect where did the request come from.
I thought I'd check if the request came via AJAX, like so:
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') // i.e - the request came as AJAX
{
$this->load->view('cart_sidebar_view', $data);
}else{ /* not ajax */
$data['main_content'] = 'cart_view';
$this->load->view('includes/template', $data);
}
But that is not good enough, because I want to use AJAX on the "display cart" page as well, to allow updating the cart from there.
So, is there a way to detect, in the cart model, where did the request come from? Or will I have to send that info in a hidden form field with every "add to cart" or "remove" button?
There is a simple way. when you are sending request from the display cart page send an additional variable. Than in the controller check for this variable if variable is coming call a logic if variable is not coming do something else.
if($this->input->is_ajax_request())
{
$this->load->view('cart_sidebar_view', $data);
}else{
if($this->input->post('another_variable')){
// do something else
}else{
$data['main_content'] = 'cart_view';
$this->load->view('includes/template', $data);
}
}
In my store, one of the category has only one product. Is it possible to take the user directly to the product detail page of this one product whenever they click this category in the nav bar? i want to use some code to get this?
Put this in the product grid or list foreach loop in /app/design/frontend/default/[template]/template/catalog/product/list.phtml
<?php if ($_productCollection->count() == 1) {
$url = $_product->getProductUrl();
Mage::app()->getFrontController()->getResponse()->setRedirect($url); }
?>
I have SCP (simple configurable product) installed and for every simple product that has no custom options, the price doesn’t show up in the product page. As soon as I add an option, the price shows up.
There is no price block or template called on the product page when no options is selected.
I saw that the extension is extending Mage_Catalog_Block_Product_Price block , but I can't find where this block should be called.
Haven't found any solution yet , so I made this jquery workaround that works
Put this at the end of template/catalog/product/view.phtml
jQuery(document).ready(function($) {
if($('.price-box').length==0){
var price = '<div class="price-box"><span class="regular-price"><span class="price">$<?php echo number_format($_product->getPrice(),2); ?></span></span></div>';
$('.product-options-bottom').before(price);
}
});
Of course, you need to have jquery.