WooCommerce check if product in cart is in stock

Check if product in cart is in stock:

foreach (WC()->cart->cart_contents as $item) {
    $product_id = isset($item['variation_id']) ? $item['variation_id'] : $item['product_id'];
    $product = new \WC_Product_Factory();
    $product = $product->get_product($product_id);
    if ($item['quantity'] > $product->get_stock_quantity()) {
        $inStock = false;
        break;
    }
}
Posted in Uncategorized, woocommerce | Tagged , , , | Leave a comment

WordPress custom search for custom post type

After creating a custom post type for an Inventory plugin the client needed also customized search for the columns.

Custom search for custom post type

First the pre_get_posts looked as a good candidate for hooking, but ended up with filtering via posts_clauses_request

add_filter('posts_clauses_request', 'customSearchClause');
function customSearchClause($query)
{
    global $wp_query;

    if (!is_search()) {
        return $query;
    }

    $term = $wp_query->query['s'];
    $query['join'] = 'INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)';
    $query['where'] = "AND wp_posts.post_type = '$this->post_type'
    AND (
        (wp_posts.post_title LIKE '%{$term}%')
        OR (wp_postmeta.meta_key = 'product' AND wp_postmeta.meta_value LIKE '%{$term}%')
        OR (wp_postmeta.meta_key = 'product_code' AND wp_postmeta.meta_value LIKE '%{$term}%')
    )";

    return $query;
}

Posted in Uncategorized, wordpress | Tagged , , | Leave a comment

Custom PHP errors with int value

I needed custom PHP errors set via .htaccess

<IfModule mod_php5.c>
php_value error_reporting 674
</IfModule>

According to docs about errors predefined constants I had to add the values of errors I wanted to see.
Inspiration for the number came from comment.

So I ran it via CLI (fastest possible way I think)

php -r 'echo E_WARNING | E_USER_WARNING | E_CORE_WARNING | E_COMPILE_WARNING;'

and finally got the int value I needed.

Posted in php, Uncategorized | Tagged , | Leave a comment

Turning XSD into documentation file

This XSD stuff is not for me, f*cking unreadable:/ Thanks god there is a stylesheet that makes it prettier, like a documentation.
First download it http://www.w3.org/2008/09/xsd.xsl due browser cross domain restriction, so you can avoid (fastest solution for me now)

Unsafe attempt to load URL https://www.w3.org/2008/09/xsd.xsl from frame with URL http://localhost/kovoinox/temp/stock.xsd. Domains, protocols and ports must match.

After download put it next to the XSD file and insert the following line as the 2nd row in the XSD:

<?xml version="1.0" encoding="Windows-1250"?>
<?xml-stylesheet type="text/xsl" href="xsd.xsl"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
 ... the rest of the XSD ...

After opening the XSD in the browser you get a much nicer interpretation.

ss_2016-08-03-16-16-23

Solution from http://stackoverflow.com/questions/6686124/how-to-turn-xsd-files-into-documentation-file

Posted in bestpractice, Uncategorized | Tagged , , | Leave a comment

Solving No matching global declaration available for the validation root.

Had to create a custom XML for Pohoda accounting software, but hit some errors so I decided to make a PHPUnit test for validating it against its XML schema definition (XSD) with PHPs DOMDocument::schemaValidate

/** @test */
public function validateStockXml()
{
$this->pohoda->createStockXml();
$xml = new \DOMDocument();
$xml->load(Pohoda::STOCK_XML_PATH);

$isValid = $xml->schemaValidate('http://www.stormware.cz/schema/version_2/stock.xsd');
$this->assertTrue($isValid);
}

Stuck with the following error for a couple of hours😦

Element ‘{http://www.stormware.cz/schema/version_2/data.xsd}dataPack’: No matching global declaration available for the validation root.

After a lot of trial and error decided to google more and more I finally found the solution at http://www.scriptscoop.me/t/81762e7e42e7/php-xml-validation-no-matching-global-declaration-available-for-the-valida.html

Changing the schema URL to http://www.stormware.cz/schema/version_2/data.xsd instead using http://www.stormware.cz/schema/version_2/stock.xsd solved the problem.

So easy, huh🙂 Always learning.

Posted in php, Uncategorized | Tagged , , , , | Leave a comment

Improving PHP autocompletion in VIM

After watching VIM as PHP IDE I revisited my config, specifically pimped ctags according to articles Vim Autocompletion for PHP and Exuberant ctags with PHP in Vim

So my current $HOME/.ctags is:

--recurse
--languages=php
--langmap=php:.inc.php
--fields=+aimS
--PHP-kinds=+cfd
--exclude=.svn
--exclude=.git
--exclude=cache
--exclude=tags
--exclude=min
--exclude=\*.min.\*
--exclude=node_modules
--regex-PHP=/abstract class ([^ ]*)/\1/c/
--regex-PHP=/interface ([^ ]*)/\1/c/
--regex-PHP=/trait ([^ ]*)/\1/c/
--regex-PHP=/(public |static |abstract |protected |private )+ function +([^ \(]*)/\2/f/
Posted in Uncategorized, vim | Tagged , , | Leave a comment

Enabling MySQL 5.7 query logging on OSX

I got used to it that sometimes I need to check the queries run by MySQL. After clean OSX upgrade I forgot to enable it.

So I added the following to /etc/mysql/my.cnf:

[mysqld]

sql_mode=

general_log = 1
general_log_file = /usr/local/mysql/data/mysqld.query.log

slow_query_log = 1
long_query_time = 1 # second
slow_query_log_file = /usr/local/mysql/data/mysqld.slow.log
log_queries_not_using_indexes = 0

The default one that comes with the package is at /usr/local/mysql/support-files/my-default.cnf

There’s also an option to set logging via SQL queries.

Posted in log, Uncategorized | Tagged , , | Leave a comment