Configure Cart Price Update - php

I want to create a product ordering page that has tons of options. Each option changes the price around a little bit, and I want the total price auto-update whenever any option changes.
All options are Drop-down menus so the prices would need to change when they select an option from a drop down.
I'm open to everything, but simple code on one page would be best. I want to be able to edit the prices as a part of the rest of the product page as there is only one product.
Any advice would be appreciated.

Try binding a jQuery/javascript function that calculates the sum of all price input fields and prints it at the bottom to the dropdowns' onchange events like so. The HTML I'm giving you is just a mockup, feel free to change it and the jQuery references to it as you wish.
HTML:
<p>Computer base price: $<span id="baseCost"></span></p>
<p>Select your upgrades:</p>
<form id="options">
<select name="processor" onchange="calculateTotals()">
<option value="Base_0">Base processor ($0)</option>
<option value="Processor1_100">Processor 1 ($100)</option>
<option value="Processor2_500">Processor 2 ($500)</option>
</select>
<select name="hard-drive" onchange="calculateTotals()">
<option value="Base_0">Base hard-drive ($0)</option>
<option value="7200rpm_250">7200rpm hard-drive ($250)</option>
</select>
</form>
<p>Upgrade total: $<span id="upgradeCost">0</span></p><br>
<p>Final total: $<span id="sumTotal"></span></p>​
Javascript:
$(document).ready(function(){
var basePrice = 2000;
$("#baseCost").text(basePrice);
$("#sumTotal").text(basePrice);
});
function calculateTotals(){
var basePrice = parseInt($("#baseCost").text(), 10);
var upgradePrice = 0;
$("#options select").each(function(){
var optionVal = $(this).val();
upgradePrice += parseInt(optionVal.substr(optionVal.indexOf("_") + 1, optionVal.length - 1), 10);
});
$("#upgradeCost").text(upgradePrice);
$("#sumTotal").text(basePrice + upgradePrice);
}​
WORKING DEMO
As you can see, the option value attributes include both the option ID and its price, separated by an underscore. This allows the Javascript and your form handling code to get both the selected options and their prices.

Related

How to update db with the multiple select option values: Wordpress

I am having a custom table that has several items and has a column of status. I have created a dropdown using select for changing the status.
Here, what I want to do is, on change of the status select option, the value of status of that particular row should be updated in the db.
I am able to get the value of the select using jquery but I am not sure how to update the table when multiple dropdowns are being selected together.
I am having the below select options,
<select id ="update-statusDropDown">
<option name="waiting" value="waiting">Waiting</option>
<option name="due" value="due">Due Diligence</option>
<option name="escrow" value="escrow">Escrow</option>
<option name="inspection" value="inspection">Inspection</option>
<option name="closed" value="closed">Closed</option>
</select>
My jQuery is something like below,
jQuery(document).ready(function () {
jQuery('select#update-statusDropDown').change(function () {
//Selected value
var inputValue = $(this).val();
alert("value in js " + inputValue);
//Ajax for calling php function
jQuery.post('update-listing-status.php', {dropdownValue: inputValue}, function (data) {
alert('ajax completed. Response: ' + data);
//do after submission operation in DOM
});
});
});
I want to update status value in the table as per the 'inputValue' from the dropdown. Also, if multiple dropdowns are selected together, how can I update all the values together.
Please can anyone help?
The screenshot of my current table is attached.
You have registered the change event of the select using the ID of that drop down which means only the drop down with that ID will trigger the request you making via the jquery.post
instead use class attribute for the select elements and register the change event on that class
now to get the unique element you can use the data attribute of that select element and option for example data-tableid="something"
in this case you can register all change event of all select elements and be able to extract the values that is only unique to the given table or column name.
$('.class-name').on( 'change' , function(){
// Get the Select itself
var me = $(this);
var tableid = me.data('tableid');
var something = $(this).find(':selected').data('something');
console.log( tableid );
console.log( something );
} );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="class-name" data-tableid="table-id">
<option value="the value" data-something="some value 2">The Title 2 </option>
<option value="the value" data-something="some value">The Title</option>
</select>
So now since you have the ability to uniquely identify which select is being used and which option is being used you can make the request as you wish.

Updating the price on product page when variations are chosen with WooCommerce

