Skip to content

Instantly share code, notes, and snippets.

View pablo-sg-pacheco's full-sized avatar

Pablo dos Santos Gonçalves Pacheco pablo-sg-pacheco

View GitHub Profile
@pablo-sg-pacheco
pablo-sg-pacheco / functions.php
Last active April 12, 2024 12:38
Wordpress - Password protect a custom post type programmatically
<?php
/**
* Password protects a custom post type programmatically.
*
* @param $post
*/
add_action( 'the_post', function( $post ){
if ( $post->post_type != 'post' ) {
return;
}
@pablo-sg-pacheco
pablo-sg-pacheco / functions.php
Created August 13, 2020 20:28
Change WooCommerce Coupon amount on the fly (programmatically)
<?php
add_filter('woocommerce_get_shop_coupon_data', 'fix_wc_coupon_discount_amount', 10, 3);
function fix_wc_coupon_discount_amount( $false, $data, $coupon ) {
if (
'yes' !== get_option( 'wcj_multicurrency_compatibility_wc_coupons', 'no' )
|| is_admin()
|| empty( $coupon_id = wc_get_coupon_id_by_code( $data ) )
|| 'fixed_cart' != get_post_meta( $coupon_id, 'discount_type', true )
) {
return $false;
@pablo-sg-pacheco
pablo-sg-pacheco / main-plugin-file.php
Last active December 19, 2022 21:23
Custom deactivation/activation hooks from a WordPress plugin that can be called from some other hooks
<?php
// Custom deactivation/activation hooks.
$activation_hook = 'plugin_prefix_on_activation';
$deactivation_hook = 'plugin_prefix_on_deactivation';
register_activation_hook( __FILE__, function () use ( $activation_hook ) {
add_option( $activation_hook, 'yes' );
} );
register_deactivation_hook( __FILE__, function () use ( $deactivation_hook ) {
do_action( $deactivation_hook );
} );
@pablo-sg-pacheco
pablo-sg-pacheco / functions.php
Last active February 3, 2021 15:55
WordPress - Convert array to string
<?php
/**
* converts array to string.
*
* @param $arr
* @param array $args
*
* @return string
*/
function convert_array_to_string( $arr, $args = array() ) {
@pablo-sg-pacheco
pablo-sg-pacheco / functions.php
Last active November 11, 2020 17:26
Frete grátis por região (faixa de cep) - Woocommerce
<?php
//Testa uma faixa de cep
function checkCpfRange($valor, $intervalos) {
$v = (int) preg_replace("/\D+/", "", $valor);
foreach ($intervalos as $range):
list($min, $max) = $range;
if ($v >= $min && $v <= $max)
return true;
endforeach;
return false;
@pablo-sg-pacheco
pablo-sg-pacheco / functions.php
Created June 8, 2020 17:29
Booster for WooCommerce - Get info from Prices and Currencies by Country Module Options
<?php
add_action('wp_footer',function(){
if ( !WCJ()->modules['price_by_country']->is_enabled() ) {
return;
}
$country = WCJ()->modules['price_by_country']->core->get_customer_country_by_ip();
$group = WCJ()->modules['price_by_country']->core->get_customer_country_group_id();
$the_result = array('country'=>$country,'group'=>$group);
?>
<script>
@pablo-sg-pacheco
pablo-sg-pacheco / functions.php
Last active May 28, 2020 17:13
Woocommerce - Direct Bank Transfer - Add fields on thank you page and order received email
<?php
/**
* Adds CNPJ and Company Name fields on the Thank You page and Order Received Email when using 'Direct Bank Transfer' payment gateway.
*/
add_filter( 'woocommerce_bacs_account_fields', function ( $fields ) {
$fields['cnpj']['label'] = __( 'CNPJ', 'domain' );
$fields['cnpj']['value'] = '99.999.999/9999-99';
$fields['company_name']['label'] = __( 'Razao Social', 'domain' );
$fields['company_name']['value'] = 'Uma empresa doce e bacana Ltda';
return $fields;
@pablo-sg-pacheco
pablo-sg-pacheco / wp-delete-transients-by-prefix.php
Last active January 31, 2020 18:10
Delete WordPress Transients by Prefix
<?php
/**
* Deletes transients that match a specific prefix.
*
* @author Pablo Pacheco <pablo.sg.pacheco@gmail.com>
* @param string $prefix prefix to search for.
* @return int|bool Boolean true for CREATE, ALTER, TRUNCATE and DROP queries. Number of rows affected/selected for all other queries. Boolean false on error.
*/
function delete_transients_by_prefix($prefix){
@pablo-sg-pacheco
pablo-sg-pacheco / functions.php
Created October 11, 2019 19:46 — forked from TimBHowe/functions.php
Forcibly remove the tax line item and calculation from the cart. (suggest just using the GEO setting in WooCommerce)
<?php
// Remove the Tax Line item from the cart.
function wc_remove_cart_tax_totals( $tax_totals, $instance ) {
if( is_cart() ) {
$tax_totals = array();
}
return $tax_totals;
}
@pablo-sg-pacheco
pablo-sg-pacheco / Settings
Last active November 5, 2018 18:33
Product Input Fields for WooCommerce - Display fields one above the other
<tr><td><div><label for="%field_id%">%title%</label></div>%field%</td></tr>