Woocommerce - validate coupon with cart items & coupon data - function

I’m in the process of validating a coupon on cart items rather than the order as a whole. If the cart items exceeds the number of available coupon limit, the coupon errors and gives the user an error message.
The code below checks how many specific product items are in the basket, for example 5. // This works
The code below also returns data from the coupon being used. // This doesn’t work
Example of the data im looking to return so I can compare.
coupon code = 123
coupon usage_count = 1
coupon usage_limit = 3
Available usage = 2
I already have another coupon validation in place that checks whether a users facilities booking falls in between their hotel booking dates, that works fine. Anyone have any suggestions where I’m going wrong with this validation? I can’t seem to return the data from the coupon being inputted. Am I using the correct filter? Thanks
add_filter( 'woocommerce_coupon_is_valid', 'coupon_validation_quantity', 10, 2);
function coupon_validation_quantity($valid, $coupon) {
global $wpdb;
global $woocommerce;
global $product_items_in_cart;
$product_id = 493;
$product_items_in_cart = 0;
foreach ( WC()->cart->get_cart() as $cart_item ) {
$cart_product_id = $cart_item['product_id'];
if($cart_product_id == $product_id){
$product_items_in_cart++;
};
};
global $product_items_in_cart;
global $cart_coupon_code;
global $cart_coupon_count;
global $cart_coupon_limit;
global $cart_coupon_available;
$applied_coupons_code = WC()->cart->get_applied_coupons();
$coupon_in_cart = new WC_Coupon($applied_coupons_code);
$cart_coupon_code = $coupon_in_cart->get_code();
$cart_coupon_count = $coupon_in_cart->get_usage_count();
$cart_coupon_limit = $coupon_in_cart->get_usage_limit();
$cart_coupon_available = ($cart_coupon_limit - $cart_coupon_count);
print_r('products in cart -> ' . $product_items_in_cart . "\r\n"); // return correct
print_r('available -> ' . $cart_coupon_available . "\r\n"); // returns zero
if($product_items_in_cart > $cart_coupon_available){
$valid = false;
};
return $valid;
}
Coupon Error Handling
add_filter( 'woocommerce_coupon_error', 'coupon_validation_error_quantity', 10, 3);
function coupon_validation_error_quantity($err, $err_code, $coupon) {
global $wpdb;
global $woocommerce;
global $product_items_in_cart;
global $cart_coupon_available;
print_r('products in cart -> ' . $product_items_in_cart . "\r\n"); // returns correctly
print_r('available -> ' . $cart_coupon_available . "\r\n"); // returns Zero
if($product_items_in_cart > $cart_coupon_available && intval($err_code) === WC_COUPON::E_WC_COUPON_INVALID_FILTERED){
$err = __( "Coupon $cart_coupon_code exceeds available usage. You have $cart_coupon_available available ", "woocommerce" );
};
return $err;
}

Related

Issue trying to convert HTML to Ajax

I got this in the video script that I use for my website (Videos Page Layout):
<div class="video-views pull-left">
{$videos[i].viewnumber|kilo} {if $videos[i].viewnumber == '1'}{t c='global.view'}{else}{t c='global.views'}{/if}
</div>
Related videos section uses Ajax for videos page layout generated with the "show more" button.
My problem is: I don't know how to convert the "kilo" function in Ajax {$videos[i].viewnumber|kilo}. I attempted a few things but with no result.
$code[] = '<div class="video-views pull-left">';
$views = ($video['viewnumber'] == '1') ? $lang['global.view'] : $lang['global.views'];
$code[] = $video['viewnumber']. ' '.$views;
$code[] = '</div>';
Kilo is not a modifier that is included in the Smarty distribution, but if you have access to the code on your site, you can extract the code from the plugin file, it is most likely in smarty/libs/plugins/modifier.kilo.php.
It looks like you're working in PHP to construct a response to an AJAX request, so you can just pull that code out and re-use it.
If you do not have access to the modifier file, you can just recreate the formatting on your own. Judging by the context, it's something simple like:
<?php
/**
* Format integers into human readable strings indicating number of thousands
* Example: 1200 -> 1.2K
* #param int $value
* #return string
*/
function kilo(int $value): string
{
// If the value is less than 1000, just return it
if($value < 1000)
{
return $value;
}
/*
* If the value is evenly divisible by 1000, we want to show a whole number,
* otherwise format it as a single precision float. Add "K" string literal
* to indicate thousands
*/
$formatString = ($value % 1000) ? '%.1fK':'%dK';
// Divide value by 1000
$value /= 1000;
// Return the formatted string
return sprintf($formatString, $value);
}
// Define some test data and echo the formatted values
$testViewCounts = [1, 123, 1230, 12300, 123000];
foreach($testViewCounts as $views)
{
// If only one view, do not pluralize
$label = ($views == 1) ? 'view':'views';
echo kilo($views).' '.$label.PHP_EOL;
}

Update prices before loading shop / product list in WooCommerce