I'm having a couple of issues with displaying an updated price for a product on the product page itself using WooCommerce.
Basically we have a custom builder page that allows the user to select different options (or variations in this case) and then when they add it to their cart the correct price is calculated. That all works fine.
The problem I'm having is that we want to display the price on screen as they change their options around.
So, let's say I've picked a Blue Item that is 6 meters long, the price should be £100, but if I choose the Red Item that is 1 meter long, the price is £10.
I'm guessing a bit of jQuery or something could dynamically update the page, but I'm at a loss to know where or how I do this.
It would need to fire a function I'm assuming on change of a form select box.
Currently, I'm displaying the price using this...
$product = new WC_Product(538);
echo esc_attr($product->get_price());
I think that's wrong anyway as that's the base price rather than the variation price, but either way, I still need to find a way to update the price on the screen WITHOUT refreshing.
Here is one of my SELECT form items, though there are quite a few of these on the page.
<select id="frame-depth" class="kad-select" name="attribute_frame-depth" data-attribute_name="attribute_frame-depth"" data-show_option_none="yes">
<option value="Standard depth" selected="selected">Standard depth</option>
<option value="Extra Deep" >Extra Deep</option>
</select>
Any help would be greatly appreciated!
If I need to update the question with any more detail, I can do so. I just thought I'd keep it simple!
Simon
I am doing it roughly like this in JS, making use of the found_variation event:
var currentVariation = null;
function calcTotalPrice(variation = null) {
if (variation) {
currentVariation = variation;
}
if (!currentVariation) {
var $price = $('#product-price');
if ($price && $price.length) {
currentVariation = {
display_price: $price.val()
};
}
}
if (currentVariation) {
var qty = $('#quantity').val();
var total = currentVariation.display_price * qty;
$('#total-price-result').html('€ ' + number_format(total, 2, ',', ' '));
}
}
$('form.variations_form').on('found_variation', function(e, variation) {
calcTotalPrice(variation);
});
$('#quantity').on('input change', function() {
calcTotalPrice();
});
$('form.variations_form').on('hide_variation', function() {
$('#total-price-div').hide();
});
$('form.variations_form').on('show_variation', function() {
$('#total-price-div').show();
});
And on the HTML/template side I override woocommerce/single-product/add-to-cart/variable.php and edit the pricing part to be similar to this:
<div id="total-price-div">
<h4><label>{{ __("Total", "my-text-domain") }}</label></h4>
<div class="value">
<span class="total-price"><span id="total-price-result" class="total-price"></span></span>
</div>
</div>
I hope this can help someone?

how to save all dropdowns values from php foreach loop in local storage?

In my cart i have my products added, there are stored in sessions.
I want to store my selected option from all dropdown when the page is refreshed.
I need to refresh my page so my sessions can be updated so i can post in my database all the updated values.
What is wrong...
if i select an option for the first row of my product it saves in local storage.but when i select another product option from other row,it overwrites the local storage,so my local storage is saving only one option,and when selected other option from other products it is rewriting my only one save option in local storage.i have to save multiple option.
Without refresh what happens is...
lets say that i've selected 1 cushion in my gallery.
So in my cart this cushion will be 1 product, and if i add two more by clicking plus button and then click on confirm order,it will post in my DB the value of 1.
But not 3.
So my page needs to refresh, so for that i need to save all dropdown selection so i can refresh the page.
So far i tried to save it,but it saves the first row of my cart.
This is what i tried...
$(function() {
if (localStorage.getItem('fabric')) {
$(".fabric option").eq(localStorage.getItem('fabric')).prop('selected', true);
}
$(".fabric").on('change', function() {
localStorage.setItem('fabric', $('option:selected', this).index());
});
});
$(function() {
if (localStorage.getItem('size')) {
$(".size option").eq(localStorage.getItem('size')).prop('selected', true);
}
$(".size").on('change', function() {
localStorage.setItem('size', $('option:selected', this).index());
});
});
this is my foreach loop if needed to understand it better.
this script below is not important for this question,but it shows how i am handling my dropdowns to make ajax get values based on dropdown selection using data attribute.
script that gets cost and subtotal
Ok... Took a liitle time, but you will like my solution (I think).
We have to set storage row by row...
So an .each() loop has to be done on product rows.
We use the index of the .each() as a part of the storage name to ensure no overwriting.
Given this HTML that I made just for this example:
<div class="row">
<!-- other elements like img etc... -->
<select class="fabric">
<option>jeans</option>
<option>leather</option>
<option>cotton</option>
</select>
<select class="size">
<option>small</option>
<option>medium</option>
<option>large</option>
</select>
</div>
<div class="row">
<!-- other elements like img etc... -->
<select class="fabric">
<option>jeans</option>
<option>leather</option>
<option>cotton</option>
</select>
<select class="size">
<option>small</option>
<option>medium</option>
<option>large</option>
</select>
</div>
Here is the script:
$(function() {
$(".row").each(function(index){
// Fabric selection
if (localStorage.getItem('row_'+index+'_fabric')) {
$(this).find('.fabric option').prop('selected', false).eq(localStorage.getItem('row_'+index+'_fabric')).prop('selected', true);
console.log("Row#"+index+" get-fabric: "+localStorage.getItem('row_'+index+'_fabric'));
}
$(this).find(".fabric").on('change', function() {
localStorage.setItem('row_'+index+'_fabric', $(this).find('option:selected').index());
console.log("Row#"+index+" set-fabric: "+$(this).find('option:selected').index());
});
// Size selection
if (localStorage.getItem('row_'+index+'_size')) {
$(this).find('.size option').prop('selected', false).eq(localStorage.getItem('row_'+index+'_size')).prop('selected', true);
console.log("Row#"+index+" get-size: "+localStorage.getItem('row_'+index+'_size'));
}
$(this).find(".size").on('change', function() {
localStorage.setItem('row_'+index+'_size', $(this).find('option:selected').index());
console.log("Row#"+index+" set size: "+$(this).find('option:selected').index());
});
});
});
Try it on this CodePen!
(Change the selects and hit "Run" to refresh)

