Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

Woocommerce - Display Notifications


Display notification in Woocommerce when a user checkouts a product.

Here is the code

1. We need get the order count 

<?php

global $woocommerce;

$order_count = wc_get_customer_order_count( $current_user->ID );
if($order_count>=1) { ?>
<div class="quantity"><?php echo $order_count; ?></div>
<?php } ?>

2. Alright!!
We got our order count, but we should allow user to show his notifications only when logged in. So we have to restrict user.

<?php if ( is_user_logged_in() ) { // If user logged in  ?>
<a href="URL">Show Notifications</a> // "URL" Add your Notifications Page URL
<?php }
 else { // If user not logged in , We will Navigate him to My account page ?>
 <a href="<?php echo get_permalink( get_option('woocommerce_myaccount_page_id') ); ?>">Show Notifications</a>
 <?php } ?>

3. Finally!! 

We have to display all the order details in the notifications page.


<ul class="notifications">
<?php
$customer_orders = get_posts( array(
    'numberposts' => -1, // No of notifications to display
    'meta_key'    => '_customer_user', // Meta Key to get customer details
    'meta_value'  => get_current_user_id(),
    'post_type'   => wc_get_order_types(),
    'post_status' => array_keys( wc_get_order_statuses() ),
) );

foreach($customer_orders as $customer_order)
{
echo "<li>"; ?>

<a href="<?php bloginfo('url'); ?>/my-account/view-order/<?php echo $customer_order->ID; ?>"> <?php echo $customer_order->post_title; ?> </a>
<?php
if($customer_order->post_status == "wc-cancelled"){$orderclass = "alert-danger";}else {$orderclass = "alert-info";} // We can add some styles by adding a dynamic class based on the order status, "$orderclass" is the class varaiable ?>
<span class="<?php echo $orderclass; ?>"> <?php  echo  $customer_order->post_status; ?> </span>

<?php echo "</li>"; } ?>

</ul>

4. Done!!


WooCommerce - Display Cart Itmes Count with Cost.

If you wanted to display Cart items and total cost for individual user, use this code below

<?php
global $woocommerce;
// cart quantity
$qty = $woocommerce->cart->get_cart_contents_count();
// cart total
$total = $woocommerce->cart->get_cart_total();
// cart url
$cart_url = $woocommerce->cart->get_cart_url();
?>

<?php
// To Display Cart Quantity
if($qty>=1) { ?>
<div class="quantinty"><?php echo $qty; ?></div>
<?php } ?>

<?php
//To Display Total Cost
if($qty>=1)
echo '<span> '.$total.'</span>'; else { ?>
<span>Cart is Empty</span>
<?php } ?>

Edmunds Vehicle API with Jquery




To get all Vehicle Details (New, Used, Future) using Jquery , Use the below code.

<!--Form Details to display year/make/model in dropdown -->

<form method="POST" action="one123.php">
       <select id="ajYears" name="ajYears">
        </select>
         <select id="ajMakes" name="makes" disabled="disabled">
            <option>Select Make</option>
        </select>
        <select id="ajModels" name="models" disabled="disabled">
            <option>Select Model</option>
        </select>
        <select id="ajStyles" name="styles" disabled="disabled">
            <option>Select Trim</option>
        </select>
        <input type="submit" value="submit">
</form>

<!--Script to fetch details from API-->

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script>
    jQuery.noConflict();
</script>

<!-- js code -->

