WooCommerce add to cart URL

To add WooCommerce product into cart you can achieve via the following query parameters:

Solution thanks to https://wordpress.stackexchange.com/a/159390/33599

In source code at https://github.com/woocommerce/woocommerce/blob/d6e5c7e3b8a17e1e5a17defda208eb3b95b18891/includes/class-wc-form-handler.php#L715

Advertisements

SQL to get WooCommerce product categories (terms)

For an import script I needed to get WooCommerce product categories so I ended up looking the generated query in /var/log/mysql/query.log for get_the_terms($postID, ‘taxonomy_name’)

I ended up using the following SQL query to get product categories:

SELECT t.*, tt.*
FROM wp_terms AS t 
INNER JOIN wp_term_taxonomy AS tt ON tt.term_id = t.term_id 
INNER JOIN wp_term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id
WHERE tt.taxonomy IN ('product_cat') AND tr.object_id IN (11448)

WooCommerce email with custom total row

To add a custom total row to woocommerce email total section just hook into woocommerce_get_order_item_totals

Customise order total rows

add_filter('woocommerce_get_order_item_totals', 'my_woocommerce_get_order_item_totals', 10, 2);
function my_woocommerce_get_order_item_totals($total_rows, $order)
{
    $new_total_rows['cart_subtotal'] = $total_rows['cart_subtotal'];

    $new_total_rows['shipping'] = array(
        'label' => 'Slovak Parcel Service:',
        'value' => wc_price(3),
    );

    $gateway = wc_get_payment_gateway_by_order($order);
    if ('cod' == $gateway->id) {
        $new_total_rows['cod'] = array(
            'label' => 'Cash on delivery:',
            'value' => wc_price(1),
        );
    }

    $new_total_rows['order_total'] = $total_rows['order_total'];

    return $new_total_rows;
}

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;
    }
}

Fix language of WooCommerce ‘Thank You’ redirect URL for qTranslate

I struggled a bit with wrong translation of the thank you page. For translation I used qTranslate and IM8 qTranslate WooCommerce

Looking at the Network tab in Chrome I saw the 301 redirect, but it took a while to figure out that a missing slash (/) was doing that wrong redirect.

Screen Shot 2014-10-19 at 9.37.36 AM Screen Shot 2014-10-19 at 9.37.49 AM
I added the following to functions.php to fix the issue:

/**
 * Add slash before question mark due 301 redirect that prevents the lang query to pass
 *
 **/
function atc_payment_successful_result( $result ) {

 $result['redirect'] = str_replace( '?lang', '/?lang', $result['redirect'] );
 return $result;
}
add_filter( 'im8qtranslatewoocommerce_payment_redirect_filters', '__return_empty_array' ); // Prevent plugin filter
add_filter( 'woocommerce_payment_successful_result', 'atc_payment_successful_result' );

 

Get currency rates from ECB

For a WooCommerce powered site I needed a currency rate fetching function so here is what I used.

/**
 * Get currency rate
 *
 * Info @ http://www.ecb.int/stats/exchange/eurofxref/html/index.en.html
 * The reference rates are usually updated by 3 pm CET
 * cron: 0 17 * * 1,2,3,4,5
 *
 * @param   string  Currency code name
 * @return  float
 **/
function wi_get_currency_rate( $currency = 'CZK' ) {

    if ( false === ( $currencies = get_transient( 'wi_currency_rates' ) ) ) {

        $currencies = array();
        $xml = wp_remote_get( 'http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml',
            array( 'user-agent' => 'WordPress' )
        );

        $xml = simplexml_load_string( $xml['body'] );
        foreach( $xml->Cube->Cube->Cube as $cube )
            $currencies[ $cube->attributes()->currency ] = (float) $cube->attributes()->rate;

        set_transient( 'wi_currency_rates', $currencies, 23 * HOUR_IN_SECONDS );
    }


    return $currencies[ $currency ];

}