Esempio n. 1
0
function load_ajax_cart()
{
    global $wpdb;
    $cart_data = wpmp_get_cart_data();
    foreach ($cart_data as $pid => $cdt) {
        extract($cdt);
        if ($pid) {
            $cart_items[$pid] = get_post($pid, ARRAY_A);
            $cart_items[$pid]['quantity'] = $quantity;
            $cart_items[$pid]['discount'] = $discount;
            $cart_items[$pid]['variation'] = $variation;
            $cart_items[$pid]['price'] = (double) $price;
            if ($cdt['coupon']) {
                $valid_coupon = check_coupon($pid, $coupon);
                if ($valid_coupon != 0) {
                    $cart_items[$pid]['coupon'] = $coupon;
                    $cart_items[$pid]['coupon_discount'] = $valid_coupon;
                } else {
                    $cart_items[$pid]['error'] = "Coupon does not exist";
                }
            }
        }
    }
    $settings = get_option('_wpmp_settings');
    $currency_sign = get_option('_wpmp_curr_sign', '$');
    $total_quantity = 0;
    $cart = "<form method='post' class='abc' action='' name='widgetcart_form'><input type='hidden' name='wpmp_update_cart' value='1' /><table class='wpdm_cart'>";
    if (is_array($cart_items)) {
        //print_r($cart_items);
        foreach ($cart_items as $item) {
            $prices = 0;
            $variations = "";
            @extract(get_post_meta($item['ID'], "wpmp_list_opts", true));
            $svariation = array();
            foreach ($variation as $key => $value) {
                foreach ($value as $optionkey => $optionvalue) {
                    if ($optionkey != "vname") {
                        if ($item['variation']) {
                            foreach ($item['variation'] as $var) {
                                if ($var == $optionkey) {
                                    $prices += $optionvalue['option_price'];
                                    $svariation[] = $optionvalue['option_name'] . ": " . ($optionvalue['option_price'] > 0 ? '+' : '') . $currency_sign . $optionvalue['option_price'];
                                    $variations .= '<input type="hidden" name="cart_items[' . $item[ID] . '][variation][]" value="' . $optionkey . '">';
                                }
                            }
                        }
                    }
                }
            }
            if ($svariation) {
                $variations .= "<small><i>" . implode(", ", $svariation) . "</i></small>";
            }
            if ($item['coupon_discount']) {
                $discount_amount = $item['coupon_discount'] / 100 * ($item['price'] + $prices) * $item[quantity];
                $discount_style = "style='color:#008000; text-decoration:underline;'";
                $discount_title = 'Discounted $' . $discount_amount . " for coupon code '{$discount_amount}'";
            } else {
                $discount_amount = "";
                $discount_style = "";
                $discount_title = "";
            }
            if ($item['error']) {
                $coupon_style = "style='border:1px solid #ff0000;'";
                $title = $item['error'];
            } else {
                $coupon_style = "";
                $title = "";
            }
            //filter for adding various message after cart item
            $cart_item_info = "";
            $cart_item_info = apply_filters("wpmp_cart_item_info", $cart_item_info, $item['ID']);
            $imgurl = "";
            $thumb = wp_get_attachment_image_src(get_post_thumbnail_id($item[ID]), array(600, 300));
            $imgurl = $thumb['0'];
            $cart .= "<tr id='mini_cart_item_{$item[ID]}'><td><a title='" . __('Delete cart item', 'wpmarketplace') . "' class='wpmp_cart_delete_item' href='#' onclick='return wpmp_mpp_remove_cart_item({$item['ID']})'>&times;</a></td><td><a href='" . get_permalink($item[ID]) . "'><img src='" . plugins_url("wpmarketplace") . "/libs/timthumb.php?src=" . $imgurl . "&w=40&h=40'></a></td><td class='cart_item_title'>{$item['post_title']}<br>{$variations}" . $cart_item_info . "<input type='hidden' name='cart_items[{$item['ID']}][license]' value='{$item['license']}'><input type='hidden' name='cart_items[{$item['ID']}][price]' value='{$item['price']}'></td><td class='cart_item_subtotal amt'>" . $currency_sign . number_format($item['price'] + $prices) . "x " . $item['quantity'] . " <input type='hidden' name='cart_items[{$item['ID']}][item_total]' value='" . number_format(($item['price'] + $prices) * $item['quantity'] - $discount_amount, 2, ".", "") . "'></td></tr>";
            $total_quantity += $item['quantity'];
        }
    }
    $cart .= "\n\n        <tr><td colspan=3 align=right>" . __("Subtotal:", "wpmarketplace") . "</td><td class='amt' id='wpmp_mini_cart_subtotal'>" . $currency_sign . wpmp_get_cart_subtotal() . "</td></tr>\n        \n        <tr><td colspan=4><button class='btn btn-primary' type='button' onclick='location.href=\"" . get_permalink($settings['page_id']) . "\"'><i class='icon-white icon-edit'></i> " . __("View Cart", "wpmarketplace") . "</button> <button class='btn btn-success' type='button' onclick='location.href=\"" . get_permalink($settings['check_page_id']) . "\"'><i class='glyphicon glyphicon-shopping-cart icon-white icon-shopping-cart'></i> " . __("Checkout", "wpmarketplace") . "</button></td></tr>\n        </table>\n\n        </form>\n\n        <script language='JavaScript'>\n        <!--\n            function  wpmp_mpp_remove_cart_item(id){\n                   if(!confirm('Are you sure?')) return false;\n                   jQuery('#mini_cart_item_'+id+' *').css('color','#ccc');\n                   jQuery.post('" . home_url('?wpmp_remove_cart_item=') . "'+id\n                   ,function(res){ \n                   var obj = jQuery.parseJSON(res);\n                   \n                   jQuery('#mini_cart_item_'+id).fadeOut().remove(); \n                   /*jQuery('#wpmp_cart_total').html(obj.cart_total); \n                   jQuery('#wpmp_cart_discount').html(obj.cart_discount);*/ \n                   jQuery('#wpmp_mini_cart_subtotal').html(obj.cart_subtotal); });\n                   return false;\n            }  \n        //-->\n        </script>\n\n        ";
    $cart_['content'] = $cart;
    $cart_['items'] = $total_quantity;
    return $cart_;
}
Esempio n. 2
0
function get_all_coupon($data)
{
    $total = 0;
    foreach ($data as $pid => $item) {
        $valid_coupon = check_coupon($pid, $item['coupon']);
        if ($valid_coupon != 0) {
            $total += $item['item_total'] * $item['quantity'] * ($valid_coupon / 100);
        }
    }
    return $total;
}