Skip to content

Instantly share code, notes, and snippets.

View NiklasHogefjord's full-sized avatar

Niklas Högefjord NiklasHogefjord

View GitHub Profile
@NiklasHogefjord
NiklasHogefjord / gist:6104328
Created July 29, 2013 13:33
WooCommerce - Display cart total weight on the cart page
<?php
/**
* WooCommerce
* --------------
*
* Display cart total weight on the cart page
*
*/
add_action('woocommerce_cart_collaterals', 'myprefix_cart_extra_info');
@NiklasHogefjord
NiklasHogefjord / gist:82f4f1a612af6356d187
Created July 2, 2015 13:47
Klarna for WooCommerce - Don't display the Klarna Checkout form if cart is less than the specified amount.
/**
* WooCommerce - Klarna payment gateway
* Don't display the Klarna Checkout form if cart is less than the specified amount.
* Useful when you only want to have KCO as an available payment method for orders above a specific order amount.
*
**/
// Check to see if the KCO checkout should be displayed
add_filter('klarna_kco_checkout', 'my_klarna_kco_checkout');
@NiklasHogefjord
NiklasHogefjord / klarna-checkout-extra-fields.php
Last active September 16, 2019 13:19
Add custom input fields to Klarna Checkout page. Added via shortcode [woocommerce_klarna_checkout_extra_fields]. Saved as post_meta to the order directly on .blur via ajax.
<?php
/**
* Plugin Name: Klarna Checkout Extra Fields
* Plugin URI: http://krokedil.com
* Description: Add custom input fields to Klarna Checkout page. Added via shortcode [woocommerce_klarna_checkout_extra_fields]. Saved as post_meta to the order directly on .blur via ajax.
* Version: 1.1
* Author: Krokedil
* Author URI: http://krokedil.com
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
@NiklasHogefjord
NiklasHogefjord / wc-klarna-disable-autofocus.php
Created October 7, 2015 15:03
Klarna for WooCommerce - Filter Create order object sent to Klarna for disabling autofocus of email field
/**
* WooCommerce - Klarna payment gateway
* Filter Create order for disabling autofocus of email field.
**/
// New order ($create)
add_filter('kco_create_order', 'my_kco_create_order');
function my_kco_create_order( $create ) {
$create['gui']['options'] = array('disable_autofocus');
@NiklasHogefjord
NiklasHogefjord / wc-specter-remove-article-type-in-product-sync.php
Created May 15, 2018 14:06
Remove articleType as parameter sent to Specter on product updates. This can be useful if some articles are marked as "paketartiklar" in Specter (since WooCommerce don't have support for that).
<?php
add_filter( 'wc_specter_send_custom_product_data', 'my_custom_product_data', 10, 3 );
function my_custom_product_data( $params, $product, $is_variation = false ) {
/**
* $params is the product data stored as an array.
* $product is the product object. Product variation object if this is a product variation.
* $is_variation is true/false depending on if this is a product variation or not.
*/
// Unset articleType type so we don't send that to Specter when product is updated in WooCommerce.
@NiklasHogefjord
NiklasHogefjord / wc-kco-v2-required-checkbox.php
Last active May 11, 2018 11:24
Display a checkbox in KCO that is required to check to be able to complete purchase. This is used for the WooCommerce Klarna gateway (v2).
<?php
// Add a cehckbox to KCO iframe that is required to check before the purchase can be completed
add_filter('kco_create_order', 'krokedil_add_required_checkbox');
add_filter('kco_update_order', 'krokedil_add_required_checkbox');
function krokedil_add_required_checkbox( $create ) {
$create['options']['additional_checkbox']['text'] = 'I agree to the <a href="https://example.com/terms" target="_blank">terms</a> and data policy.';
$create['options']['additional_checkbox']['checked'] = false;
$create['options']['additional_checkbox']['required'] = true;
return $create;
}
@NiklasHogefjord
NiklasHogefjord / wc-specter-add-customer-note-as-published-comment.php
Created April 4, 2018 13:26
Add WooCommerce customer note to publishedComment in order data sent to Specter
<?php
/**
* Filter hook wc_specter_published_comment
* Modify publishedComment sent to Specter on new order submission.
* publishedComment is displayed on the order/packing slip that can be sent to the customer.
*
**/
add_filter( 'wc_specter_published_comment', 'my_wc_specter_published_comment', 10, 2 );
function my_wc_specter_published_comment( $published_comment, $order ) {
@NiklasHogefjord
NiklasHogefjord / wc-kco-validate-customer-age.php
Last active January 8, 2018 16:18
Code example on how to only accept purchases from people over 18 years of age with Klarna Checkout and WooCommerce
<?php
/**
* WooCommerce - Klarna payment gateway
* Only accept purchases from customer over 18 years of age.
**/
// Edit the order data sent to Klarna to make 'Date of birth' a mandatory field in the KCO iframe
add_filter('kco_create_order', 'my_kco_create_order');
function my_kco_create_order( $create ) {
$create['options']['national_identification_number_mandatory'] = true;
@NiklasHogefjord
NiklasHogefjord / wc-klarna-change-mandatory-fields-and-fetch-order-data-php
Last active January 8, 2018 13:26
Filter Create order & Update order sent to Klarna to change mandatory fields in the checkout iframe (Date of birth & Title). Also fetching order data returned from Klarna after a successful payment and updating the local WooCommerce order.
<?php
/**
* WooCommerce - Klarna payment gateway (Klarna Checkout)
* Filter Create order & Update order sent to Klarna to change mandatory fields in the checkout iframe (Date of birth & Title).
* Also fetching order data returned from Klarna after a successful payment and updating the local WooCommerce order.
*
* Learn more about available params in order object sent to Klarna here:
* V2 - https://developers.klarna.com/en/se/kco-v2/checkout-api
* V3 - https://developers.klarna.com/api/#checkout-api
*
@NiklasHogefjord
NiklasHogefjord / wc-specter-pay-type-identifier.php
Last active October 19, 2017 12:09
Filter the payTypeIdentifier sent to Specter
/**
* Specter for WooCommerce
* Filter the payTypeIdentifier sent to Specter
*
**/
add_filter('wc_specter_pay_type_identifier', 'my_wc_specter_pay_type_identifier', 10, 2);
function my_wc_specter_pay_type_identifier( $paytype, $order_id ) {
// Switches the paytype to D (cash payment) for all orders that uses Cach on Delivery (originally paytype P in the extension) as the payment method.