예제 #1
0
 /**
  * Save the meta when the post is saved.
  *
  * @param int $post_id The ID of the post being saved.
  */
 public function save($post_id)
 {
     global $post, $wpdb;
     // Check if our nonce is set.
     if (!isset($_POST['woocommerce_giftcard_nonce'])) {
         return $post_id;
     }
     $nonce = $_POST['woocommerce_giftcard_nonce'];
     // Verify that the nonce is valid.
     if (!wp_verify_nonce($nonce, 'woocommerce_save_data')) {
         return $post_id;
     }
     // If this is an autosave, our form has not been submitted,
     //     so we don't want to do anything.
     if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
         return $post_id;
     }
     // Check the user's permissions.
     if ('rp_shop_giftcard' == $_POST['post_type']) {
         if (!current_user_can('edit_page', $post_id)) {
             return $post_id;
         }
     } else {
         if (!current_user_can('edit_post', $post_id)) {
             return $post_id;
         }
     }
     /* OK, its safe for us to save the data now. */
     $newGift = new WPR_Giftcard();
     $newGift->createCard($_POST);
     do_action('woocommerce_rpgc_options');
 }
/**
 * Updates the Gift Card and the order information when the order is processed
 *
 */
function rpgc_update_card($order_id)
{
    global $woocommerce;
    $giftCard_id = WC()->session->giftcard_post;
    if ($giftCard_id != '') {
        //Decrease Ballance of card
        $giftcard = new WPR_Giftcard();
        $payment = $giftcard->wpr_get_payment_amount();
        $giftcard->wpr_decrease_balance($giftCard_id);
        $giftCard_IDs = get_post_meta($giftCard_id, 'wpr_existingOrders_id', true);
        $giftCard_IDs[] = $order_id;
        $newBalance = wpr_get_giftcard_balance($giftCard_id);
        update_post_meta($giftCard_id, 'rpgc_balance', $newBalance);
        // Update balance of Giftcard
        update_post_meta($giftCard_id, 'wpr_existingOrders_id', $giftCard_IDs);
        // Saves order id to gifctard post
        update_post_meta($order_id, 'rpgc_id', $giftCard_id);
        update_post_meta($order_id, 'rpgc_payment', $payment);
        update_post_meta($order_id, 'rpgc_balance', $newBalance);
        WC()->session->idForEmail = $order_id;
        unset(WC()->session->giftcard_payment, WC()->session->giftcard_post);
    }
    if (isset(WC()->session->giftcard_data)) {
        update_post_meta($order_id, 'rpgc_data', WC()->session->giftcard_data);
        unset(WC()->session->giftcard_data);
    }
}
 public static function wpr_discount_total($total, $cart)
 {
     $giftcard = new WPR_Giftcard();
     $discount = $giftcard->wpr_get_payment_amount();
     $total -= $discount;
     //WC()->session->discount_cart = $discount;
     return $total;
 }