Skip to content

Instantly share code, notes, and snippets.

@KeylorCR
Last active Jun 25, 2022
Embed
What would you like to do?
List of filters included in the WC Pickup Store plugin. Check for filter availability in the plugin source code.
<?php
/**
* Add new content to be displayed in the template of store information in the Checkout page
*
* @author: @keylorcr
* @version 1.5.23
*/
function kmchild_wps_stores_fields($the_fields) {
foreach ($the_fields as $store_id => $value) {
$the_fields[$store_id][] = array(
'key' => 'content_info',
'value' => apply_filters('the_content', get_post_field('post_content', $store_id))
);
}
return $the_fields;
}
add_filter('wps_stores_fields', 'kmchild_wps_stores_fields');
/**
* Mapping custom fields for the Checkout template
*
* @author: @keylorcr
* @version 1.5.22
*/
function kmchild_wps_get_store_custom_fields($fields) {
$fields[] = 'description';
return $fields;
}
add_filter('wps_get_store_custom_fields', 'kmchild_wps_get_store_custom_fields');
/**
* Change Shipping and Store details label
*
* @author: @keylorcr
*/
function kmchild_wps_shipping_title($title) {
$post_data = wps_validate_ajax_request();
return 'My custom label';
}
add_filter('wps_shipping_method_label', 'kmchild_wps_shipping_title');
add_filter('wps_store_checkout_label', 'kmchild_wps_shipping_title');
/**
* Update WPS shipping costs
*
* @author: @keylorcr
*/
function kmchild_update_wps_shipping_costs($cost) {
return 75;
}
add_filter('wps_shipping_costs', 'kmchild_update_wps_shipping_costs', 50);
/**
* Edit WC Pickup Store shipping label
*
* @author: @keylorcr
* @version 1.7.1
*
* @param string $formatted_title Set title with costs
* @param string $title Raw title for shipping method
* @param float $calculated_costs
*/
function kmchild_wps_formatted_shipping_title( $formatted_title, $title, $calculated_costs ) {
return ( $calculated_costs == 0 ) ? $formatted_title . ' Free' : $title . '...etc';
}
add_filter('wps_formatted_shipping_title', 'kmchild_wps_formatted_shipping_title', 10, 3);
/**
* Hide shipping rates when free shipping is available, but keep "WC Pickup Store"
*/
function kmchild_hide_shipping_if_free_is_available( $rates, $package ) {
$new_rates = array();
foreach ( $rates as $rate_id => $rate ) {
// Check for free_shipping
if ( 'free_shipping' === $rate->method_id ) {
$new_rates[ $rate_id ] = $rate;
break;
}
}
if ( ! empty( $new_rates ) ) {
// Check for wc_pickup_store
foreach ( $rates as $rate_id => $rate ) {
if ('wc_pickup_store' === $rate->method_id ) {
$new_rates[ $rate_id ] = $rate;
break;
}
}
return $new_rates;
}
return $rates;
}
add_filter( 'woocommerce_package_rates', 'kmchild_hide_shipping_if_free_is_available', 10, 2 );
/**
* Disable payment gateway by chosen store
*
* @author @keylorcr
*/
function kmchild_wps_disable_payment_gateway_by_store( $payment_gateways ) {
if ( isset( WC()->session ) ) {
$store_name = WC()->session->get( 'shipping_store' );
$store_id = wps_get_store_id_by_name( $store_name );
// Array of stores where COD payment will be disabled
if ( in_array( $store_id, array( 79 ) ) ) {
unset( $payment_gateways['cod'] );
}
}
return $payment_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'kmchild_wps_disable_payment_gateway_by_store' );
/**
* Remove shipping method based on Country
*
* @since 1.0.0
* @link https://www.xadapter.com/woocommerce-shipping-hide-shipping-methods-based-destination-country/
*/
function kmchild_remove_shipping_methods_by_country( $available_shipping_methods, $package ) {
$customer_country = WC()->customer->get_shipping_country();
// Way #1: Disable by a country list
$country_list = array(
'CR' => array( 'wc_pickup_store' ),
);
if ( in_array( $customer_country , array_keys( $country_list ) ) ) {
if ( ! empty( $country_list[ $customer_country ] ) ) {
foreach ( $country_list[ $customer_country ] as $shipping_methods ) {
foreach ( $available_shipping_methods as $shipping_method => $value ) {
if ( strpos( $shipping_method, $shipping_methods ) !== false ) {
unset( $available_shipping_methods[ $shipping_method ] );
}
}
}
}
}
// Way #2: Enable only specific country
$country_list = array( 'CR' );
if ( ! in_array( $customer_country, $country_list ) ) {
unset( $available_shipping_methods['wc_pickup_store'] );
}
return $available_shipping_methods;
}
add_filter( 'woocommerce_package_rates', 'kmchild_remove_shipping_methods_by_country', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment