Please help me to correct the code. Where it is conflicting with other javascript action. This price filter in opencart is stopping the action of Add to cart in category page. following is the code where php is called inside the javascript. Something is missing or not properly coded.
<div class="row">
<div class="slide-filter">
<h2><?php echo $heading_title; ?></h2>
<div class="list-group">
<div class="list-group-item">
<div id="filter-group1">
<div id="scale-slider"></div>
<button type="button" id="button-price-filter" class="filter-button"><?php echo $button_filter; ?></button>
</div>
</div>
</div>
</div>
</div>
and the javascript is
$("#scale-slider")
.slider({
min: <?php echo round($price_range_min) ; ?>,
max: <?php echo round($price_range_max) ; ?>,
range: true,
values: [<?php echo (isset($price_range[0])?$price_range[0]:0); ?>, <?php echo (isset($price_range[1])?$price_range[1]:$price_range_max); ?>]
})
.slider("pips", {
rest: false,
<?php if (!$right_code) { ?>
prefix: "<?php echo $price_code; ?>",
<?php } else { ?>
suffix: "<?php echo $price_code; ?>"
<?php } ?>
})
.slider("float");
(function( $ ) {
$(function() {
$('#button-price-filter').on('click', function() {
priceRange = [];
$('#scale-slider .ui-slider-tip').each(function(){
priceRange.push($(this).html());
});
$('.<?php echo $product_class; ?>').hide();
$('.clearfix').remove();
$('.<?php echo $product_class; ?>').each(function(){
if( $(this).find( ".price span.price-new" ).length ) {
var price = $(this).find( ".price span.price-new" ).html().replace('<?php echo $price_code; ?>','').replace(',','');
} else {
var text = $(this).find('.price').html().replace('<?php echo $price_code; ?>','');
if( $(this).find( ".price span" ).length ) {
var price = text.substring(0,text.indexOf('<span')).replace(',','');
} else {
var price = text.replace(',','');
}
}
price = parseInt(price);
if( !isNaN(price) && (price > priceRange[0] && price < priceRange[1]) ){
$(this).fadeIn("slow");
}
});
});
});
})(jQuery);
remove the $('.clearfix').remove(); ,may be work perfectly
Related
Actually my data is getting stored in backend but I'm not able to display it in frontend , also i want to add multiple images in post type wordpress plugin
You can follow the code:
//Add meta box in the post or page
add_action( 'add_meta_boxes', 'custom_postpage_meta_box' );
function custom_postpage_meta_box(){
$post_types = array('post');
foreach($post_types as $pt){
add_meta_box('custom_postpage_meta_box',__( 'More Featured Images', 'textdomain'),'custom_postpage_meta_box_func',$pt,'side','low');
}
}
//Load the field in meta screen
function custom_postpage_meta_box_func($post){
$meta_keys = array('second_featured_image');
foreach($meta_keys as $meta_key){
$image_meta_val=get_post_meta( $post->ID, $meta_key, true);
?>
<div class="custom_postpage_wrapper" id="<?php echo $meta_key; ?>_wrapper" style="margin-bottom:20px;">
<img onclick="custom_postpage_add_image('<?php echo $meta_key; ?>');" src="<?php%20echo%20(%24image_meta_val!=''?wp_get_attachment_image_src(%20%24image_meta_val)%5B0%5D:'');%20?>" style="width:100%;cursor:pointer;display: <?php echo ($image_meta_val!=''?'block':'none'); ?>" alt="">
<a class="addimage" style="cursor:pointer;" onclick="custom_postpage_add_image('<?php echo $meta_key; ?>');"><?php _e('Set featured image','textdomain'); ?></a><br>
<a class="removeimage" style="cursor:pointer;display: <?php echo ($image_meta_val!=''?'block':'none'); ?>" onclick="custom_postpage_remove_image('<?php echo $meta_key; ?>');"><?php _e('Remove featured image','textdomain'); ?></a>
<input type="hidden" name="<?php echo $meta_key; ?>" id="<?php echo $meta_key; ?>" value="<?php echo $image_meta_val; ?>">
</div>
<?php } ?> <script>function custom_postpage_add_image(key){
var $wrapper = jQuery('#'+key+'_wrapper');
custom_postimage_uploader = wp.media.frames.file_frame = wp.media({
title: '<?php _e('select image','textdomain'); ?>',
button: {
text: '<?php _e('select image','textdomain'); ?>'
},
multiple: false
});
custom_postpage_uploader.on('select', function() {
var attachment = custom_postpage_uploader.state().get('selection').first().toJSON();
var img_url = attachment['url'];
var img_id = attachment['id'];
$wrapper.find('input#'+key).val(img_id);
$wrapper.find('img').attr('src',img_url);
$wrapper.find('img').show();
$wrapper.find('a.removeimage').show();
});
custom_postpage_uploader.on('open', function(){
var selection = custom_postpage_uploader.state().get('selection');
var selected = $wrapper.find('input#'+key).val();
if(selected){
selection.add(wp.media.attachment(selected));
}
});
custom_postpage_uploader.open();
return false;
}
function custom_postpage_remove_image(key){
var $wrapper = jQuery('#'+key+'_wrapper');
$wrapper.find('input#'+key).val('');
$wrapper.find('img').hide();
$wrapper.find('a.removeimage').hide();
return false;
}</script> <?php wp_nonce_field( 'custom_postpage_meta_box', 'custom_postpage_meta_box_nonce' );
?>
}
//Save the meta box
add_action( 'save_post', 'custom_postimage_meta_box_save' );
function custom_postimage_meta_box_save($post_id){
if ( ! current_user_can( 'edit_posts', $post_id ) ){ return 'You have no permission to edit this post types'; }
if (isset( $_POST['custom_postimage_meta_box_nonce'] ) && wp_verify_nonce($_POST['custom_postimage_meta_box_nonce'],'custom_postimage_meta_box' )){
$meta_keys = array('second_featured_image');
foreach($meta_keys as $meta_key){
if(isset($_POST[$meta_key]) && intval($_POST[$meta_key])!=''){
update_post_meta( $post_id, $meta_key, intval($_POST[$meta_key]));
}else{
update_post_meta( $post_id, $meta_key, '');
}
}
}
}
For more details please read the article https://yourblogcoach.com/upload-multiple-featured-images-in-a-post-or-page/
Thank you
I ask for your help regarding the refreshment of my DIV.
When loading my page, _promo.php is called but does not update the data of this page.
Can you help me please ?
I don't know much about javascript
<?php include($_SERVER["DOCUMENT_ROOT"]."/includes/header.php"); ?>
<?php
try {
$stat = $pdo->query( "SELECT * FROM tab_shop_produits");
$i = 0;
while ($data = $stat->fetch( PDO::FETCH_ASSOC)) {
$reference = $data["reference"];
?>
<div id="promo_<?php echo $i; ?>" class="mb-2 text-end small text-success blink_"></div>
<input type="hidden" id="txt_reference_<?php echo $i; ?>" value="<?php echo $reference; ?>">
<?php
$i++;
}
} catch(PDOException $e){
echo "<div class='alert alert-danger'>".$e->getMessage()."</div>";
}
?>
<script>
$(document).ready(function(){
function loadLog(i) {
var reference = document.getElementById("txt_reference_" + i).value;
var intTotal = parseInt('<?php echo $i; ?>');
$.ajax({
type: "POST",
url: "_promo.php",
cache: true,
data: {reference: reference},
error: function (e) {console.log('Ajax Error', e);alert('Erreur Ajax');},
success: function(html){
document.getElementById("promo_"+i).innerHTML = html;
console.log(html);
i=parseInt(i)+1;
if(i<intTotal){setTimeout(loadLog(i), 1000);}
}
});
}
setTimeout(loadLog('<?php echo $i; ?>'), 1000);
});
</script>
<?php include($_SERVER["DOCUMENT_ROOT"]."/includes/footer.php"); ?>
I'm building an online store for my sister and i'm struggling with removing specific item from cart ($_SESSION) when I click the X icon of product (onclick="").
<?php
if (empty($_SESSION['cart'])) {
$_SESSION['cart'] = array();
}
?>
<div class="cart-content d-flex">
<!-- Cart List Area -->
<div class="cart-list">
<?php
$subtotal = 0;
$livrare = 17;
$subtotal_modif = 0 . " Lei";
$object = new Produs();
$cartItems = $_SESSION['cart'];
foreach ($cartItems as $item):
$rows = $object->getRows("SELECT * FROM produs");
foreach ($rows as $row) {
//$subtotal += $row['pret_produs'];
if ($item['id'] == $row['id_produs']) {
$imagini = $object->getRows("SELECT * FROM imagini WHERE id_produs_imagine = ? LIMIT 1", [$row['id_produs']]);
$pret = $row['pret_produs'];
$pret_modif = str_replace('.', ',', $row['pret_produs']) . " LEI";
$pret_vechi = $row['pret_vechi_produs'];
$pret_redus_modif = str_replace('.', ',', $row['pret_vechi_produs']) . " LEI";
$subtotal = $subtotal + ($pret * $item['cantitate']);
$subtotal_modif = str_replace('.', ',', $subtotal) . " LEI";
?>
<!-- Single Cart Item -->
<div class="single-cart-item">
<a href="#" class="product-image">
<?php foreach ($imagini as $img) {
echo '<img src="'. $object->photoPath() . $img['nume_imagine'] .'" alt="">';
} ?>
<!-- Cart Item Desc -->
**<div class="cart-item-desc">
<span class="product-remove"><i onclick="removeItem('<?php $item['id']; ?>')" class="fa fa-close" aria-hidden="true"></i></span>**
<!-- <span class="badge">Mango</span> -->
<h6><?php echo $row['nume_produs']; ?></h6>
<p class="size">Marime: <?php echo $item['marime']; ?></p>
<p class="color">Cantitate: <?php echo $item['cantitate']; ?></p>
<p class="price"><?php echo $pret; ?></p>
</div>
</a>
</div>
<?php } }
endforeach;
?>
</div>
I'm thinking in doing something like this at the end of page but I don't know how to do it properly:
<script>
function removeItem(itemID) {
<?php unset($_SESSION['cart']['<script>itemID</script>']); ?>
}
</script>
I dont know how to combine PHP and JavaScript.
You can put this in the top of your PHP script:
if ( empty( $_SESSION['cart'] ) ) {
$_SESSION['cart'] = [];
}
if ( isset( $_POST['remove_item'] ) ) {
$itemID = $_POST['remove_item'];
if ( isset( $_SESSION['cart'][ $itemID ] ) ) {
unset( $_SESSION['cart'][ $itemID ] );
}
echo $itemID;
die();
}
// THE REST OF YOUR PHP CODE.
Give the container of the item a unique id based on the item's id:
<div class="single-cart-item" id="single-cart-item-<?php echo $item['id']; ?>">
<!-- --------------- -->
</div>
And this in your JS:
<script type="text/javascript">
function removeItem( itemID ) {
// make AJAX request to server to remove item from session.
var xhttp = new XMLHttpRequest();
xhttp.open("POST", "cart.php", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("remove_item=" + itemID);
xhttp.onreadystatechange = function() {
if (this.readyState === 4 && this.status === 200) {
var element = document.getElementById("single-cart-item-" + this.responseText);
if (element !== null) {
element.remove();
}
}
};
}
</script>
The function removeItem( itemID ) is making an AJAX call to your PHP script. It passes the item ID as a POST value. Replace cart.php with the correct path (URL to your cart page).
I want when I add a product to the cart and reload the same page, but the problem did not this product.
The controller
public function detail()
{
$data=array('title' =>'Ecommerce Online | Detail Product',
'username' => $this->session->userdata('id'),
'categories' => $this->categories_model->get_categories(),
'details' => $this->categories_model->get_details($this->uri->segment(3)),
'isi' =>'member/detail');
$this->load->view('template_home/wrapper',$data);
}
function addtocart()
{
if($this->cart_model->validate_add_cart_item() == TRUE){
if($this->input->post('ajax') != '1'){
redirect('member/detail/'); // this problem
}else{
echo 'true';
}
}
}
I add my models
function validate_add_cart_item()
{
$id = $this->input->post('product_id');
$cty = $this->input->post('quantity');
$this->db->where('productID', $id);
$query = $this->db->get('product', 1);
if($query->num_rows > 0){
foreach ($query->result() as $row)
{
$data = array(
'id' => $id,
'qty' => $cty,
'price' => $row->price,
'name' => $row->productName
);
$this->cart->insert($data);
return TRUE;
}
}else{
return FALSE;
}
}
I add my view
<?php foreach ($details as $s) { ?>
<div class="col-md-5">
<div class="box text-center">
<img src="<?php echo base_url('upload/'.$s->photo); ?>" width="150px" height="150px">
<br><?php echo $s->productName; ?>
<br><strong>Rp. <?php echo $s->price; ?></strong>
<br>
<?php echo form_open('member/add'); ?>
<fieldset>
<label>Quantity</label>
<?php echo form_input('quantity', '1', 'maxlength="2"'); ?>
<?php echo form_hidden('product_id', $s->productID); ?>
<?php echo form_submit('add', 'Add'); ?>
</fieldset>
<?php echo form_close(); ?>
</div>
</div>
<?php } ?>
Jquery script
<script type="text/javascript">
$(document).ready(function() {
/*place jQuery actions here*/
var link = "<?php echo site_url('member/detail')?>/"; // Url to your application (including index.php/)
$(".detail-product").submit(function(){
var id = $(this).find('input[name=product_id]').val();
var qty = $(this).find('input[name=quantity]').val();
$.post(link + "member/add", { product_id: id, quantity: qty, ajax: '1' },
function(data){
if(data == 'true'){
$.get(link + "member/detail", function(cart){ // Get the contents of the url cart/show_cart
$("#cart_content").html(cart); // Replace the information in the div #cart_content with the retrieved data
});
}else{
alert("Product does not exist");
});
return false; // Stop the browser of loading the page defined
});
});
</script>
This is problem url: http://localhost/scientificwriting/member/detail/ and productid can not be invoked. Do I need to replace the IF statement on my controller and my jquery?
Please help me thanks
I tried to add checked property to a specific checkbox using jquery, but it seems doesn't work properly.
Checkbox Function Display
function product_category_loop_array ($product_category_array = array (), $parent_id = 0, $margin = -20) {
if (!empty ($product_category_array[$parent_id])) {
$margin = $margin + 20;
foreach ($product_category_array[$parent_id] as $items) {
echo "<input style='margin: 0 0 0 ".$margin."px; overflow: hidden;' id='".$items['id']."' class='category_checkbox left' name='product_category[]' type='checkbox' value='".$items['id']."'><label class='category_checkbox_label left'>".$items['name']."</label>
<div class='clear'></div>";
product_category_loop_array ($product_category_array, $items["id"], $margin);
}
}
}
function product_category () {
$db_connect = mysqli_connect (db_host, db_username, db_password, db_name);
$product_category_query = $db_connect->query ("SELECT id, name, parent_id FROM `product_category` ORDER BY name ASC");
$product_category_array = array ();
if (mysqli_num_rows ($product_category_query)) {
while ($row = mysqli_fetch_array ($product_category_query, MYSQLI_ASSOC)) {
$product_category_array[$row['parent_id']][] = $row;
}
product_category_loop_array ($product_category_array);
}
}
HTML
<div id="tabs-2" class="product_detail hide">
<div class="left">
<form class="product_detail_form" method="post" action="">
<?php product_category () ?>
<p class="detail_submit"><input name="save_product_category" type="submit" value="Save"></p>
</form>
</div>
</div>
Jquery
<script>
$ (document) .ready (function () {
<?php echo $hide_div ?>;
var selected_array = <?php echo json_encode ($product_category_selected) ?>;
$.each (selected_array (key, value) {
$ ("#"+value).prop("checked", true);
});
});
</script>
Any help will be appreciated.
Use
$ ("#"+value).attr("checked", "checked");
<script>
$(document).ready(function() {
<?php echo $hide_div ?>;
// DECODE JSON IN JS
var selected_array = $.parseJSON(<?php echo json_encode ($product_category_selected) ?>);
$.each(selected_array(key, value) {
$("#" + value).prop("checked", true);
});
});
</script>