Created
April 14, 2013 06:37
-
-
Save claudiosanches/5381700 to your computer and use it in GitHub Desktop.
WooCommerce Correios - Only Sedex Add-on
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: WooCommerce Correios - Only Sedex Add-on | |
* Plugin URI: http://claudiosmweb.com/ | |
* Description: Este plugins serve para indicar que certos produtos não podem ser entregues por PAC | |
* Author: claudiosanches | |
* Author URI: http://claudiosmweb.com/ | |
* Version: 1.0 | |
* License: GPLv2 or later | |
*/ | |
/** | |
* Register only sedex metabox. | |
* | |
* @return void | |
*/ | |
function cs_correios_only_sedex_metabox() { | |
add_meta_box( | |
'only_sedex', | |
'Correios', | |
'cs_correios_only_sedex_metabox_content', | |
'product', | |
'side', | |
'default' | |
); | |
} | |
add_action( 'add_meta_boxes', 'cs_correios_only_sedex_metabox' ); | |
/** | |
* Only sedex metabox content. | |
* | |
* @param object $post Product data. | |
* | |
* @return string Metabox HTML. | |
*/ | |
function cs_correios_only_sedex_metabox_content( $post ) { | |
// Use nonce for verification. | |
wp_nonce_field( basename( __FILE__ ), 'cs_correios_only_sedex' ); | |
$html = '<label for="only_sedex">' . __( 'Entrega apenas por sedex:' ) . '</label> '; | |
$html .= sprintf( '<input type="checkbox" id="only_sedex" name="only_sedex" value="1"%s />', checked( 1, get_post_meta( $post->ID, 'only_sedex', true ), false ) ); | |
echo $html; | |
} | |
/** | |
* Save metabox data. | |
* | |
* @param int $post_id Post ID. | |
* | |
* @return void | |
*/ | |
function cs_correios_only_sedex_save_metabox( $post_id ) { | |
// Verify nonce. | |
if ( ! isset( $_POST['cs_correios_only_sedex'] ) || ! wp_verify_nonce( $_POST['cs_correios_only_sedex'], basename( __FILE__ ) ) ) { | |
return $post_id; | |
} | |
// Verify if this is an auto save routine. | |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { | |
return $post_id; | |
} | |
// Check permissions. | |
if ( 'product' == $_POST['post_type'] ) { | |
if ( ! current_user_can( 'edit_page', $post_id ) ) { | |
return $post_id; | |
} | |
} elseif ( ! current_user_can( 'edit_post', $post_id ) ) { | |
return $post_id; | |
} | |
if ( isset( $_POST['only_sedex'] ) ) { | |
update_post_meta( $post_id, 'only_sedex', $_POST['only_sedex'] ); | |
} else { | |
delete_post_meta( $post_id, 'only_sedex' ); | |
} | |
} | |
add_action( 'save_post', 'cs_correios_only_sedex_save_metabox' ); | |
/** | |
* Remove PAC with the package as only sedex argument. | |
* | |
* @param array $rates The Correios available rates. | |
* @param object $package Order items. | |
* | |
* @return array New rates. | |
*/ | |
function cs_remove_pac_with_is_only_sedex( $rates, $package ) { | |
foreach ( $package['contents'] as $item_id => $values ) { | |
if ( get_post_meta( $values['product_id'], 'only_sedex', true ) ) { | |
unset( $rates[0] ); | |
break; | |
} | |
} | |
return $rates; | |
} | |
add_filter( 'woocommerce_correios_shipping_methods', 'cs_remove_pac_with_is_only_sedex', 1, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment