altering order total in the commerce cart - cart

I am Trying this to change price in the cart:
function mymodule_commerce_cart_line_item_refresh($line_item, $order_wrapper){
if($line_item->type == 'product') {
$line_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
$entity = $commerce_product = $line_wrapper->commerce_product->value();
$product_wrapper = entity_metadata_wrapper('commerce_product', $entity);
$cprice = $product_wrapper->field_totalprice->value();
$cprice *=100;
$line_item->commerce_unit_price[LANGUAGE_NONE]['0']['amount'] = $cprice;
// Alter the base_price component.
$line_item->commerce_unit_price[LANGUAGE_NONE]['0']['data']['components']['0']['price']['amount'] = $cprice;
$line_wrapper->save();
}
}
After "Add to cart" cart total sum = base price, then after the second refresh admin/commerce/orders/carts price becomes = field_totalprice and after the third refresh cart total in the page changes to field_totalprice.
How can I change price with the "Add to cart" button?

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();
};
});

Magento: Addon Products

I'm looking to create "Addon Products" in Magento. What I mean is: If the product were a Greeting Card, and you were to add that to your cart, you might be presented with options on the checkout page to add an "Envelope" or "Stamps". These would be separate products with their own prices, however, they would not be available otherwise in the store. In other words, some sort of "Linked / Child Products" that only become available once the "Parent" product has been added.
Does this sort of product configuration exist within Magento? Doesn't seem to be a Bundled Product.
Yes Magento does this out of the box.
Against each product assign 'cross sell' products, using the cross sell tab on the left. When you first get to it, if the list is empty press the 'reset filter' button to show all the products in the store. Find the ones you want and put a tick next to them, then click save.
The block you are after is;
<block type="checkout/cart_crosssell" name="checkout.cart.crosssell" as="crosssell" template="checkout/cart/crosssell.phtml"/>
Which most template load within "content" in the layout/checkout.xml layout file of your theme,
<checkout_cart_index translate="label">
////
<reference name="content">
///// HERE
</reference>
/////
</checkout_cart_index>
And then add this to the checkout/cart.phtml template (if it's not there already);
<?php echo $this->getChildHtml('crosssell') ?>
But you can add it to wherever you wish.
To handle these products not appearing elsewhere, set their visibility to 'catalog' and either put them in a category that isnt visible or dont add them to a category.
TO ANSWER YOUR SECOND QUESTION...
You asked how you could remove all 'add on' items from the basket if there are no 'main product' items in the basket. Here is a quick solution that will do this.
Create a custom select product attribute, give it ID code 'product_type_var' and give it 2 options 'Main Product' and 'Addon Product'. Add it to your product attribute set and set the values against the appropriate products.
You can then run the following code against the basket. Ideally you would create a module with an event observer - but for the sake of this example, you could also place this code at the top of
app/design/frontend/XXX/YYY/template/checkout/cart.phtml
Here's the code;
$quote = Mage::getSingleton('checkout/session')->getQuote();
$needsAction = true;
$toRemove = array();
foreach ($quote->getAllItems() as $item) {
$product = $item->getProduct();
$productLoad = Mage::getModel('catalog/product')->load($product->getId());
$customVariable = $productLoad->getResource()->getAttribute('product_type_var')->getFrontend()->getValue($productLoad);
if($customVariable == 'Main Product') {
$needsAction = false;
break; // No need to do anything
}
if($customVariable == 'Addon Product') {
$toRemove[] = $productLoad->getId(); // Build list of addon IDs
}
}
if($needsAction && (!empty($toRemove))) {
// There are no Main Products and 1 or more Addons
foreach($toRemove as $removeId) {
$quote->removeItem($removeId)->save();
}
}
Revision 3
To make sure any 'addon' products only remain in the cart if they relate to a specific 'main product' found in the cart, try this;
$quote = Mage::getSingleton('checkout/session')->getQuote();
$allowedUpsells = array();
$upsellsInCart = array();
$allIdsInCart = array();
foreach ($quote->getAllItems() as $item) {
$product = $item->getProduct();
$productLoad = Mage::getModel('catalog/product')->load($product->getId());
$customVariable = $productLoad->getResource()->getAttribute('product_type_var')->getFrontend()->getValue($productLoad);
if($customVariable == 'Main Product') {
$allIdsInCart[] = $productLoad->getId(); // Build list of all products in the cart
$upsells = $productLoad->getUpSellProductCollection(); // Get this products available upsells
foreach($upsells as $upsell) {
$allowedUpsells[] = $upsell->getId(); // Build full list of allowed addon IDs
}
}
if($customVariable == 'Addon Product') {
$allIdsInCart[] = $productLoad->getId(); // Build list of all products in the cart
$upsellsInCart[] = $productLoad->getId(); //Build full list of addon IDs
}
}
if(!empty($upsellsInCart)) { // Upsells might need attention
$allowedVsInCart = array_intersect($allowedUpsells, $allIdsInCart); // Remove other upsells that are avaiable to the product but not in the cart
$toBeRemoved = array_diff_assoc($allowedVsInCart, $upsellsInCart); // Now find the products in the cart that shouldnt be
if(!empty($toBeRemoved)) {
foreach($toBeRemoved as $removeId) {
$quote->removeItem($removeId)->save();
}
}
}

magento show all discount coupons in shopping cart

<?php $rulesCollection = Mage::getModel('salesrule/rule')->getCollection();
$i=1;
$items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
$totalPrice = 0;
foreach($items as $item) {
$totalPrice+=$item->getPrice();
}
$coll = Mage::getResourceModel('salesrule/rule_collection')->load();
echo '<div class=vip_test>';
foreach($coll as $rule){
$productDetail = $rule->afterLoad();
$discountAmount = $productDetail['discount_amount'];
echo "<div class=ssk><span> You Save : </span>".$totalAmount = $totalPrice-($discountAmount/100*$totalPrice)."</div>";
$ruleID = $productDetail['rule_id'];
}
foreach($rulesCollection as $rule){
$coupon = $rule->getCode();
$couponName = $rule->getName()
?>
In Magento, with this I am getting only the coupon code and coupon name. I want to display if there is any coupon code for particular product in shopping cart, in front of coupon - its showing the saving amount. Is it possible or not? Am I doing Wrong? please help me
First you need to get the collection of products which are in your cart. Then you need to check if there are any active coupons in catalog price rules or shopping cart price rules for the products which are in your cart. If all the above conditions becomes true then you need to display the coupon code of it.
On the next side, you need to calculate the difference amount of it and also you need to display if the user increases his quantity then the savings amount value also has to be increased.

How to get preconfigure price from product view page to listing page in magento

Recently i came across the issue is When i display price of bundle product on listing page which shows either lowest price or highest price total by making total of all product within a bundle.
As i have set few product to default selected within a group so, on the product view page that default poduct price from perticualar group has been calculated in final total. but on price of product listing page count the minimum amount from the group of product.
So, what happens that customer view the product detail from product listing where it shows the lowest price but, on product view page it shows different price because now it counts default product price instead of minimum price from the group.
I want to display pre-configured product price from view page to product listing page.
Thanks in advance!
// load product
$product = new Mage_Catalog_Model_Product();
$product->load(165);
$priceModel = $product->getPriceModel();
// get options
$block = Mage::getSingleton('core/layout')->createBlock('bundle/catalog_product_view_type_bundle');
$options = $block->setProduct($product)->getOptions();
$price = 0;
foreach ($options as $option) {
$selection = $option->getDefaultSelection();
if ($selection === null) {
continue;
}
$price += $priceModel->getSelectionPreFinalPrice($product, $selection, $selection->getSelectionQty());
}

{Magento} Product page and Add To Cart

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

Resources