I added the woocommerce Cart Widget to my sidebar and due to some customization of the theme and woocommerce behavior I added this line of code to my themes functions.php:
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );
When this line is active no cart contents are shown, just the widget title. When I comment out this line the items in the cart are shown on the single products page and on the shop page. See markup:
<div id="secondary" class="widget-area" role="complementary">
<aside id="woocommerce_widget_cart-12" class="widget woocommerce widget_shopping_cart">
<h3 class="widget-title">Winkelmand</h3>
<div class="widget_shopping_cart_content">
<ul class="cart_list product_list_widget ">
<li>
<a href="http://localhost/?product=flying-ninja">
<img width="600" height="600" src="//localhost/wp-content/uploads/2013/06/poster_2_up-600x600.jpg" class="attachment-shop_thumbnail wp-post-image" alt="poster_2_up">Flying Ninja </a>
<span class="quantity">1 × <span class="amount">€12,00</span></span>
</li>
<li>
<a href="http://localhost/?product=patient-ninja">
<img width="600" height="600" src="//localhost/wp-content/uploads/2013/06/hoodie_3_front-600x600.jpg" class="attachment-shop_thumbnail wp-post-image" alt="hoodie_3_front">Patient Ninja </a>
<span class="quantity">1 × <span class="amount">€35,00</span></span>
</li>
<li>
<a href="http://localhost/?product=photo-3">
<img width="399" height="600" src="//localhost/wp-content/uploads/2014/12/DSC07870-399x600.jpg" class="attachment-shop_thumbnail wp-post-image" alt="SONY DSC">Photo 3 </a>
<span class="quantity">1 × <span class="amount">€15,00</span></span>
</li>
</ul><!-- end product list -->
<p class="total"><strong>Subtotaal:</strong> <span class="amount">€62,00</span></p>
<p class="buttons">
Winkelmand bekijken
Afrekenen
</p>
</div>
</aside>
</div>
As you can see all thre products are shown and the buttons appears as well.
However they are not shown on all the other pages. On those pages only the widget title appears. on those pages the markup looks like this:
<div id="secondary" class="widget-area" role="complementary">
<aside id="woocommerce_widget_cart-12" class="widget woocommerce widget_shopping_cart">
<h3 class="widget-title">Winkelmand</h3>
<div class="widget_shopping_cart_content">
</div>
</aside>
</div>
The questions I have are:
Why is the remove_action line of code interfering with the contents of the cart widget?
Why is the div class="widget_shopping_cart_content">empty on the other pages, no matter if the remove_action line is commented out or not.
I figured out that it has something to do with my customization. When I use the standard twentytwelve theme (on wicht my child theme is build) it works fine.
EDIT
Of course I'm searching myself to and was able to narrow it further down.
The cause is in this function in my theme's function.php but I don't know what is wrong with it and what I should do about it.
function twentytwelve_child_masonry() {
if (!is_admin()) {
wp_enqueue_script('masonry');
add_action('wp_footer', 'twentytwelve_child_add_masonry');
function twentytwelve_child_add_masonry() { ?>
<script>
(function( $ ) {
"use strict";
$(function() {
//set the container that Masonry will be inside of in a var
var container = document.querySelector('.products');
//create empty var msnry
var msnry;
// initialize Masonry after all images have loaded
imagesLoaded( container, function() {
msnry = new Masonry( container, {
itemSelector: '.product',
isAnimated: true
});
});
});
}(jQuery));
</script>
<?php
}
}
}
add_action('init', 'twentytwelve_child_masonry');
I'm pulling my hair out..... I'm trying for a week now to solve this problem. Please help!?
EDIT 2
I got this message when I try to debug the project:
TypeError: 'null' is not an object (evaluating 'a.length') (15:45:27:306 | error, javascript)
at f (http://localhost/wp-includes/js/masonry.min.js?ver=3.1.2:1:28901)
at g (http://localhost/wp-includes/js/masonry.min.js?ver=3.1.2:1:29105)
at g (http://localhost/wp-includes/js/masonry.min.js?ver=3.1.2:1:29031)
at (anonymous function) (http://localhost/?product=happy-ninja:262:40)
at j (http://localhost/wp-includes/js/jquery/jquery.js?ver=1.11.1:2:27248)
at fireWith (http://localhost/wp-includes/js/jquery/jquery.js?ver=1.11.1:2:28057)
at ready (http://localhost/wp-includes/js/jquery/jquery.js?ver=1.11.1:2:29901)
at J (http://localhost/wp-includes/js/jquery/jquery.js?ver=1.11.1:2:30261)
>
The problem probably lies with your Masonry function and not the widget itself. The default WC cart widget creates just an empty div for the widget; it then uses JS to insert the products. It's highly likely that the Masonry JS you show is causing some kind of error that's preventing WC's JS from working. This is why when you disable your script the widget works fine. You need to check your console for any JS errors that you're finding and correct them.
Related
I want to create a onclick modal in single product page. I have put together the following:
FILES
js/modal-jquery.js and modal-box.html are uploaded in child theme's directory.
css is in child theme's css.
I enqueued the jquery in functions.php
// Enqueue MODAL Jquery JS
function my_enqueue_stuff() {
if(is_singular( 'product' ))
{
wp_enqueue_script( 'modal-jquery-js', get_stylesheet_directory_uri() . '/js/modal-jquery.js');
// wp_enqueue_script( 'modal-box', get_stylesheet_directory_uri() . 'modal-box.html');
}
}
add_action( 'wp_enqueue_scripts', 'my_enqueue_stuff' );
LINK in functions.php
// POPUP LINK
add_action( 'woocommerce_before_add_to_cart_form', 'popup-modal', 3 );
function popup-modal() {
// $file = file directory to modal-box.html
echo '<a class="js-open-modal" href="#" data-modal-id="popup"> Click me </a>';
}
modal-box.html
<div id="popup" class="modal-box">
<header>
×
<h3>Modal Title</h3>
</header>
<div class="modal-body">
<p>Modal Body</p>
</div>
<footer>
Close Button
</footer>
</div>
I'm not sure if this is the right approach.
I've been trying to add modal-box.html into function popup-modal().
I've also tried to enqueue modal-box.html but it gives an error message Uncaught SyntaxError: expected expression, got '<'
How can I load the html file as an external file into the single product page? If this is not the right way to do it, how can I add a modal box to the single product page?
Thanks
Instead of is_singular( 'product' ) use woocommerce's own is_product()
For the modal, what about including it in the footer, also only on product pages, like this?:
function your_modal_footer(){
if( !is_product() ){
return;
}
?>
<div id="popup" class="modal-box">
<header>
×
<h3>Modal Title</h3>
</header>
<div class="modal-body">
<p>Modal Body</p>
</div>
<footer>
Close Button
</footer>
</div>
<?php
}
add_action('wp_footer', 'your_modal_footer');
I would add a style="display:none;" to the popup div and then make it visible onclick of the popup's trigger.
I have a theme that displays a menu of products via widgets. We are able to show/hide these categories (sidebars) via the code below. Right now it will show/hide this carousel-item if one of the two product categories have widgets. We would like keep this functionality, but hide the category if it is empty. For example if there are products in category 1, but not in category 2 we want to hide an entire section (see notes in code of where we want this.
<?php if ( is_active_sidebar( 'widget-category-1' ) || is_active_sidebar('widget-category-2') ) { ?>
<div class="carousel-item active animated fadeIn">
<div class="d-block w-100">
//show or hide start here//
<div class="container-fluid px-5">
<div class="row">
<ul class="three-col">
<?php dynamic_sidebar( 'widget-category-1' ); ?>
</ul>
</div>
</div>
//show or hide end here//
//show or hide start here//
<div class="container-fluid">
<div class="row">
<ul class="three-col">
<?php dynamic_sidebar( 'widget-category-2' ); ?>
</ul>
</div>
</div>
//show or hide end here//
</div>
</div>
<?php } ?>`
You can use first-of-type or nth-of-type to reference the element you want to toggle. That or possibly a not() pseudo class should do the trick.
I added a CSS tag to your question. You would need to add the rest of the element that dynamic_sidebar is returning to be sure but typically there is a 'has-items' identifier you can reference.
We ended up wrapping each section in a <div class="phide"></div> and used javascript to hide the this div if no <li></li> is present
<script>
(function($) {
$( document ).ready(function() {
$('.phide:not(:has(li))').hide();
});
})(jQuery);
</script>
I'm building a wordpress site (here: http://dev.tps.entropii.com/contact-us/) that requires a full page width google map on the contact page. The Business has two addresses so they effectively need two maps with a means to switch between them. I'm using Advanced Custom Fields plugin to give the user the functionality to update/edit their addresses.
Here's the problem. In order to give the functionality to switch back and forth between the two maps I decided to put them inside a twitter bootstrap carousel. One slide for each map. This works as expected with one problem. The map that is contained within the inactive slide (e.g. the slide that doesn't have the class 'active' on page load), doesn't seem to fully load. If I put the maps side by side on the page without the carousel they load no problem.
Because I'm using advanced custom fields, the maps are being loaded using php. Here's the HTML/php from my template file:
`
<!-- Wrapper for slides -->
<div class="carousel-inner">
<div class="item active">
<!-- map 1 -->
<?php $map1 = get_field('map_1'); if( !empty($map1) ): ?>
<div class="acf-map" id="map1">
<div class="marker" data-lat="<?php echo $map1['lat']; ?>" data-lng="<?php echo $map1['lng']; ?>"></div>
</div>
<?php endif; ?>
</div>
<div class="item">
<!-- map 2 -->
<?php $map2 = get_field('map_2'); if( !empty($map2) ): ?>
<div class="acf-map" id="map2">
<div class="marker" data-lat="<?php echo $map2['lat']; ?>" data-lng="<?php echo $map2['lng']; ?>"></div>
</div>
<?php endif; ?>
</div>
</div>
<!-- Controls -->
<a class="left carousel-control" href="#map-carousel" data-slide="prev"></a>
<a class="right carousel-control" href="#map-carousel" data-slide="next"></a>
</div>`
My suspicion is that because the inactive slides of the bootstrap carousel are set to display none, that the content isn't being loaded int he first place properly. However I'm completely stabbing in the dark. Can anyone shed any light on this?
Let me know if you ned any more info. Thanks,
apologies if this has been covered anywhere here. I tried searching, but only found topics related to styling and positioning captions within the carousel code…
So, I have a layout that contains three columns:
The left column contains general information related to each page/project.
The center column contains a Bootstrap 3 carousel with images.
The right column is where I was planning on displaying all the captions related to the images in the center Carousel.
I can't quite figure out how to get the captions working in the right column. To clarify, the captions will change with each carousel slide, just as they do in the default carousel setting. I basically want to move the captions off the image, and into the right column.
I am using Kirby (www.getkirby.com) as my CMS and I am using a foreach loop to get the code for the images, etc. in the carousel. Here is my current code, including the caption section (which I am trying to move…)
<div id="center" class="col-xs-12 col-sm-6 col-md-8">
<?php $imagepage = $page->children()->first() ?>
<?php if($imagepage->hasImages()): ?>
<div id="merry-go-round" class="carousel slide">
<!-- Wrapper for slides -->
<div class="carousel-inner">
<?php $n=0; foreach($imagepage->images() as $image): $n++; ?>
<div class="item<?php if($n==1) echo ' active' ?>">
<img style="display:block; margin-left: auto; margin-right: auto;" src="<?php echo $image->url() ?>" alt="<?php echo html($image->title()) ?>" class="img-responsive">
<div class="carousel-caption"><?php echo $image->img_title() ?></div>
</div>
<?php endforeach ?>
<?php endif ?>
</div><!-- /carousel-inner -->
<!-- Controls -->
<a class="left carousel-control" href="#merry-go-round" data-slide="prev"></a>
<a class="right carousel-control" href="#merry-go-round" data-slide="next"></a>
</div><!-- /merry-go-round -->
</div><!-- /#center -->
<div id="right" class="col-xs-12 col-sm-3 col-md-2">
<p>THIS IS WHERE I AM TRYING TO PUT THE CAROUSEL CAPTIONS…</p>
</div><!-- /#right -->
I've tried by best but I am all out of ideas. I thought maybe I could do something like make the caption a variable:
<?php $test_caption = $image->img_title() ?><?php echo $test_caption ?>
but this doesn't work outside the carousel area. I'm guessing it's that it won't work outside of the foreach loop?
Anyway, if anyone has any suggestions I would really appreciate it. I'm learning PHP as I go along, but I don't know any javascript so I'm hoping there's a solution outside that. And again, I'm using Bootstrap 3.
Here is a link to a fiddle I made (without all the php stuff…):
http://jsfiddle.net/4tMfJ/2/
Based on Twitter Bootstrap Carousel - access current index
You can add the code below to your javascript (after loading jquery / bootstrap)
$(function() {
$('.carousel').carousel();
var caption = $('div.item:nth-child(1) .carousel-caption');
$('#right h1').html(caption.html());
caption.css('display','none');
$(".carousel").on('slide.bs.carousel', function(evt) {
var caption = $('div.item:nth-child(' + ($(evt.relatedTarget).index()+1) + ') .carousel-caption');
$('#right h1').html(caption.html());
caption.css('display','none');
});
});
also see: http://jsfiddle.net/4tMfJ/3/
Thanks Bass for your answer it worked for me !
But i did not want to have replicated content so i did it my way ;)
$("#slider").on('slide.bs.carousel', function(evt) {
var step = $(evt.relatedTarget).index();
$('#slider_captions .carousel-caption:not(#caption-'+step+')').fadeOut('fast', function() {
$('#caption-'+step).fadeIn();
});
});
http://jsfiddle.net/4fBVV/3/
This might seem easy enough for the lovely experts here at SO :) but I can't find a decent answer/question about this on SO or google. Pls help. :)
How do i display a current value of a php variable in realtime via AJAX/js/jQuery
Basically im trying to do an E-commerce site and I am storing the shopping cart contents using a session and displaying it with an echo statement counting how many contents is currently in the basket, however when they are on a certain product and tried adding it in the cart, the value of the cart doesn't echo the added item until there is a page refresh, so i need something like an AJAX to display the updated value of the session when an item is added without having a page refresh.
Thank you so much.
Here is my code snippet for the cart as a sample:
// Assign shopping cart ($_SESSION['cart']) into variable
$basket = count($_SESSION['cart']);
And for displaying:
<!-- Display contents of the shopping cart -->
<ul id="basket" class="clearfix">
<li class="top">
<a href="checkout.php" class="top_link">
<span>Items: <? echo $basket ?></span>
</a>
</li>
</ul>
Thank you so much.. kindly provide a sample code on how to achieve this (either js or jQuery) and would help as well for best practices in dealing with this type of scenario.
First solution (based on your request - BAD IDEA)
get-count-items.php
<?php
session_start();
die(count($_SESSION['cart']));
?>
Javascript
<script>
setInterval(function(){
$.post('get-count-items.php', {}, function (total_items){
$('#total-items').html(total_items);
});
}, '1000');
</script>
HTML
<ul id="basket" class="clearfix">
<li class="top">
<a href="checkout.php" class="top_link">
<span>Items: <span id="total-items"><? echo $basket ?></span></span>
</a>
</li>
</ul>
This javascript will make a request to server at each 1 second.
Second solution (event on button "Add to cart")
add-to-cart.php
<?php
session_start();
// code for add to cart...
die(count($_SESSION['cart']));
?>
Javascript
<script>
$('.add-to-cart').click(function(){
$.post('add-to-cart.php', {}, function (total_items){
$('#total-items').html(items);
});
return false;
});
</script>
HTML
<ul id="basket" class="clearfix">
<li class="top">
<a href="checkout.php" class="top_link">
<span>Items: <span id="total-items"><? echo $basket ?></span></span>
</a>
</li>
</ul>
<a href="" class="add-to-cart">Add to cart</>