{Magento} Product page and Add To Cart - magento

I am building a static page that contains several products. I took the static HTML that was generated by one of my product pages and added all the other products to this page. Each product has a radio button and the customer can only select one of them. The qty will always be 1.
How do I submit the product_addtocart_form?
I modified the form submit function like this:
var productAddToCartForm = new VarienForm("product_addtocart_form");
productAddToCartForm.submit = function(){
if(this.validator.validate()) {
var product_id = jQuery("input[name='product']:checked").val();
this.form.action = "/store/checkout/cart/add/product/"+product_id+"/qty/1";
this.form.submit();
}
}.bind(productAddToCartForm);
But it doesn't always work. If I modify the action to this, which is the same as my product page but changing the product_id:
this.form.action = "/store/checkout/cart/add/uenc/aHR0cDovL3N0YWdpbmcuY2ljLnNjaWMuY29tL3N0b3JlL3B1YmxpY2F0aW9ucy8yNS1tb3N0LWlubm92YXRpdmUtYWdlbnRzLWluLWFtZXJpY2EuaHRtbD9fX19TSUQ9VQ,,/"+product_id+"/qty/1";
It also works inconsistenty.
How do I do this??????

Just post the form as an action="get" to /checkout/cart/add
Name your radio field product and another hidden field named qty. Pass the Magento product ID to product and the quantity to qty

Related

How to hide a button if the cart total is less than a specific amount in woocommerce cart page dynamically

I want to hide a button in the cart page, if the cart total is less than $500,and display if it is more than $500.
Which must be get dynamically,when i update woocommerce cart.
Checked with many ajax codes,nothing works at all.
my code:
This button i want get hide/display with cart conditions:
<button id="add_cart_button_style_rg_id" onclick="onclick_pay_button()" class="add_cart_button_style_rg"></button>'
//refresh cart page
add_filter('add_to_cart_custom_fragments',
'woocommerce_header_add_to_cart_custom_fragment');
function woocommerce_header_add_to_cart_custom_fragment( $cart_fragments ) {
global $woocommerce;
ob_start();
?>
<button id="add_cart_button_style_rogue_id" class="add_to_cart_button_link" onclick="onclick_pay_button()" class="add_cart_button_style_rg"></button>
<?php
$cart_fragments['.add_to_cart_button_link'] = ob_get_clean();
return $cart_fragments;
}
If you want to hide button based on cart update, you can use JQuery.
Try following code. It uses updated_cart_totals trigger and compare the price value then hide the element
jQuery (document.body ).on( 'updated_cart_totals', function(){
<!-- get subtotal after cart updated -->
var total = jQuery('table.shop_table .cart-subtotal').html();
<!-- format the price to integer value -->
total = total.replace(/,/g, ''); // Replace comas by points
var total_val = parseInt(total);
if (total_val < 500) {
jQuery("#add_cart_button_style_rogue_id").hide();
}else {
jQuery("#add_cart_button_style_rogue_id").show();
};
});

How to show product custom options in checkout/cart page

I want show custom options on cart page i tried using $_options = $this->getOptionList() but it only shows the selected option only , i want to retrieve all options.
To get product custom option value at cart page which are set at 'AddtoCart' time try with following code.
$cart = Mage::helper('checkout/cart')->getCart()->getQuote()->getAllItems();
/* cart item loop */
foreach($cart as $item) {
/* This will get custom option value of cart item */
$_customOptions = $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct());
/* Each custom option loop */
foreach($_customOptions['options'] as $_option){
echo $_option['label'] .'=>'. $_option['value']."<br/>";
// Do your further logic here
}
}
Already replied at
Display Magento Custom Option Values in Shoping Cart
You can get only selected options from using this function $this->getOptionList(), but if you actually want to retrieve all custom options then first of all you need to retrieve product id
You can get product like this on cart page
$_item = $this->getItem();
$_item->getProduct();
This will retrieve product from this you can get product and load product.
$product = Mage::getModel('catalog/product')->load($_item->getProduct()->getData('entity_id'));
$product = Mage::getModel('catalog/product')->load($product);
foreach ($product->getOptions() as $o) {
print_r($o); // show all product options
}
}

How to display product on home page category wise which is checked(custom attribute) in magento backend admin

How can we display new products on home page category wise which are selected using custom attribute "Show"(drop down or check box) in magento product list grid or product upload page?
To find product for a particular attribute in category, use the following code:
$id=Mage::app()->getRequest()->getParam('id', false);
$_products=Mage::getModel('catalog/category')->load($id);
$_productcollection=$_products->getProductCollection();
foreach($_productcollection->getAllIds() as $_productid)
{
$_product=Mage::getModel('catalog/product')->load($_productid);
//print $_product->getName().$_product->getShowfront()."<br/>";
$showfnt=$_product->getShowfront();
if($showfnt==1)
{
echo $_product->getname()."<br/>";
/* write here you grid or list code for display product */
}
}
In the above code I created showfront attribute so you write your attribute name there.

Magento - No price in product page

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.

Magento Auto Add Items Based on Quantity

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.

Resources