Apply coupon if user bought a specific product

Recently I had to do discount in WooCommerce if a user is bought a specific product – in this case “Membership” product.
The fallowing code will check if a user is bought a product with given id, and if so it will apply specified coupon code.

Just place the fallowing at the end of your functions.php in your theme:

// Apply coupon programmatically if specific product is bought

add_action( 'woocommerce_before_cart', 'auto_add_coupon' );

function auto_add_coupon() {
    global $woocommerce;

    $coupon_code = 'membership-cc';

    if ( $woocommerce->cart->has_discount( $coupon_code ) ) return;

    foreach ($woocommerce->cart->cart_contents as $key => $values ) {

    $target_product = array(5183);

		$current_user = wp_get_current_user();
		if (woocommerce_customer_bought_product($current_user->user_email, $current_user->ID, $target_product)){

        $woocommerce->cart->add_discount( $coupon_code );
        $woocommerce->show_messages();
    }
    }

}

WooCommerce can’t remove items from cart on Nginx

Just finished moving client site from Apache to Nginx, and upon testing I noticed when I try to remove some product from the cart, it wasn’t working . All is happening is to reload the page.

It turns out that the problem is in my Nginx config (as I suspected).

A really simple fix.

If you have section like this in your config file:

    location / {
                try_files $uri $uri/ /index.php;
        }

you will need to add ?$args after index.php, so it is going to look like this:

    location / {
                try_files $uri $uri/ /index.php?$args;
        }

Check the number of active connections on port 80

If you want to quickly check the number of the active connections on your server you can run this command:

netstat -anp | grep :80 | grep ESTABLISHED | wc -l

Don’t forget the semi-colon.
This will work also for all other ports for example SSH on the default 22, or HTTPS on 443.

And this one will list the number of the active connections per IP sorted

netstat -ntu | grep :80 | grep -v LISTEN | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -rn 

WordPress subdomain rewrite rules for Zeus web server

If you have to deal with the Zeus web server, using WordPress subdomain, you most probably noticed that the “pretty” permalinks include “/index.php/” whatever you do.

You will need to create file named “rewrite.script” in the root directory where WordPress is installed – pretty much the equivalent to “.htaccess” in Apache.

Use this in your rewrite.sript and replace the domain URL with your own.



RULE_3_START:
insensitive match IN:Host into % with ^www.sub.domain.com
if  matched then goto RULE_2_END
RULE_3_END:

RULE_2_START:
insensitive  match IN:Host into % with ^(.*).sub.domain.com
if matched then match URL  into $ with ^/(.*)
if not matched then goto RULE_2_END
set URL =  /%1/$1
RULE_2_END:


RULE_0_START:
# get the document root
map path into SCRATCH:DOCROOT from /
# initialize our variables
set SCRATCH:ORIG_URL = %{URL}
set SCRATCH:REQUEST_URI = %{URL}

# see if theres any queries in our URL
match URL into $ with ^(.*)\?(.*)$
if matched then
set SCRATCH:REQUEST_URI = $1
set SCRATCH:QUERY_STRING = $2
endif
RULE_0_END:

RULE_1_START:
# prepare to search for file, rewrite if its not found
set SCRATCH:REQUEST_FILENAME = %{SCRATCH:DOCROOT}
set SCRATCH:REQUEST_FILENAME . %{SCRATCH:REQUEST_URI}

# check to see if the file requested is an actual file or
# a directory with possibly an index. don?EUR(TM)t rewrite if so
look for file at %{SCRATCH:REQUEST_FILENAME}
if not exists then
look for dir at %{SCRATCH:REQUEST_FILENAME}
if not exists then
set URL = /index.php?q=%{SCRATCH:REQUEST_URI}
goto QSA_RULE_START
endif
endif

# if we made it here then its a file or dir and no rewrite
goto END
RULE_1_END:

QSA_RULE_START:
# append the query string if there was one originally
# the same as [QSA,L] for apache
match SCRATCH:ORIG_URL into % with \?(.*)$
if matched then
set URL = %{URL}&%{SCRATCH:QUERY_STRING}
endif
goto END
QSA_RULE_END:

After that you will need to go to the WordPress back-end and change your permalinks settings.

It might work with the main domain too – but I haven’t tested this.

Taxonomy Breadcrumbs

Add this function to the functions.php file in your current theme:

/* Taxonomy Breadcrumb */
function id_taxonomy_breadcrumb() {
// Get the current term
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
// Create a list of all the term's parents
$parent = $term->parent;
while ($parent):
$parents[] = $parent;
$new_parent = get_term_by( 'id', $parent, get_query_var( 'taxonomy' ));
$parent = $new_parent->parent;
endwhile;
if(!empty($parents)):
$parents = array_reverse($parents);
// For each parent, create a breadcrumb item
foreach ($parents as $parent):
$item = get_term_by( 'id', $parent, get_query_var( 'taxonomy' ));
$url = get_bloginfo('url').'/'.$item->taxonomy.'/'.$item->slug;
echo '<a href="'.$url.'">'.$item->name.'</a> / ';
endforeach;
endif;
// Display the current term in the breadcrumb
echo $term->name;
}

And the call it in the temlate:

<?php id_taxonomy_breadcrumb(); ?>

Set WordPress permissions recursively with CLI in Linux

To easily change the permissions, just navigate to the WordPress install folder, and execute this two commands:

#Set all directories recursively to 755:
find . -type d -exec chmod 0755 {} +

#Set all files recursively to 644:
find . -type f -exec chmod 0644 {} +

If you are not in the current directory you will need to add the desired one after find, something like:

find /top/path/here -type d -exec chown www-data:www-data {} \;

PHP Mail Headers

Most MTA/MUA’s insert a lot of extra headers; however, here is sort of the bare minimum you can expect.

From: 
Reply-To:
To: 
Subject:
Date: date("r");
MIME-Version:
Content-Type:

If you using HTML, then you should probably be using multipart messages–but it’s not strictly necessary.

Here is a snippet of code that shows how multi-part mime headers can be assembled without the help of a mailer class.

<?
$mime_boundary=md5(time());
$headers .= 'From: My Email <me@company.com>' . "\n";
$headers .= 'MIME-Version: 1.0'. "\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"".$mime_boundary."\"". "\n"; 
 
$msg .= "--".$mime_boundary. "\n";
$msg .= "Content-Type: text/plain; charset=iso-8859-1". "\n";
$msg .= "Content-Transfer-Encoding: 7bit". "\n\n";
$msg .= $textmessage . "\n\n";
 
$msg .= "--".$mime_boundary. "\n";
$msg .= "Content-Type: application/pdf; name=\"".$filename."\"". "\n";
$msg .= "Content-Transfer-Encoding: base64". "\n";
$msg .= "Content-Disposition: attachment; filename=\"".$filename."\"". "\n\n";
$msg .= chunk_split(base64_encode($doc)) . "\n\n";
$msg .= "--".$mime_boundary."--". "\n\n";
?>