How to get .on("change") to work with gldatepicker? - javascript

I have a date select box that when a date is selected, the date will be used in a calculation.
<? echo $this->Form->input( 'start_date', array( 'label' => __('Show only accounts made since:'), 'value' => '', 'readonly' => 'readonly' ) ); ?>
The Javascript I tried:
$('#start_date').glDatePicker({ cssName: 'darkneon'});
$("#start_date").on("change",function() {
var date = this.value;
console.log (date);
do_stuff(date);
});
The console shows nothing when I select a date. I am clearly missing something. Is on-change even the right way to handle this?

Try this:
$('#start_date').glDatePicker({
cssName: 'darkneon',
onClick: function(target, cell, date, data) {
console.log (date);
do_stuff(date);
}
);
From the docs (find with Ctrl+f):
Callback that will trigger when the user clicks a selectable date.
Example (check example #3)

Related

How to use multiple filtering options that will affect the price in Woocommerce simple products?

I am using "Add a select field that will change price in Woocommerce simple products" answer code. It only uses a single selection field for filtering. I want to extend this code using jQuery to be able to use multiple fields for filtering.
Example: 2 select fields, one text, a checkbox and some radio buttons that will all determine the final calculated price.
// Select field
woocommerce_form_field('materials_pack', array(
'type' => 'select',
'class' => array('material-field form-row-wide'),
'label' => __('Select Materials:', $domain),
'required' => true,
'options' => $options,
),'');
<script>
jQuery(function($){
var a = <?php echo json_encode($prices); ?>,
b = 'p.price',
c = 'select[name="materials_pack"]';
$(c).on( 'change', function(){
$.each( a, function( key, value ){
if( $(c).val() == key )
$(b).html(value);
});
});
});
</script>
How to use it for multi fields? and how do the jquery script?
Ok i found my answer here now i just need to how to do it in this code?
add_action('woocommerce_before_calculate_totals', 'set_cutom_cart_item_price', 20, 1);
foreach ( $cart->get_cart() as $cart_item ) {
if ( isset( $cart_item['material_data']['new_price'] ) ){
$cart_item['data']->set_price( $cart_item['material_data']['new_price'] );
}
}

laravel getting error on empty request

I get error below while trying to add item to my cart:
Darryldecode \ Cart \ Exceptions \ InvalidItemException
validation.numeric
The error comes from this part of my code:
$customAttributes = [];
if(!empty($request->attr)){
foreach($request->attr as $sub) {
// find the suboption
$sub = Suboption::find($sub);
if (!empty($sub->id)) {
$itemCondition1 = new \Darryldecode\Cart\CartCondition(array(
'name' => $sub->title,
'value' => $sub->price,
'type' => 'additional',
'target' => 'item',
));
array_push($customAttributes, $itemCondition1);
}
}
}
and it take place in here:
Cart::add(array(
'id' => $product->id,
'name' => $product->title,
'price' => $price,
'quantity' => $request->input('quantity'),
'attributes' => $weightArray,
'conditions' => $customAttributes, //here
));
The $customAttributes code supposed to get data IF product does have those information And user chose any of it and suppose to ignore if product doesn't have any of those info or user didn't chose any of it.
Issue is
The code expect data no matter what, product does have that info or not, user selected any or not, even if user select that data still i get error above.
Demo
https://imgur.com/a/KHJqp
any idea why is that?
UPDATE
I figured my issue comes from 'price' => $price, in my add method where i get my $price like:
$price = $product->discounts;
if($price->count() > 0 ) {
foreach($discounts as $disc){
if($disc->value_to >= $mytime) {
$price = $product->price - $disc->amount;
}
}
}else{
$price = $product->price;
}
this part supposed to get product price if there is no discount, and get discounted price if there is.
How I get to this line of code? here is it
SOLVED
I used hidden field and got my data from front-end instead of controller method.

Why isn't AJAX $.post working in wordpress?

I've been killing myself trying to understand why $.post isn't working..
I'm trying to make two drops downs..
The first will contain the main category and the second drop down contains the sub categories for the first drop down..
[MAIN CATEGORY DROP DOWN] [SUB CATEGORY DROP DOWN]
The first drop down works flawlessly, but the sub categories just isn't fetching with ajax for some reason why I do not know..
add_shortcode( 'custom_query', 'custom_query' );
add_action( 'wp_ajax_custom_query', 'getSubCategory' );
add_action( 'wp_ajax_nopriv_custom_query', 'getSubCategory' );
function custom_query( $cat_id )
{
// first dropdown
$first_dropdown = array( 'show_option_all' => '','show_option_none' => 'Select main category','orderby' => 'ID','order' => 'ASC','show_count' => 0, 'hide_empty' => 0, 'exclude' => 0,'echo' => 1,'selected' => 0,'child_of' => $cat_id,'hierarchical' => 1,'name' => 'chained-categories','id' => '','class' => 'postform', 'depth' => 1, 'tab_index' => 0,'taxonomy' => 'category', 'hide_if_empty' => false );
wp_dropdown_categories( $first_dropdown );
// subcategories empty but will be populated once the first main category is selected
echo '<div id="chained-subcontainer">
<select name="chained-subcategories" id="chained-subcategories">
<option value="">- Select a category first -</option>
</select>
</div>';
// javascript
echo '
<script type="text/javascript">
(function($)
{
var ajaxurl = "../../wp-admin/admin-ajax.php";
var subdata = { action: "custom_query" };
$("#chained-categories").change(function()
{
subdata[ "chained_subcat_id" ] = $( this ).val();
subdata[ "chained_subcat_name" ] = $("#chained-categories option[value="+ $( this ).val() + "]").text();
if( $( this ).val() > 0 )
{
#this here works once the first cat is picked
alert($("#chained-categories option:selected").val());
$.post
(
ajaxurl,
subdata,
function( response )
{
$( "#chained-subcontainer" ).html( response );
}
);
} else {
$( "#chained-subcontainer" ).html( "<select name="chained-subcategories" id="chained-subcategories"><option value="">- Select a category first -</option></select>" );
}
});
})(jQuery);
</script>
';
}
function getSubCategory()
{
$second_dropdown = array('show_option_all' => '', 'show_option_none' => 'Select subcategory','orderby' => 'ID', 'order' => 'ASC', 'show_count' => 0, 'hide_empty' => 0, 'exclude' => 0, 'echo' => 1, 'selected' => 0, 'child_of' => $_POST[ 'chained_subcat_id' ], 'hierarchical' => 1, 'name' => 'chained-subcontainer', 'id' => '', 'class' => 'postform','depth' => 1, 'tab_index' => 0, 'taxonomy' => 'category', 'hide_if_empty' => false );
wp_dropdown_categories( $second_dropdown );
}
I know for a fact that it's the $.post part that isn't working because I included an alert for once any option is selected in the first drop down it would show the value of it.. and it works, as long as the $.post part of the code isn't included..
EDIT: So after david's suggestion, I was able to check some piece of the code, while everything works just as expected.. as far as ajax working.. I've got a small problem..
In this line of code,
else {
$("#chained-subcontainer").html("<select name="chained-subcategories" id="chained-subcategories"><option value="">Select a category first</option></select>");
}
If I leave that on, the code does not work as far as ajax.. if I remove that part, then ajax fetches the data correctly.. and I can't seem to understand why..this is what I get from JSHINT "Expected ')' and instead saw 'chained'."
If I leave that on, the code does not work as far as ajax
That's because you have a syntax error, so none of the JavaScript is going to work.
Notice the syntax highlighting here on Stack Overflow:
$( "#chained-subcontainer" ).html( "<select name="chained-subcategories" id="chained-subcategories"><option value="">- Select a category first -</option></select>" );
^--- here
In this case, the easiest solution is simply to use single-quotes for your strings. Since your strings don't themselves contain single-quotes (but do dontain double-quotes):
$( '#chained-subcontainer' ).html( '<select name="chained-subcategories" id="chained-subcategories"><option value="">- Select a category first -</option></select>' );
Any time a string contains a quote character, you have to ensure that the parser isn't going to interpret that character as being the end of the string. Either by using different kinds of quotes or by "escaping" those quote characters.

CakePHP Drop Down Form Value Not Being Sent to Controller

I have a form with four fields - three of which are properly being sent up to the controller. One field is a drop-down selector field and it is not being sent. Here is sample form code from my schedule_date field which is being sent properly:
<?php
echo $this->Form->input("schedule_date"
,array('label' => false
,'type' => 'text'
,'class' => 'step_dates'
,'readOnly' => 'readOnly'
)
);
?>
And here is the code for the drop-down box which is NOT being sent properly:
<?php
echo $this->Form->input("project_step_type_id"
,array('label' => false
,'id' => 'project_step_type_id'
,'class' => 'project_step_type_id'
,'empty' => '( select step )'
,'options' => $project_step_types
)
);?>
Here is what my debug looks like on the form data I'm sending back (notice project_step_type_id is missing):
<pre class="cake-debug">
array(
'user_id' => '402',
'trainer_user_id' => '524',
'schedule_date' => '2014-11-26'
)
</pre>
Can anyone help? All fields are inside the form tag. I believe the issue is with this line:
request_data=$('#my_form input').serializeCakeArray();
It's not getting the "select" fields, only the "input" fields. Does anyone know how to fix that? Thank you.
The answer is:
request_data=$('#my_form input, #my_form select').serializeCakeArray();

Yii- How can I make the second dropdownlist values displayed

I'm new to Yii framework and Javascript. Now, I'm using two dropdownlists for min_cost and max_cost. I'm using the below code to list the values in the dropdownlist.
<?php
$this->widget('ext.combobox.EJuiComboBox', array(
'model' => $model,
'attribute' => 'min_cost',
'data' => Yii::app()->params['cost_min_resales'],
'options' => array(
'onSelect' => 'cost_change(item.value);',
'allowText' => false,
),
'htmlOptions' => array('placeholder' => 'Min Cost', 'style'=>'width:70px'),
));
?>
</br>
<?php
$this->widget('ext.combobox.EJuiComboBox', array(
'model' => $model,
'attribute' => 'max_cost',
'data' =>Yii::app()->params['cost_max_resales'],
'options' => array(
'allowText' => false,
),
'htmlOptions' => array('placeholder' => 'Max Cost', 'style'=>'width:70px'),
));
?>
For this I'm using the below script:
<script>
function cost_change(price) {
var removed;
console.log("removed", removed);
select = "#SearchForm_max_cost_select";
var value = price;
console.log("value", value);
jQuery('#SearchForm_max_cost_select').prepend(removed);
var toKeep = jQuery('#SearchForm_max_cost_select option').filter(function() {
return parseInt(this.value) > parseInt(value);
});
console.log("to keep",toKeep);
removed = jQuery('#SearchForm_max_cost_select option').filter(function() {
return parseInt(this.value) < parseInt(value);
});
}
</script>
Now, I want to populate the second dropdownlist with values greater than the value selected in first one. Suppose there are values hardcoded in an array as (1,2,3,4,5) I select 2 from first dropdownlist then all values greater than 2 (i.e 3,4,5) should be listed in second dropdownlist.
The above script works fine. I can see the values of second dropdownlist printed in the logs . But, I'm not able to pass this values to second dropdownlist. How can I do this.
Edit :
Below is the image of the object I'm getting when I select value .
this will work, actually i was also facing the same problem but somehow figured it out.
In your controller just check for this condition..
if ($model->"yourdbid" == null) {
$model->"yourdbid" = $_POST['yourdbid'];
}

Categories

Resources