simple multiple selection ng-selected with array not working

This is my category list
$scope.categories= [{"category_id":"1","category_name":"sports"},{"category_id":"2","category_name":"casual"},{"category_id"
:"3","category_name":"formal"},{"category_id":"4","category_name":"party wear"},{"category_id":"5","category_name"
:"winter"},{"category_id":"9","category_name":"summer"}]
The product can have multiple categories.
$scope.product_categories=[{"category_id":"3"},{"category_id":"4"},{"category_id":"5"}]
I have these two array first array categories holds all the category.
and second array holds the category which that product has.
I have select tag where all the categories are listed at the time of adding the product.user can select multiple product.
{{category.category_name}}
Now suppose I have added one product with 3 category 3,4,5 respectively.
When I trying to edit that product these 3,4,5 category must be selected because this product is related with these category. So this is my code which is not working.
<select multiple="" class="form-control" name="category_list[]" ng-model="categories" >
<option ng-selected="{{category.category_id == product_categories}}"
ng-repeat="category in categories"
value="{{category.category_id}}">
{{category.category_name}}
</option>
I am confused here how to do the multiple selection when I have array with array.Categories 3,4,5 must be selected among the category.if id do
ng-selected="{{category.category_id ==5}}" like this only 5 category is get selected.how to do the multiple selection with array or multiple values?
I have a solution, use $scope.$watch
$scope.$watch('product_categories', function (nowSelected) {
$scope.selectedValues = [];
if (!nowSelected) {
// here we've initialized selected already
// but sometimes that's not the case
// then we get null or undefined
return;
}
angular.forEach(nowSelected, function (val) {
$scope.selectedValues.push(val.category_id.toString());
});
});
And apply selectedValues via following:
<select multiple ng-model="selectedValues" style="width: 50%;" size="7">
<option ng-repeat="category in categories" value="{{category.category_id}}" ng-selected="{{selectedValues.indexOf(category.category_id.toString())!=-1}}">{{category.category_name}}</option>
</select>
==> Full code at Plunker multi selection ng-selected
Hope it helps!
you forgot to write the ng-app="" in your code ...
something like this !
<div ng-app="">
<select multiple="" class="form-control" name="category_list[]" ng-model="categories" >
<option ng-selected="{{category.category_id == product_categories}}"
ng-repeat="category in categories"
value="{{category.category_id}}">
{{category.category_name}}
</option>
</div>
I have added
$watch for the multiple selection.
angular.module("myApp.controllers",[])
.controller("product",function($scope){
$scope.categories= [{"category_id":"1","category_name":"sports"},{"category_id":"2","category_name":"casual"},{"category_id"
:"3","category_name":"formal"},{"category_id":"4","category_name":"party wear"},{"category_id":"5","category_name"
:"winter"},{"category_id":"9","category_name":"summer"}]
$scope.product_categories=[{"category_id":"3"},{"category_id":"4"},{"category_id":"5"}]
$scope.$watch('product_categories', function(nowSelected){
$scope.selectedValues = [];
if( ! nowSelected ){
return;
}
angular.forEach(nowSelected, function(val){
$scope.selectedValues.push( val.category_id.toString() );
});
});
});
});
and I made some changes in my view part
<select class="form-control" name="category_list[]" multiple ng-model="selectedValues" >
<option ng-repeat="category in categories"
value="{{category.category_id}}">
{{category.category_name}}
</option>
</select>
I put all the categories in selectedValues and assigned to select tag as ng model with multiple selection. After this change It works for me.

Set select options dynamically in catalog product page in Magento Admin panel

Need to have a dynamic set of values in an select attibute, depending upon another select attribute.
e.g. there will be two dropdown attributes 1. parent dropdown, 2. child dropdown
if "A" is selected in parent dropdown then "Air","Apple","Ant" will be shown in dropdown.
if "B" is selected in parent attribute then "Ball", "Box", "Base" will be shown.
So basically values of child dropdown will be depended upon the selected value of parent dropdown.
I want to make it dynamic as options can be saved under attributes and those values are to shown in Catalog Product Edit page.
Thanks in advance.
try the below code if you have data inside select box in object or array in JS then you can filter it out easily and append it to select box
Here's Demo DEMO
var data = {
"A": ["Air", "Apple", "Ant"],
"B": ["Water", "Mango", "Fly"]
}
jQuery('#parent').on('change', function() {
var tempData = data[this.value];
var selectChild = jQuery('#child');
jQuery('option', selectChild).remove();
for (var i = 0; i < tempData.length; i++) {
var option = new Option(tempData[i], tempData[i]);
selectChild.append(jQuery(option));
}
});
<select id="parent">
<option value="">Select Parent</option>
<option value="A">A</option>
<option value="B">B</option>
</select>
<select id="child">
<option value="">Select Child</option>
</select>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>

Categories