Created
February 25, 2015 11:32
-
-
Save digitalchild/dbf176a266e14a09ea3f to your computer and use it in GitHub Desktop.
WC Vendors Per Product Shipping WPUF Hook code
This file contains 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 | |
/** | |
* wcv_pps_shipping_costs | |
* | |
* This is fired on save & edit of a product | |
* | |
*/ | |
function wcv_pps_shipping_costs ( $post_id ){ | |
// This is required for the rule order in PPS | |
$count = 0; | |
foreach( $_POST as $key => $value ) { | |
if ( strpos( $key, 'ship_cost' ) === 0) { | |
$count++; | |
} | |
} | |
// get the shipping rates | |
// This is the input name this can be whatever you want it to be | |
// You can have as many countries as you'd like here | |
// US & CA is just for an example | |
$us = $_POST['ship_cost_us']; | |
$ca = $_POST['ship_cost_ca']; | |
// Enable Per Product shipping for this product | |
update_post_meta( $post_id, '_per_product_shipping', 'yes' ); | |
// Store US Shipping if there is any | |
if ( !empty( $us) ) { | |
wcv_pps_dbcheck( $us, 'US', $count--, $post_id ); | |
} | |
// Store CA shipping if there is any | |
if ( !empty($ca) ) { | |
wcv_pps_dbcheck( $ca, 'CA', $count--, $post_id) ; | |
} | |
} | |
// Hook into the save & edit actions | |
add_action( 'wpuf_add_post_after_insert', 'wcv_pps_shipping_costs' ); | |
add_action( 'wpuf_edit_post_after_update', 'wcv_pps_shipping_costs' ); | |
/** | |
* wcv_pps_db | |
* | |
* This function will store the actual information into the pps table | |
* | |
*/ | |
function wcv_pps_db( $cost, $country, $order, $post_id, $action ) { | |
global $wpdb; | |
if (is_null($action)) { | |
$wpdb->insert( | |
$wpdb->prefix . 'woocommerce_per_product_shipping_rules', | |
array( | |
'rule_country' => esc_attr( $country ), | |
'rule_state' => '', | |
'rule_postcode' => '', | |
'rule_cost' => '', | |
'rule_item_cost' => esc_attr( $cost ), | |
'rule_order' => $order, | |
'product_id' => absint( $post_id ) | |
) | |
); | |
} else { | |
$wpdb->update( | |
$wpdb->prefix . 'woocommerce_per_product_shipping_rules', | |
array( | |
'rule_country' => esc_attr( $country ), | |
'rule_state' => '', | |
'rule_postcode' => '', | |
'rule_cost' => '', | |
'rule_item_cost' => esc_attr( $cost ), | |
'rule_order' => $order, | |
), | |
array( | |
'product_id' => absint( $post_id ), | |
'rule_id' => absint( $action ) | |
), | |
array( | |
'%s', | |
'%s', | |
'%s', | |
'%s', | |
'%s', | |
'%d' | |
), | |
array( | |
'%d', | |
'%d' | |
) | |
); | |
} | |
} | |
/** | |
* wcv_pps_dbcheck | |
* | |
* This will check to see if there is already an entry for this product if there is update it. | |
* | |
*/ | |
function wcv_pps_dbcheck( $cost, $country, $order, $post_id ) { | |
global $wpdb; | |
$tbl = $wpdb->prefix . 'woocommerce_per_product_shipping_rules'; | |
$exists = $wpdb->get_var(" | |
SELECT rule_id | |
FROM $tbl | |
WHERE product_id = $post_id | |
AND rule_country = '$country' | |
" | |
); | |
wcv_pps_db( $cost, $country, $order, $post_id, $exists ); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment