Skip to content

Instantly share code, notes, and snippets.

View rynaldos-zz's full-sized avatar

Rynaldo rynaldos-zz

View GitHub Profile
@rynaldos-zz
rynaldos-zz / wc_pk_country_states.php
Last active April 22, 2024 12:42
Pakistan state/city extension for checkout page and shipping zones. Replaces states for cities
add_filter( 'woocommerce_countries', 'rs_edit_pk_country' );
function rs_edit_pk_country ( $countries ) {
$new_countries = array(
'PK' => __( 'Pakistan', 'woocommerce' ),
);
return array_merge( $countries, $new_countries );
}
add_filter( 'woocommerce_continents', 'rs_add_new_pk_country_to_continents' );
@rynaldos-zz
rynaldos-zz / wc-zero-tax-line.php
Created January 17, 2019 10:28
Show zero rate tax line in checkout when exempt
add_filter( 'woocommerce_cart_tax_totals', 'show_zero_rate_taxes' );
function show_zero_rate_taxes( $tax_totals ) {
if ( empty( $tax_totals ) && WC()->customer->is_vat_exempt() ) {
$tax_totals['zero-rated'] = (object) array(
'label' => 'Zero Rated Tax',
'amount' => 0,
'formatted_amount' => wc_price( 0 ),
'name' => 'Zero Rated Tax',
@rynaldos-zz
rynaldos-zz / wcc-ap-php
Last active September 20, 2021 23:53
[WooCommerce 3.0] show categories / links above product title in shop / archive page
add_action( 'woocommerce_before_shop_loop_item_title', 'add_category_title', 25);
function add_category_title()
{
global $product;
$product_cats = wp_get_post_terms($product->get_id(), 'product_cat');
$count = count($product_cats);
foreach($product_cats as $key => $cat)
{
echo
'<span class="wcc_category_title">
@rynaldos-zz
rynaldos-zz / wc_no-atc-per=product-cat.php
Last active September 20, 2021 23:53
[WooCommerce] Hide "add to cart" button on certain product categories
add_action( 'woocommerce_after_shop_loop_item', 'remove_add_to_cart_buttons', 1 );
function remove_add_to_cart_buttons() {
// replace a_category and another_category with the slugs of the categories you'd like to have the button removed from
if( is_product_category( array( 'a_category', 'another_category'))) {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
}
}
// Did this help? Donate me some BTC: 1BEsm8VMkYhSFJ92cvUYwxCtsfsB2rBfiG
@rynaldos-zz
rynaldos-zz / nopricerange-from.php
Created January 27, 2016 14:18
remove price range and add prepending text to base price
function woo_ninja_remove_price_range( $price, $product ) {
// Main Price
$prices = array( $product->get_variation_price( 'min', true ), $product->get_variation_price( 'max', true ) );
$price = $prices[0] !== $prices[1] ? sprintf( __( '%1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
// Sale Price
$prices = array( $product->get_variation_regular_price( 'min', true ), $product->get_variation_regular_price( 'max', true ) );
sort( $prices );
$saleprice = $prices[0] !== $prices[1] ? sprintf( __( '%1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
@rynaldos-zz
rynaldos-zz / wc-sold-label-on-archive-pages.php
Last active March 23, 2021 17:50
[WooCommerce] Display 'SOLD' label on product archive pages (out of stock items)
add_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_stock', 10 );
function woocommerce_template_loop_stock() {
global $product;
if ( ! $product->managing_stock() && ! $product->is_in_stock() )
echo '<p class="stock out-of-stock">SOLD</p>';
}
@rynaldos-zz
rynaldos-zz / wc-redirect-atc-checkout.php
Last active March 23, 2021 17:47
[WooCommerce] Redirecting from add-to-cart to checkout page
function my_custom_add_to_cart_redirect( $url ) {
$url = WC()->cart->get_checkout_url();
// $url = wc_get_checkout_url(); // since WC 2.5.0
return $url;
}
add_filter( 'woocommerce_add_to_cart_redirect', 'my_custom_add_to_cart_redirect' );
@rynaldos-zz
rynaldos-zz / wc-sort-price-cost.php
Last active March 23, 2021 17:45
[WooCommerce] Sort the shipping prices by cost
add_filter( 'woocommerce_package_rates' , 'sort_woocommerce_available_shipping_methods_by_cost', 10, 2 );
function sort_woocommerce_available_shipping_methods_by_cost( $rates, $package ) {
if ( ! $rates ) {
return;
}
$tmp = array();
foreach( $rates as $rate ) {
$tmp[] = $rate->cost;
}
@rynaldos-zz
rynaldos-zz / add-ane-shipping-methods.php
Last active March 23, 2021 17:44
[WooCommerce] Add the Shipping method to advanced notifications emails
// Place the following code in your theme's functions.php file to add the shipping method to ALL emails
add_action( 'woocommerce_email_after_order_table', 'wc_add_shipping_method_to_emails', 15, 2 );
function wc_add_shipping_method_to_emails( $order, $is_admin_email ) {
echo '<p><strong>Shipping Method:</strong> ' . $order->get_shipping_method() . '</p>';
}
// Place the following code in your theme's functions.php file to add the shipping method to ADMIN emails only
add_action( 'woocommerce_email_after_order_table', 'wc_add_shipping_method_to_admin_emails', 15, 2 );
@rynaldos-zz
rynaldos-zz / sample.only
Created February 17, 2017 18:30
[WooCommerce advanced notifications] Include payments / shipping type in email
<?php
/**
* New order email
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// allowed tags for escaping
$allowed_tags = array(