Skip to content

Instantly share code, notes, and snippets.

@KeylorCR
Last active February 28, 2024 05:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KeylorCR/34a8cff25b7a287bf4f288c05d1e0ede to your computer and use it in GitHub Desktop.
Save KeylorCR/34a8cff25b7a287bf4f288c05d1e0ede to your computer and use it in GitHub Desktop.
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
*
* Add this snippet to your custom functions.php, then copy the
* template plugin_dir/templates/selected-store-details.php
* to your_theme/template-parts/selected-store-details.php
* and add this code to the template with the required field:
*
* // Wrap the value in {{{ _value_ }}} to allow HTML tags
* <# if ( data.description ) { #>
* <span><?= __('Description', 'wc-pickup-store') ?><span class="colon">:</span></span> {{{ data.description.value }}} <br>
* <# } #>
*
* @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 ) {
return __( 'My custom label', 'kmchild' );
}
add_filter( 'wps_shipping_method_label', 'kmchild_wps_shipping_title' );
add_filter( 'wps_store_checkout_label', 'kmchild_wps_shipping_title' );
/**
* Filtering first option for store select
*
* @version 1.6.1
* @author: @keylorcr
*/
function kmchild_wps_store_select_first_option() {
return __( 'Select a School', 'kmchild' );
}
add_filter( 'wps_store_select_first_option', 'kmchild_wps_store_select_first_option' );
/**
* 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 );
/**
* Editing args in store CPT
*
* @since 1.0.0
*/
function kmchild_register_store_post_type_args( $args ) {
$args['has_archive'] = false; // Remove archive page
return $args;
}
add_filter( 'register_store_post_type_args', 'kmchild_register_store_post_type_args' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment