onclick Modal box in single product page - php

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.

Related

How can I load WooCommerce order pages style into my Custom submenu page?

I have registered a new submenu under WooCommerce. What I am looking for now is to load WooCommerce admin style css on that page. So I can just use WooComerce css classes and id to make it looks like the Orders Page.
Here is my code to register new setting page.
add_submenu_page(
'woocommerce', __('My Page Title', 'text-domain'), __('My Page Title', 'text-domain'), 'manage_options', 'my-custom-slug', array($this, 'my_custom_Page')
);
Here is my code to render my setting page elements.
public function my_custom_Page(){
?>
<div class="wrap">
<div id="root">
<div class="woocommerce-layout">
<div class="woocommerce-layout__header">
<div class="woocommerce-layout__header-wrapper">
<h1 class="woocommerce-layout__header-heading">My Custom Page</h1>
</div>
</div>
</div>
</div>
</div>
<?php
}
I am trying to find a solution to load WooCommerce admin pages css and JS into my custom admin page under WooCommerce.
To use WooCommerce custom admin css on your custom settings page add this code to the same file that add_submenu_page() code resides.
function random_add_woocommerce_admin_css() {
wp_enqueue_style( 'woocommerce_admin_styles', WC()->plugin_url() . '/assets/css/admin.css');
}
add_action( 'admin_enqueue_scripts', 'random_add_woocommerce_admin_css' );

Wordpress nested if is_active_sidebar within another is_active_sidebar

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>

Woocommerce Cart widget displays no content

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.

ShortCode Wordpress doesnt work

I have a shortcode that I put in my post but somehow it doent execute for some reasong. Here is the code I have:
short-code.php:
add_shortcode('teammate',function($atts){
$classes=$atts['country'];
$imgUrl=$atts['img'];
$name=$atts['name'];
$description=$atts['description'];
echo '
<div class="member "'.$classes.'>
<div class="member-img">
<img src="'.$imgUrl.'">
</div>
<div class="member-desc">
<h1>'.$name.'</h1>
<p>'.$description.'</p>
</div>
<div class="clear"> </div>
</div>
';
});
Here is how I include the file in functions.php:
$includes = array(
'includes/theme-options.php', // Options panel settings and custom settings
'includes/theme-functions.php', // Custom theme functions
'includes/theme-actions.php', // Theme actions & user defined hooks
'includes/theme-comments.php', // Custom comments/pingback loop
'includes/theme-js.php', // Load JavaScript via wp_enqueue_script
'includes/sidebar-init.php', // Initialize widgetized areas
'includes/theme-widgets.php', // Theme widgets
'includes/short-code.php', // Custom shortcode file for side menu
'includes/team.php' //team shortcode
);
and here is what i put in my page:
<div class="outer-members">
<div class="inner-members">
[teammate name="Darko Petkovski" img="http://myurl.com/myimage.jpg" description="test" country="mc"]
</div>
</div>
Seems that the code is working ok, but the only problem is that shortcode isn't executing outside wp.

add draggable sections in wordpress plugin page

want to make draggable and sortable sections in plugin page of wordpress, like we see on dashboard. i tried finding but dint get exactly what I want.
Here is the piece of code though it add two divs with interface similar to draggable interface in dashboard, but i am unable to drag.
<div class="wrap">
<h2>I like to move it, move it</h2>
<div class="meta-box-sortables ui-sortable">
<div class="postbox" id="p1">
<h3 class="hndle">Drag me around, babe</h3>
<div class="container">
<p>Your content goes here</p>
</div>
</div><!-- .postbox -->
<div class="postbox" id="p2">
<h3 class="hndle">Drag me, too</h3>
<div class="container">
<p>Your content goes here, again</p>
</div>
</div><!-- .postbox -->
</div><!-- .meta-box-sortables.ui-sortable-->
</div><!-- .wrap -->
<?php
function move_me_around_scripts() {
wp_enqueue_script('dashboard');
wp_enqueue_script( 'jquery-ui-sortable');
}
function admin_page_with_draggable_boxes(){
$my_page = add_dashboard_page( 'moveit', 'moveit', 'read',
'moveit' );
add_action('load-'.$my_page, 'move_me_around_scripts');
}
add_action('admin_menu', 'admin_page_with_draggable_boxes'); ?>
You have to enqueue the sorting script, and add jQuery and jQuery UI Sortable as dependencies. Your sample code has add_dashboard_page with wrong parameters, also, use admin_print_scripts instead of load-$page.
add_action('admin_menu', 'admin_page_with_draggable_boxes');
function admin_page_with_draggable_boxes()
{
$my_page = add_dashboard_page(
'Move it',
'Move it',
'add_users',
'moveit-page',
'moveit_callback'
);
add_action( "admin_print_scripts-$my_page", 'move_me_around_scripts' );
}
function move_me_around_scripts()
{
wp_enqueue_script(
'move-it',
plugins_url( '/moveit.js', __FILE__ ), // <----- get_stylesheet_directory_uri() if used in a theme
array( 'jquery-ui-sortable', 'jquery' ) // <---- Dependencies
);
}
function moveit_callback()
{
?>
<div class="wrap">
<h2>I like to move it, move it</h2>
<div class="meta-box-sortables ui-sortable">
<div class="postbox" id="p1">
<h3 class="hndle">Drag me around, babe</h3>
<div class="container">
<p>Your content goes here</p>
</div>
</div><!-- .postbox -->
<div class="postbox" id="p2">
<h3 class="hndle">Drag me, too</h3>
<div class="container">
<p>Your content goes here, again</p>
</div>
</div><!-- .postbox -->
</div><!-- .meta-box-sortables.ui-sortable-->
</div><!-- .wrap -->
<?php
}
And the moveit.js file:
jQuery(document).ready(function($)
{
$('.meta-box-sortables').sortable({
opacity: 0.6,
revert: true,
cursor: 'move',
handle: '.hndle'
});
});
From what i can see of your code you probably want to use sortable together with draggable ui
http://jqueryui.com/draggable/#sortable in someway or alone with snapping.

Categories