Disable checkout page for not logged in users in specific countries - authentication

I have a shop in WooCommerce and I try to disable the check-out page for some countries (France and Greece). These countries should be forced to log-in as b2b customers in order to continue their check-out.
I use this code, but unfortunately the action applies to all countries and not just the two I need (France and Greece):
add_action( 'template_redirect', 'checkout_redirect_non_logged_to_login_access');
function checkout_redirect_non_logged_to_login_access() {
global $woocommerce;
$msg_states = array( 'BE', 'BG', 'DE', 'DK', 'EE', 'FI', 'IT', 'HR', 'LI', 'LT', 'MT', 'IE', 'IS', 'NL', 'NO', 'PT', 'RO', 'CH', 'SK', 'SI', 'CZ', 'HU', 'GB', 'AT' );
// Here the conditions (woocommerce checkout page and unlogged user)
if( is_checkout() && !is_user_logged_in() && !in_array( WC()->customer->get_billing_country, $msg_states ) ) {
// Redirecting to your custom login area
wp_redirect( get_permalink( get_option('woocommerce_myaccount_page_id') ) );
// always use exit after wp_redirect() function.
exit;
}
}
Is there any possibility to differentiate the kind of log-in, from a normal user to b2b client?

Related

{ scientistSeedError: 'syntax error at or near "["' }

I am trying to add some data to a table in postgresql through a seed file I've created. all the routes work and I already created another table. However, on this particular table I'm trying to add an array in one of the columns, but I keep getting this error. Does anyone has an idea of what it could be? or how can I inform a postgresql table I want to add some arrays:
```{ scientistSeedError: 'syntax error at or near "["' }````
below is the query to create the table
const query =
CREATE TABLE "scientist" (
"id" serial NOT NULL,
"first_name" varchar(255) NOT NULL,
"last_name" varchar(255) NOT NULL,
"area_expertise" varchar(255) NOT NULL,
"areaExpertise" TEXT ARRAY NOT NULL,
"field" TEXT ARRAY NOT NULL,
"wiki_link" varchar(255) NOT NULL,
"picture" varchar(255) NOT NULL,
"short_description" varchar(400),
CONSTRAINT "scientist_pk" PRIMARY KEY ("id")
) WITH (
OIDS=FALSE
);`
;
try {
await db.query(query);
res.send("Database successfully created");
} catch(e){
console.log(e.message)
}
};```
**and below a fragment of the query to add the data. As is too large to post here but**
```module.exports.seedPart2 = async (req, res, next) => {
// Scientist query
const scientistsQuery = `
INSERT INTO scientist (first_name,
last_name,
area_expertise,
field,
issue_tackled,
wiki_link,
picture) VALUES
('Eugenie', 'Clark', 'Ichthyologist', 'biology', 'Environmental preservation', 'https://en.wikipedia.org/wiki/Eugenie_Clark', 'http://www.alertdiver.com/cdn/13649.jpg'),
('Susan', 'Solomon', 'Atmospheric Chemist', 'Chemistry', ['Environmental preservation', 'Chemistry'], 'https://en.wikipedia.org/wiki/Susan_Solomon' , 'https://upload.wikimedia.org/wikipedia/commons/thumb/8/80/Susan_Solomon-Desk_With_Globe.jpg/1024px-Susan_Solomon-Desk_With_Globe.jpg'),
('Donna Theo', 'Strickland', 'Physics, Optics, and Lasers', 'Physics', ['Medicine', 'New technologies'], 'https://en.wikipedia.org/wiki/Donna_Strickland', 'https://upload.wikimedia.org/wikipedia/commons/thumb/3/38/Donna_Strickland_EM1B5760_%2846183560632%29_%28cropped%29.jpg/220px-Donna_Strickland_EM1B5760_%2846183560632%29_%28cropped%29.jpg'),
('Linda', 'Brown Buck', 'Biologist', 'Biology', 'Medicine', 'https://en.wikipedia.org/wiki/Linda_B._Buck', 'https://upload.wikimedia.org/wikipedia/commons/thumb/2/20/Linda_Buck_2015_%28cropped%29.jpg/200px-Linda_Buck_2015_%28cropped%29.jpg')`;
try {
const {rows} = await db.query(scientistsQuery);
res.status(201).json(rows)
} catch (e) {
console.log({ scientistSeedError: e.message });
}
};
Thank you for any help!!!
Array literals in PosgreSQL are denoted by curly braces, not square braces (see the documentation for details):
const scientistsQuery = `
INSERT INTO scientist (first_name,
last_name,
area_expertise,
field,
issue_tackled,
wiki_link,
picture) VALUES
('Eugenie', 'Clark', 'Ichthyologist', 'biology', 'Environmental preservation', 'https://en.wikipedia.org/wiki/Eugenie_Clark', 'http://www.alertdiver.com/cdn/13649.jpg'),
('Susan', 'Solomon', 'Atmospheric Chemist', 'Chemistry', '{"Environmental preservation", "Chemistry"}', 'https://en.wikipedia.org/wiki/Susan_Solomon' , 'https://upload.wikimedia.org/wikipedia/commons/thumb/8/80/Susan_Solomon-Desk_With_Globe.jpg/1024px-Susan_Solomon-Desk_With_Globe.jpg'),
('Donna Theo', 'Strickland', 'Physics, Optics, and Lasers', 'Physics', '{"Medicine", "New technologies"}', 'https://en.wikipedia.org/wiki/Donna_Strickland', 'https://upload.wikimedia.org/wikipedia/commons/thumb/3/38/Donna_Strickland_EM1B5760_%2846183560632%29_%28cropped%29.jpg/220px-Donna_Strickland_EM1B5760_%2846183560632%29_%28cropped%29.jpg'),
('Linda', 'Brown Buck', 'Biologist', 'Biology', 'Medicine', 'https://en.wikipedia.org/wiki/Linda_B._Buck', 'https://upload.wikimedia.org/wikipedia/commons/thumb/2/20/Linda_Buck_2015_%28cropped%29.jpg/200px-Linda_Buck_2015_%28cropped%29.jpg')`;