<script>
  <script>
    var protoCall = "https://";
    var host = "api.edmunds.com";

        // Make sure to change the API KEY
    var apiKey = "v4hq7w7nac87ehk9exfvasu5";
    var fmt = "json";
    var standardParams = "&fmt=" + fmt +  "&api_key=" + apiKey + "&callback=?";
    var $yearSelect = jQuery('#ajYears');
    var $makeSelect = jQuery('#ajMakes');
    var $modelSelect = jQuery('#ajModels');
    var $styleSelect = jQuery('#ajStyles');

    // lets bind some events on document.ready.
    jQuery(function(){

        $yearSelect.on('change', function() { getMakeModelsByYear()});
        $makeSelect.on('change', function() { enableModelDropdown() });
        $modelSelect.on('change', function() { getStyles();  });
$styleSelect.on('change', function() { jQuery('.veh_submit').removeAttr("disabled");  });

        // lets build the year drop down.
        ajEdmundsBuildYear();

    });

    // Method to build the year drop down.
    function ajEdmundsBuildYear()
    {
        var baseYear = 1990,
            endYear = new Date().getFullYear();
        $yearSelect.empty();
        $yearSelect.append('<option value="-1">Select Year</option>');
        for(var yyyy=baseYear; yyyy<=endYear; yyyy++)
        {
            $yearSelect.append('<option value="' + yyyy + '">' + yyyy +'</option>');
        }
    }

    // get the makes and modals for a year in one go, but we don't display modals here...
    function getMakeModelsByYear()
    {
$makeSelect.empty();
$modelSelect.empty();
$styleSelect.empty();
         $makeSelect.append('<option value="-1">Select Make</option>');
$modelSelect.append('<option value="-1">Select Modal</option>');
$styleSelect.append('<option value="-1">Select Style</option>');
$makeSelect.attr('disabled','disabled');
$modelSelect.attr('disabled','disabled');
$styleSelect.attr('disabled','disabled');
        var year = $yearSelect.val(),
            endPoint = "/api/vehicle/v2/makes",
            view = "basic",
            options = "year=" + year + "&view=" + view + standardParams,
            postURL = protoCall + host + endPoint + "?" + options;
        jQuery.getJSON(postURL,
            function(data) {
                 // clear the make drop down... re add the first option... remove dsiabled attr.
                 $makeSelect.empty();
                 $makeSelect.append('<option value="-1">Select Make</option>');
                 $makeSelect.removeAttr('disabled');
             

                 // loop the makes... each make contains model... add the make in make drop down.
                 jQuery.each(data.makes, function(i, val) {
                      make = data.makes[i];
                      $makeSelect.append('<option value="' + make.niceName + '">' + make.name + '</option>');
                 });
            });
    }

    // since we already grabed the models...
    // now just matter of showing only the matching models for a make.
    function enableModelDropdown()
    {

// clear the Style down... re add the first option... add dsiabled attr.
$styleSelect.empty();
$styleSelect.append('<option value="-1">Select Style</option>');
    $styleSelect.attr('disabled','disabled');
var make1 =  $makeSelect.val();
var year = $yearSelect.val(),
            endPoint = "/api/vehicle/v2/makes",
            view = "basic",
            options = "year=" + year + "&view=" + view + standardParams,
            postURL = protoCall + host + endPoint + "?" + options;
        jQuery.getJSON(postURL,
            function(data) {
//let's empty our modal
$modelSelect.empty();
// loop the makes... each make contains model... add the modal in modal drop down.
//re add the first option
                 $modelSelect.append('<option value="-1">Select Model</option>');
                 jQuery.each(data.makes, function(i, val) {
                      make = data.makes[i];

// loop the makes... each make contains model... add the modal in modal drop down but it depends on make value we select previously
                      jQuery.each(make.models, function(x, val){
                           model = make.models[x];
  if(make1 == make.niceName){ // here is the logic
                           $modelSelect.append('<option make="' + make.niceName +'" value="' + model.niceName + '">' + model.name + '</option>');
  }
                      });
                 });
            });

        $modelSelect.removeAttr('disabled');

    }

    // grab the styles... pretty much same as
    // getMakeModelsByYear()
    function getStyles()
    {

        var year = $yearSelect.val(),
            make = $makeSelect.val(),
            model = $modelSelect.val(),
            endPoint = "/api/vehicle/v2/" + make + "/" + model + "/" + year + "/styles",
            view = "basic",
            options = "view=" + view + standardParams,
            postURL = protoCall + host + endPoint + "?" +  options;
        jQuery.getJSON(postURL,
            function(data) {
                 $styleSelect.empty();
                 $styleSelect.removeAttr('disabled');

                 $styleSelect.append('<option value="-1">Select Style</option>');
                 jQuery.each(data.styles, function(i, val) {
                      style = data.styles[i];
                      $styleSelect.append("<option value='" + style.id + "'>" + style.name + "</option>");
                 });
            });
    }
