What I want to do is really simple but I'm obviously not great and very new to PHP. I'm using woocommerce for my store and a woocommerce add on called "woocommerce scheduler" to set a start and end time specific products are for sale each day.
The way the scheduling program works is you set start and end times with 24hr clock example 10am (10:00) to 8pm (20:00) the product can be purchased anytime from 8am-10pm after 10pm the plugin "hides" the add to cart button ans displays a custom message such as "Currently Unavailable" and you can't purchase the item.
I ran into 2 problems one of which is solved
I wanted the plugin to show the start time the product would become available with the unavailable message basically it would say "unavailable until 8am"
So the solution to that problem was a plugin called Advanced Custom Fields
and creating a custom field that I can set on the product page and then I can display by putting this php code if the feild is empty on another product page it isn't displayed <?php if( get_field('product_time') ): ?>
<p>Available at <?php the_field('product_time'); ?></p>
<?php endif; ?>
into my single product page template file.
I got that working just fine. Now my second issue that I can't solve.
I want to hide the code that shows the time when the unavailable message is hidden because the product is in the hours where it is available for purchase. So how would I go about this?
I found the bit of code that pulls the unavailable message but it is a js. file so I cant put the php I have into that file.
wdm_validate.js
jQuery(document).ready(function(){
jQuery('.cart').html('<p class = "wdm_message3">'+wdm_message.wdm_expiration_message+' </p>');
});
this is the bit in the php file for the scheduler plugin that calls that js file
woocommerce_scheduler.php
if($wdm_start_date <= $d && $d<=$wdm_end_date && $str_start_time <=
$curtime && $curtime <= $str_end_time) {
}else{
//echo "<style>.cart{display:none;}</style>";
//echo "hello";
//exit;
wp_enqueue_script('wdm_expiration_message',plugins_url('js/wdm_validate.js',__FILE__),
array('jquery'));
$data = array(
'wdm_expiration_message' => get_option( 'woocommerce_custom_product_expiration',true )
);
wp_localize_script('wdm_expiration_message','wdm_message',$data);
}
In your custom code just add class to <p> tag and perform following:-
<?php if( get_field('product_time') ): ?>
<p class='wdm_available'>Available at <?php the_field('product_time'); ?></p>
<script>
jQuery(document).ready(function(){
//checking wdm_message3 i.e. expiration message exist or not
if(!jQuery('.wdm_message3').is(':visible')){
jQuery('.wdm_available').hide();
}
});
</script>
<?php endif; ?>
adding this code to my price.php template page in woocommerce got me exactly what I needed. The time is hidden when the product is available and shows when the item is not available for sale letting my customer know what time they can purchase it.
<?php if( get_field('product_time') ): ?>
<p class='wdm_available'>Unavailable Until <?php the_field('product_time'); ?></p>
<script>
jQuery(document).ready(function(){
// set a timeout function to get around brief delay in scheduler script
setTimeout(function(){
//check if scheduler message is NOT visible
if(!jQuery('.wdm_message3').is(':visible')) {
// if not, hide custom availabilty time message
jQuery('.wdm_available').hide();
}
}, 0);
});
</script>
<?php endif; ?>
There's no way for me to know the format of your custom field, but you should be able to run a comparison against the current time. As an idea:
$current_time = time();
$sale_time = get_field('product_time');
if( $sale_time && $sale_time < $current_time ): ?>
<p class='wdm_available'>Unavailable Until <?php the_field('product_time'); ?></p>
<?php endif; ?>
Related
I have plugin A that needs to verify two pieces of shortcodes before an action takes place within another plugin.
<?php
function mepr_add_some_tabs($user) {
?>
<span class="mepr-nav-item prem-support <?php MeprAccountHelper::active_nav('member-profile'); ?>">
<!-- KEEPS THE USER ON THE ACCOUNT PAGE -->
Profile
</span>
<?php
}
add_action('mepr_account_nav', 'mepr_add_some_tabs');
function mepr_add_tabs_content($action) {
//Listens for the "member-profile" action on the account page, before rendering the FF view shortcode.
if($action == 'member-profile') {
echo do_shortcode('[frm-condition source=frm-stats id=80 type=count user_id=current greater_than=0] [display-frm-data id=5728] [/frm-condition]');
}
}
add_action('mepr_account_nav_content', 'mepr_add_tabs_content');
The problem is I'm not a developer and I don't know how to add the second shortcode in the echo do_shortcode function.
The current shortcode basically says: if the user has an entry on the field ID: 80, then show them the content of the entry.
What I need to add is another, second shortcode which will say almost the same thing, but about another field ID.
How can I add it while following this requirement?
I'm creating a custom woocommerce integrated theme for wordpress.
I have a blob on the top that displays the total number of items in the cart, I want to update this blob using Jquery (w/o reloading the page) I was able to increase the number of items by getting the current number in the blob and increasing it by +1 for each click, the problem is the add to cart has an option to select the number of items you want to add to the cart. So if I select 3 items and click the button the blob only increases by one.
I can create a way to get the number of items being added from the front-end but I think it's unnecessary. I want to be able to get the total number from PHP sessions using jquery so that on every click of add item or remove item I'll get the current number dynamically from the server.
What I have done so far is to create a reloadCart.php file that echos the cart total, here's the code
<?php
require('../../../wp-blog-header.php');
global $woocommerce;
echo $woocommerce->cart->get_cart_contents_count();
?>
When I visit this page it echos the current item totals, but I cant get this data from jquery, it's been sometime since I last used AJAX also I have not worked on web projects for a very long time, but with what I remember, the AJAX call that I'm making is right.
I have tried using the get() and post() functions of jquery as well as the normal ajax() function, but nothing seems to work. Can someone please help?
$(".ajax_add_to_cart").click(function () {
/*$("#bag-total").html(function () {
var bagTotal = parseInt($(this).html());
return ++bagTotal;
});*/
alert('clicked');
$.get("<?php echo get_template_directory_uri(); ?>/reloadCart.php", function(data){
alert("Data: " + data);
});
});
The lines that are commented are the ones that I was using previously, to add the cart total by getting the current cart number from the front-end.
Any help would be appreciated. Thanks in advance!
You should not use any reload to update the cart content count… Instead you should use the dedicated woocommerce_add_to_cart_fragments action hook that is Ajax powered.
1) The HTML to be refreshed: So first in your theme's header.php file you should need to embed the cart count in a specific html tag with a defined unique ID (or a class), for example something like:
$items_count = WC()->cart->get_cart_contents_count();
?>
<div id="mini-cart-count"><?php echo $items_count ? $items_count : ' '; ?></div>
<?php
or:
$items_count = WC()->cart->get_cart_contents_count();
echo '<div id="mini-cart-count"><?php echo $items_count ? $items_count : ' '; ?></div>';
2) The code:
add_filter( 'woocommerce_add_to_cart_fragments', 'wc_refresh_mini_cart_count');
function wc_refresh_mini_cart_count($fragments){
ob_start();
$items_count = WC()->cart->get_cart_contents_count();
?>
<div id="mini-cart-count"><?php echo $items_count ? $items_count : ' '; ?></div>
<?php
$fragments['#mini-cart-count'] = ob_get_clean();
return $fragments;
}
if you use a class in your html Tag, you will replace ['#mini-cart-count'] by ['.mini-cart-count']. This hook is also used to refresh the mini-cart content.
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Since few years global $woocommerce; + $woocommerce->cart is outdated and replaced by WC()->cart to access WooCommerce cart object.
If you need jQuery to force refresh that count, you can try wc_fragment_refresh or wc_fragments_refreshed delegated events, like:
$(document.body).trigger('wc_fragment_refresh');
or:
$(document.body).trigger('wc_fragments_refreshed');
For anyone who wants the proper ajax implementation, here is the way to go.
in functions.php
add_action('wp_ajax_cart_count_retriever', 'cart_count_retriever');
add_action('wp_ajax_nopriv_cart_count_retriever', 'cart_count_retriever');
function cart_count_retriever() {
global $wpdb;
echo WC()->cart->get_cart_contents_count();
wp_die();
}
in your script file (assuming you have enqued the script file and passed the ajax object into the script. you also need to put this block into a setInterval or in some other jquery action.
var data = {
'action': 'cart_count_retriever'
};
jQuery.post(ajax_object.ajax_url, data, function(response) {
alert('Got this from the server: ' + response);
});
In header.php or where you want to show count
<?php $items_count = WC()->cart->get_cart_contents_count();
echo $items_count; //use this function for print the value of cart items count
?>
I have not used woocommerce before but one pretty simple option when you say in your post:
When I visit this page it echos the current item totals, but I cant get this data from JQuery
...would be to use a user-sided JavaScript variable for the display, and then just call the PHP update methods for adding items to your cart using AJAX (which I do not show below because you have not provided that code).
<?php
//hardcoded value for $woocommerce->cart->get_cart_contents_count()
$woocommerce = 59;
?>
<button class="ajax_add_to_cart">Add to cart</button>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
//user sided variable for PHP value
var total = parseInt($(".totalCost").text());
$(".ajax_add_to_cart").click(function(){
total++; //add to cart
$(".totalCost").text(total); //update
});
});
</script>
<p class="totalCost">
<?php echo json_encode($woocommerce); ?>
</p>
You can copy and test this snippet on: http://phpfiddle.org/
Basically in the above code, I set the PHP value as a paragraph text on page load and then read that value into a JS variable to mess around with the data on the client side of the application and then I update the display text as needed.
function woocommerce_header_add_to_cart_fragment( $fragments ) {
$fragments['li.cart-open'] = '<li class="cart-open"><a href="javascript:void(0)" class="cart" title="Cart">
<svg xmlns="http://www.w3.org/2000/svg" width="96" height="96" viewBox="0 0 96 96" class="svg-large"><switch><g><path d="M68 24v-4C68 8.954 59.046 0 48 0S28 8.954 28 20v4H12v60c0 6.63 5.37 12 12 12h48c6.63 0 12-5.37 12-12V24H68zm-32-4c0-6.627 5.373-12 12-12s12 5.373 12 12v4H36v-4zm40 64c0 2.21-1.79 4-4 4H24c-2.21 0-4-1.79-4-4V32h56v52z"/></g></switch></svg>
<i>'.WC()->cart->get_cart_contents_count().'</i>
</a></li>';
return $fragments;
}
Dear fellow coders and community,
I have really researched this issue for quite a while now, and though I have some coding background, I can't seem to figure out the issue. No links to the topic helped since they all only suggest the code snippet I am using already.
I have a working Word Press installation : Version 4.8.2 & WooCommerce : Version 3.3.5
My Theme consists of essentially 2 Files, since I had to breakdown the whole thing in order to find the issue.
The functions.php
<?php
add_filter('loop_shop_columns', 'loop_columns');
if (!function_exists('loop_columns')) {
function loop_columns() {
return 5; // 3 products per row
}
}
add_filter( 'loop_shop_per_page', 'new_loop_shop_per_page', 999999999999 );
function new_loop_shop_per_page( $cols ) {
// $cols contains the current number of products per page based on the value stored on Options -> Reading
// Return the number of products you wanna show per page.
//
var_dump($cols);
$cols = 3;
return $cols;
}
?>
The code is just supposed to limit the amount of products displayed on the product category page to 3.
And the index.php
<?php
get_header();
?>
<div class="container content pr-0 pl-0" >
<div class="index">
<?php if ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endif; ?>
</div>
</div>
<?php
get_footer();
?>
This is just the essential index file needed by any wordpress installation.
Now I tried changing backend settings already with the default 2017 theme and it worked just fine...so it cannot be some backend thing.
What could be the issue here?
Any help or leads will be much appreciated! Thanks!
The loop_shop_columns filter doesn't effect the number of posts shown, just the number of columns on the page (number of products going across before a new line is started).
The number of products shown on any page is actually taken from the WordPress Settings > Reading "Blog pages show at most" setting.
You can also change the number programatically by using the pre_get_posts filter.
add_action( 'pre_get_posts', 'rc_modify_query_exclude_category' );
// Create a function to excplude some categories from the main query
function rc_modify_query_exclude_category( $query ) {
// Check if on frontend and main query is modified
if ( ! is_admin() ) {
$query->set( 'posts_per_page', '-1' );
} // end if
}
I am trying to create a coupon click counter.
I received the default number of clicks using the code
<?php $couponusers = get_field('coupon_users'); ?>
And I displayed the default number of clicks using the code,
<p><?php echo $couponusers ?></p>
Now, I want to increase the value of ‘coupon_users’ whenever a click is made. Any suggestions on how to do this?
The page I need help with: https://deals.onlinerockershub.com/bluehost-web-hosting/
Add this action to your functions.php
function coupon_callback() {
//
// Add here cookie check
//
if(is_page('1')){ // ID of "Thank you for coupon click" page
update_field('coupon_users',$couponusers); // or similar
}
return;
}
add_filter( 'wp', 'coupon_callback' );
I would like to show a custom welcome message for each of my registered shop customers. Something like "Welcome CUSTOMERNAME! You have been with us for one year now. To say thanks, we are giving you a discount on everything in our shop of 10%."
I have created a custom field welcome_message with the plugin Advanced Custom Fields and would like to show the value of it in the account dashboard of my customers at frontend.
At the backend, I am showing the textarea at the user profile page and I am adding the field with this code:
$message = get_field( 'welcome_message' );
echo esc_attr( $message );
This code is placed here: /wp-content/themes/flatsome-child/woocommerce/myaccount/dashboard.php
But somehow the value is not coming up. I have also tried it with simply the_field('welcome_message'); or to add [acf field="{$welcome_message}"] at the account page in WordPress but that didn't work either. Somehow the value is not coming through.
I hope someone could help me out a litte.
Thanks in advance.
Best regards
It's necessary that get_field function is connected to user.
Eg:
<?php
$variable = get_field('field_name', 'user_1');
?>
In case of repeater field:
<?php if( have_rows('repeater', 'user_1') ): ?>
<ul>
<?php while( have_rows('repeater', 'user_1') ): the_row(); ?>
<li><?php the_sub_field('title'); ?></li>
<?php endwhile; ?>
</ul>
<?php endif; ?>