How do i add whatsapp api link to email orders using the customer phone number?

I would like to add whatsapp link to admin complete orders email, that takes the customer phone number, and adds it to the whatsapp api link.
The idea is that, when i receive notification about order, i can contact my customer using whatsapp and let him know i got his order.
so it should look like this:
api.whatsapp.com/send?phone= {{customer phone number}} &text=...
Add the follows code snippet in your active theme's functions.php and changes the text messages as per you.
function add_wp_link_woocommerce_completed_order_email( $order, $sent_to_admin, $plain_text, $email ) {
if ( $email->id == 'customer_completed_order' || $email->id == 'new_order' ) {
$link = 'https://wa.me/'.$order->get_billing_phone( 'edit' ).'/?text='.urlencode( 'your text messages' );
echo '<div style="margin-bottom: 40px;">
<h2>'.__( 'Customer WhatsApp link', 'text-domain' ) .'</h2>
<p>'.__( 'Contact', 'text-domain' ).'</p>
</div>';
}
}
add_action( 'woocommerce_email_customer_details', 'add_wp_link_woocommerce_completed_order_email', 99, 4 );

multi variable in where clause in Laravel

I am building a query to give a row with multi variables in Laravel.
select * from Fruit where apple = red and banana = raw and orange = 1
how could I create a SQL query into Laravel in this condition?
$result = Fruit::where('apple ', red , 'banana ', raw , 'orange ', 1);
If you are using a recent version of Laravel, then you may define an associative array containing the conditions:
$conditions = ['apple' => 'red', 'banana' => 'raw', 'orange' => '1'];
$result = Fruit::where($conditions)->get();
This assumes that, as you have stated, you want to AND together some number of criteria only.
You can do it some ways
Solution 1:
$result = Fruit::whereApple('red')->whereBanana('raw')->whereOrange('1')->get();
Solution 2:
$result = Fruit::where('apple', 'red')->where('banana', 'raw')->where('orange', '1')->get();
Solution 3:
$result = Fruit::where([
'apple' => 'red',
'banana' => 'raw',
'orange' => '1'
])->get();
Solution 4:
$result = Fruit::where([
['apple', '=', 'red'],
['banana', '=', 'raw'],
['orange', '=', '1']
])
->get();