I am currently developing a website for a trade / wholesale company who offer their clients different discounts (there are roughly 30 discount groups).
I am using wordpress / woocommerce.
I have access to each customers price list through a webservice which I have successfully integrated.
I am currently getting the price and updating woocommerce using the hook 'woocommerce_before_shop_loop'.
The issue I am having is that if all the current prices in woocommerce are set to say $1.00 but the price the current customer pays is $2.00 the first time the page loads it says $1.00 until its reloaded.
I understand that its because the query is obviously performed before both my script runs and the page loads.
function t31_before_shop_list_call() {
if ( is_user_logged_in()) {
$Conn = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
$itemCodesArray = array(); // WILL CONTAIN ARRAY OF SKU's FOR WS
while (have_posts()) : the_post();
global $product;
if( $product->is_type( 'simple' ) ){
$sku = get_post_meta( get_the_ID(), '_sku', true );
array_push($itemCodesArray, $sku);
} elseif( $product->is_type( 'variable' ) ){
$child_ids = $product->get_children();
foreach ($child_ids as $child_id) {
$sku = get_post_meta( $child_id, '_sku', true );
array_push($itemCodesArray, $sku);
}
}
endwhile;
... RUN WEBSERVICE THAT GETS CLIENTS PRICES FOR THE
... RUN THROUGH EACH PRODUCT RETURNED IN WEBSERVICE AND EXEC FOLLOWING UPDATE
UPDATE wp_postmeta
SET meta_value = " . $GDUSproduct_price . "
WHERE post_id = " . $GDUSproduct_id . "
AND meta_key = '_regular_price';";
DELETE
FROM `wp_options`
WHERE (`option_name` LIKE '_transient_wc_var_prices_%'
OR option_name` LIKE '_transient_timeout_wc_var_prices_%')";
}
}
add_action( 'woocommerce_before_shop_loop', 't31_before_shop_list_call', 15 );
Is there a hook I can use that executes before the query is performed or is there a way to re query before the html is displayed.?

Facebook login page and cache

I've got a Facebook login in my website
but I need to request permission to post in my user's wall.
I found many many examples on google that use the scope, but they're too different form mine, and I don't understand where to put it
This is the code:
<?php
session_start();
// added in v4.0.0
require_once 'autoload.php';
require 'functions.php';
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\Entities\AccessToken;
use Facebook\HttpClients\FacebookCurlHttpClient;
use Facebook\HttpClients\FacebookHttpable;
// init app with app id and secret
FacebookSession::setDefaultApplication( 'APPID','APPSECURE' );
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper('http://www.bestparty.altervista.org/APP/facebook/fbconfig.php' );
try {
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
// When Facebook returns an error
} catch( Exception $ex ) {
// When validation fails or other local issues
}
// see if we have a session
if ( isset( $session ) ) {
// graph api request for user data
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();
// get response
$graphObject = $response->getGraphObject();
$fbid = $graphObject->getProperty('id'); // To Get Facebook ID
$fbfullname = $graphObject->getProperty('name'); // To Get Facebook full name
$femail = $graphObject->getProperty('email');
/* ---- Session Variables -----*/
$_SESSION['FBID'] = $fbid;
$_SESSION['FULLNAME'] = $fbfullname;
$_SESSION['EMAIL'] = $femail;
checkuser($fbid,$fbfullname,$femail);
/* ---- header location after session ----*/
$cookie_name = 'FBID';
$cookie_value = $fbid;
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), '/');
$cookie_name2 = 'FULLNAME';
$cookie_value2 = $fbfullname;
setcookie($cookie_name2, $cookie_value2, time() + (86400 * 30), '/');
header("Location: ../gestaccount.php");
} else {
$cookie_name = 'FBID';
$cookie_value = $fbid;
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), '/');
$loginUrl = $helper->getLoginUrl();
header("Location: ".$loginUrl);
}
?>
Also:
May I save the access token in mysql to use it in future?
Why does the datas that I save in cache stays only until I close the browser?
1, You can store the access token however which way you like. As long as it's secure and you are able to update them in case of expired/updated/extended access tokens, it should be fine.
2, This is a bit unclear, are you setting and handling cookies correctly ?

Laravel: Querying based on Input. If input is empty, get all

I have a calendar (FullCalendar) where the user can filter down results based on a few params (Tutor Secondary Tutor, Lesson, Location). When the user makes a change to the query it hits the following code.
The issue I am having is the 'OR'. What I really want is an IF input is null then get all.
If User { Get all lessons where lead_tutor_id = 1 and secondary_tutors_id = 1 }
If User and Location { Get lessons where the user is as above, but have location_id = 3 }
etc, etc.
So, is there a way I can fall back to get ALL the results IF only one or two filters are set?
$current_events = Calendar::Where(function($query) use ($start_time, $end_time, $tutor, $location, $lesson)
{
$query->whereBetween('date_from', [$start_time, $end_time])->orderBy('date_from')
->whereRaw('lead_tutor_id = ?
OR secondary_tutors_id = ?
OR location_id = ?
OR lesson_id = ?',
[
$tutor, // Input get() for user
$tutor, // Input get() for user
$location, // Input get() for location
$lesson, // Input get() for lesson
]
);
})->with('lessons', 'leadtutor', 'secondarytutor')->get();
I've been playing with Query Scopes, but this seems to fail if passing a NULL value through to it.
Any help is very much appreciated. Thanks in advance.
You can build the query on forehand, store it in a variable and use it once its build.
$query = isset($var) ? $var : '';
$query .= isset($othervar) ? $othervar : '';
whereBetween(*)->orderBy(*)->whereRaw($query)
Only thing you need to keep in mind is to insert the 'OR's in the right place . So have like a check for wether it is the first thing to be inserted or not, if not then put 'OR' in front of it.
Hope that is enough info to help you.
After the advice from Saint Genius, I have got this working:
$built_query = [];
isset($lead_tutor) ? $built_query['lead_tutor'] = 'lead_tutor_id = ' . $lead_tutor . ' ' : null;
isset($secondary_tutor) ? $built_query['secondary_tutor'] = 'secondary_tutors_id = ' . $secondary_tutor . ' ' : null;
isset($location_id) ? $built_query['location'] = 'location_id = ' . $location_id : null;
isset($lesson_id) ? $built_query['lesson'] = 'lesson_id = ' . $lesson_id : null;
// Flatten the array so we can create a query and add the word ADD in between each element.
$built_query = implode(" AND ", $built_query);
// Run the query
$current_events = Calendar::whereBetween('date_from', [$start_time, $end_time])->orderBy('date_from')->whereRaw($built_query)->with('lessons', 'leadtutor', 'secondarytutor')->get();

Using json API pull to store in file or database

I am trying to pull data from the justin.tv API and store the echo I get in the below code in to a database or a file to be included in the sidebar of website. I am not sure on how to do this. The example of what I am trying to achieve is the live streamers list on the sidebar of teamliquid.net. Which I have done but doing it the way I have done it slows the site way down because it does about 50 json requests every time the page loads. I just need to get this in to a cached file that updates every 60 seconds or so. Any ideas?
<?php
$json_file = file_get_contents("http://api.justin.tv/api/stream/list.json?channel=colcatz");
$json_array = json_decode($json_file, true);
if ($json_array[0]['name'] == 'live_user_colcatz') echo 'coL.CatZ Live<br>';
$json_file = file_get_contents("http://api.justin.tv/api/stream/list.json?channel=coldrewbie");
$json_array = json_decode($json_file, true);
if ($json_array[0]['name'] == 'live_user_coldrewbie') echo 'coL.drewbie Live<br>';
?>
I'm not entirely sure how you would imagine this being cached, but the code below is an adaption of a block of code I've used in the past for some Twitter work. There are a few things that could probably be done better from a security perspective. Anyway, this gives you a generic way of grabbing the Feed, parsing through it, and then sending it to the database.
Warning: This assumes that there is a database connection already established within your own system.
(* Make sure you scroll to the bottom of the code window *)
/**
* Class SM
*
* Define a generic wrapper class with some system
* wide functionality. In this case we'll give it
* the ability to fetch a social media feed from
* another server for parsing and possibly caching.
*
*/
class SM {
private $api, $init, $url;
public function fetch_page_contents ($url) {
$init = curl_init();
try {
curl_setopt($init, CURLOPT_URL, $url);
curl_setopt($init, CURLOPT_HEADER, 0);
curl_setopt($init, CURLOPT_RETURNTRANSFER, 1);
} catch (Exception $e) {
error_log($e->getMessage());
}
$output = curl_exec($init);
curl_close($init);
return $output;
}
}
/**
* Class JustinTV
*
* Define a specific site wrapper for getting the
* timeline for a specific user from the JustinTV
* website. Optionally you can return the code as
* a JSON string or as a decoded PHP array with the
* $api_decode argument in the get_timeline function.
*
*/
class JustinTV extends SM {
private $timeline_document,
$api_user,
$api_format,
$api_url;
public function get_timeline ($api_user, $api_decode = 1, $api_format = 'json', $api_url = 'http://api.justin.tv/api/stream/list') {
$timeline_document = $api_url . '.' . $api_format . '?channel=' . $api_user;
$SM_init = new SM();
$decoded_json = json_decode($SM_init->fetch_page_contents($timeline_document));
// Make sure that our JSON is really JSON
if ($decoded_json === null && json_last_error() !== JSON_ERROR_NONE) {
error_log('Badly formed, dangerous, or altered JSON string detected. Exiting program.');
}
if ($api_decode == 1) {
return $decoded_json;
}
return $SM_init->fetch_page_contents($timeline_document);
}
}
/**
* Instantiation of the class
*
* Instantiate our JustinTV class, fetch a user timeline
* from JustinTV for the user colcatz. The loop through
* the results and enter each of the individual results
* into a database table called cache_sm_justintv.
*
*/
$SM_JustinTV = new JustinTV();
$user_timeline = $SM_JustinTV->get_timeline('colcatz');
foreach ($user_timeline AS $entry) {
// Here you could check whether the entry already exists in the system before you cache it, thus reducing duplicate ID's
$date = date('U');
$query = sprintf("INSERT INTO `cache_sm_justintv` (`id`, `cache_content`, `date`) VALUES (%d, '%s', )", $entry->id, $entry, $date);
$result = mysql_query($query);
// Do some other stuff and then close the MySQL Connection when your done
}