</script>



==========================================================

Hello Mates here is the second step

<?php
if(isset($_POST['submit'])) {
$to = 'youremial@gmail.com'; // Change this to your email
$subject = ''.$_POST['m_phone'].'';
$subject_sucess = "Edmunds Vehicle API with Jquery -  Output";
$ajYears = $_POST['ajYears'];
$ajMakes = $_POST['ajMakes'];
$ajModels = $_POST['ajModels'];
$ajStyles = $_POST['ajStyles'];
$mycar = $_POST['mycar'];
$approx_milage = $_POST['approx_milage'];
$email = $_POST['email'];
$h_phone = $_POST['h_phone'];
$m_phone = $_POST['m_phone'];
$fname = $_POST['fname'];
$l_name = $_POST['l_name'];
$add1 = $_POST['add1'];
$add2 = $_POST['add2'];
$city = $_POST['city'];
$r_state = $_POST['r_state'];
$zip = $_POST['zip'];
$loanamont = $_POST['loanamont'];
$from = ''.$fname.'<'.$email.'>';

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Create email headers
$headers .= 'From: '.$fname.' <youremial@yourwebistename.com>'. "\r\n"; // Change this to your website email
    'Reply-To: '.$email."\r\n" .
    'X-Mailer: PHP/' . phpversion();

// To send HTML mail, the Content-type header must be set
$headers_success  = 'MIME-Version: 1.0' . "\r\n";
$headers_success .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Create email headers
$headers_success .= 'From: Edmunds Vehicle API <youremial@yourwebistename.com>'. "\r\n"; // Change this to your website email
    'Reply-To: '.$email."\r\n" .
    'X-Mailer: PHP/' . phpversion();

// Compose a simple HTML email message
$message = '<b>Year:</b> '.$ajYears.'<br>
<b>Make:</b> '.$ajMakes.'<br>
<b>Model:</b> '.$ajModels.'<br>
<b>Style:</b> '.$ajStyles.'<br>
<b>My car does not appear:</b> '.$mycar.'<br>
<b>Approx. Mileage:</b> '.$approx_milage.'<br>
<b>Email:</b> '.$email.'<br>
<b>Home Phone:</b> '.$h_phone.'<br>
<b>Mobile Phone:</b> '.$m_phone.'<br>
<b>First Name:</b> '.$fname.'<br>
<b>Last Name:</b> '.$l_name.'<br>
<b>Address Line 1:</b> '.$add1.'<br>
<b>Address Line 2:</b> '.$add2.'<br>
<b>City:</b> '.$city.'<br>
<b>Residence State:</b> '.$r_state.'<br>
<b>Zip Code:</b> '.$zip.'<br>
<b>Loan Amount Requested:</b> '.$loanamont.'';

$message_sucess='Thank you for filling out, We will be happy to assist you.';

// Sending email
if(mail($to, $subject, $message, $headers)){ mail($email, $subject_sucess, $message_sucess, $headers_success); ?>
    <script>window.location.assign("http://www.yourwebistename.com");</script>
<?php }
else {echo "mail not sent";}
}
?>

==========================================================

If no email functionality is required you can use this Code.

1. Update your HTML with the below

<!--Form Details to display year/make/model in dropdown -->

       <select id="ajYears" name="ajYears">
        </select>
         <select id="ajMakes" name="makes" disabled="disabled">
            <option>Select Make</option>
        </select>
        <select id="ajModels" name="models" disabled="disabled">
            <option>Select Model</option>
        </select>
        <select id="ajStyles" name="styles" disabled="disabled">
            <option>Select Style</option>
        </select>
        <input type="submit" value="Submit" class="veh_submit">

<!-- output Table -->
<table width="735" border="0"  class="output" cellpadding="0" cellspacing="0" style="display:none;">
<tr>
<td colspan="2" style="text-align:center"><strong>Vehicle Information</strong></td>
</tr>
<tr>
<td><strong>Year</strong></td>
<td class="year">Empty</td>
</tr>
<tr>
<td><strong>Make</strong></td>
<td class="make">Empty</td>
</tr>
<tr>
<td><strong>Modal</strong></td>
<td class="modal">Empty</td>
</tr>
<tr>
<td><strong>Style</strong></td>
<td class="style">Empty</td>
</tr>
</table>
</div>

2. Add this script after </body> tab

<script>
// just to display the output
        jQuery('.veh_submit').on("click",(function(){

var ajYears = jQuery("#ajYears").val();
var ajMakes = jQuery("#ajMakes").val();
var ajModels = jQuery("#ajModels").val();
var ajStyles = jQuery("#ajStyles option:selected").text();

//Basic Validation

if(ajYears =='' || ajYears==-1)
{
   alert("Please Select a Year");
exit;
}
else if(ajMakes =='' || ajMakes==-1)
{
alert("Please Select a Make");
exit;
}
else if(ajModels =='' || ajModels==-1)
{
alert("Please Select a Model");
exit;
}
else if(ajStyles =='' || ajStyles=="Select Style")
{
alert("Please Select a Style");
exit;
}

jQuery('.output').show();
jQuery('.year').html(ajYears);
jQuery('.make').html(ajMakes);
jQuery('.modal').html(ajModels);
jQuery('.style').html(ajStyles);

}));

</script>


Get Hosting and Domains for Best prices  at  http://webservices.redtreetechnology.com/

Reference url:  http://developer.edmunds.com/faq.html

How to Create Custom Posts without using plugins

Step1: Copy and paste the Below code in your theme "function.php". Here i am using this code to create Cusotm Post for "Testimonials", We can Rename it as required

<?php add_action('init', 'testimonials_init');
function testimonials_init()
{
    $feature_labels = array(
        'name' => _x('Testimonials', 'post type general name'), // Name which displays in dashboard
        'singular_name' => _x('Testimonial', 'post type singular name'),
        'all_items' => __('All Testimonials'),
        'add_new' => _x('Add new Testimonial', 'director'),
        'add_new_item' => __('Add new Testimonial'),
        'edit_item' => __('Edit Testimonial'),
        'new_item' => __('New Testimonial'),
        'view_item' => __('View Testimonial'),
        'search_items' => __('Search in Testimonial'),
        'not_found' =>  __('No Testimonial found'),
        'not_found_in_trash' => __('No Testimonial found in trash'),
        'parent_item_colon' => ''
    );
    $args = array(
        'labels' => $feature_labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'menu_position' => 5,
        'supports' => array('title', 'editor','thumbnail'), //  Delete this if thumbnail is not required
        'has_archive' => 'testimonial'
    );
    register_post_type('testimonial',$args); // Post type ID
}
?>

Step2: Copy  and paste this code in your Template to display posts  
Note: Change "post_type=testimonial" if you changed it in the above code

<?php
$my_query = new WP_Query('post_type=testimonial&posts_per_page=-1'); // "-1" describs to display all posts , You can Restrict posts by using positive values
while ($my_query->have_posts()) : $my_query->the_post();
?>
 <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> // Post Title
<div class="thumbnail">
<?php the_post_thumbnail();?> // Post Thumbnail
</div>
<div class="entry">
      <?php the_content(); ?> // Post Content
    </div>
<?php endwhile; ?>