Only join rows that meet certain criteria (left join of data that is left joined)

I have users that have FacebookPage and Application entities associated to them. FacebookPage entities also have Application entities associated with them.
$qb = $this->createQueryBuilder('u')
->addSelect('p', 'a')
->leftJoin('u.facebook_pages', 'p')
->leftJoin('p.applications', 'a')
->leftJoin('a.editors', 'e', 'WITH', 'e = :user')
->where('u = :user')
->setParameter('user', $user)
->orderBy('p.name', $order)
;
$u = $qb->getQuery()->getSingleResult();
return $u->getFacebookPages();
I want to return all of the user's Facebook pages and then show a count of applications associated with that page. I only want to count the applications where the user is an editor.
When run my current query, the pages have a count with all the page's applications (so the count includes applications where the user is not an editor).
If I change the a.editors leftJoin to a join, I get the correct counts for the pages, but it only lists pages that actually have any applications.
How can I show all the pages and only join the applications where the user is an editor?
I solved this by adding a new query to get app ids for the applications where user is an editor.
$user_apps = $this->getEntityManager()->getRepository('Application')->getUserApps($user);
$app_ids = array();
foreach($user_apps as $app) {
$app_ids[] = $app->getId();
}
$qb = $this->createQueryBuilder('u')
->addSelect('p', 'a')
->leftJoin('u.facebook_pages', 'p');
if(count($app_ids)) {
// join with IN statement to include only apps where user is an editor
$qb->leftJoin('p.applications', 'a', 'WITH', sprintf('a.id IN (%s)', implode(',', $app_ids)));
} else {
// join empty anyway to avoid db calls when iterating over pages
$qb->leftJoin('p.applications', 'a');
}
$qb
->where('u = :user')
->setParameter('user', $user)
->orderBy('p.name', $order)
;
try {
$u = $qb->getQuery()->getSingleResult();
} catch (Exception $e) {
throw $e;
}
return $u->getFacebookPages();
This now returns all the user's Facebook pages and only join applications where user is an editor. I am probably going to change this to a DQL query so I can move the ids in IN to a subquery.

How do I save xml to mysql database?

I found this from original link:
http://www.ecb.europa.eu/stats/exchange/eurofxref/html/index.en.html#dev
e.g.
<?php
function StartElement($parser, $name, $attrs) {
if (!empty($attrs['RATE'])) {
echo "1€=".$attrs['RATE']." ".$attrs['CURRENCY']."<br />";
}
}
$xml_parser= xml_parser_create();
xml_set_element_handler($xml_parser, "StartElement", "");
// for the following command you will need file_get_contents (PHP >= 4.3.0)
// and the config option allow_url_fopen=On (default)
xml_parse($xml_parser, file_get_contents ("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"));
xml_parser_free($xml_parser);
?>
then would be look like:
INSERT INTO `curr_table` (`curr_id`, `curr_title`, `Code`, `decimal_place`, `value`) VALUES
(1, 'EURO', 'EUR', '2', 1.3917),
(2, 'Japan Yen', 'JPY', '2', 112.88),
(3, 'Bla..bla', 'BGN', '2', 1.9558),
..............................etc
(20, 'Bla..bla.', 'CZK', '2', 24.575);
Any pointer and samples code would be appreciate and thanks in advance
If you are using MySQL 5.1 or newer, there is an XML support:
http://dev.mysql.com/tech-resources/articles/xml-in-mysql5.1-6.0.html#xml-5.1-in-and-out