Category Archives: Wordpress

add postmeta on wp admin



add_filter('manage_post_posts_columns', function($columns) {
	return array_merge($columns, ['source_domain' => __('URL', 'textdomain')]);
});
 
add_action('manage_post_posts_custom_column', function($column_key, $post_id) {
	if ($column_key == 'source_domain') {
		$source_domain = get_post_meta($post_id, 'source_domain', true);
		//if ($source_domain == 1) {} else {}
		echo $source_domain;
	}
}, 10, 2);

As seen on

woocommerce – “I have read and agree to the website” always in english

This code that i’v found on github worked for me

if (!function_exists('My_terms_and_conditions_checkbox_text')) {
    function My_terms_and_conditions_checkbox_text( ) {
        return sprintf( __( 'I have read and agree to the website %s', 'woocommerce' ), '[terms]' ) ;
    }
}
add_filter('woocommerce_get_terms_and_conditions_checkbox_text', 'My_terms_and_conditions_checkbox_text', 10, 3);

https://github.com/woocommerce/woocommerce/issues/22127#issue-387480392

woocommerce remove shipping if product of X shipping method is in cart

So, if we need to hide shipping, if the buyer has some item of specific shipping class, in this case ‘recolha-em-armazem’, we can use this code, on functions.php of our theme.

// Replace 'shipping-class' with the specific shipping class you want to force the local pick up upon
// To make it more readable, replace $shippingClass with your class name, eg : $hugeProducts

function my_hide_shipping_when_local_is_available( $rates ) {
    $cart_items = WC()->cart->get_cart();
    $shippingClass = false;
    
    foreach ( $cart_items as $cart_item ) {
		$product = $cart_item['data'];
		$class   = $product->get_shipping_class();
		
		if ( 'recolha-em-armazem' == $class ) {
			$shippingClass = true;
		}
	}
    
	$local = array();
	foreach ( $rates as $rate_id => $rate ) {
		if ('local_pickup' === $rate->method_id ) {
            $local[ $rate_id ] = $rate;
        }
	}
    
    if ( !empty($local) && ($shippingClass == true) ) {
        return $local;
    } else {
        return $rates;
    }

}

add_filter( 'woocommerce_package_rates', 'my_hide_shipping_when_local_is_available', 100 );

As seen on https://gist.github.com/kane-c/5e4d53601564ed667fb9

I had to create a custom js code to select the shipping method, ’cause buyer wasn’t able to select the shipping method… this snippet might not be need… or u need to tweak it a little bit…

jQuery(document).ready(function( $ ){ 
   if ( $('#shipping_method li').length == 1 )
   {
      setInterval(function() {
 
jQuery('#shipping_method_0_local_pickup3').clone().attr('type','radio').insertAfter('#shipping_method_0_local_pickup3').prev().remove();

      jQuery('#shipping_method_0_local_pickup3').attr('checked',true);
      jQuery('p.woocommerce-shipping-destination').hide();
      jQuery('form.woocommerce-shipping-calculator').hide();
              
              }, 500);
    }