Showing posts with label WooCommerce. Show all posts
Showing posts with label WooCommerce. Show all posts

Tuesday, 21 August 2018

Continue Shopping To Redirect Category Page Woocommerce


There is no functionality provide WooCommerce to retrieve last category page viewed. But, I solved.

First create a custom action, check if page is category page, and set Cat ID to session data if so. Then on your button, use this to get proper URL.

add_action( 'init', 'wp_check_product_cat_sess');
function wp_check_product_cat_sess(){
    if(!session_id()){
        session_start();
    }
}
add_action( 'wp', 'check_product_cats');
function check_product_cats(){
    global $wp_query;
    if( is_product_category() ){
        $_SESSION['set_last_cat_id'] = $wp_query->get_queried_object()->term_id;
    }
    if( is_product() ){
        $get_cats = get_the_terms( $wp_query->get_queried_object(), 'product_cat' ) ;
        if( count( $get_cats ) > 0 ){
            foreach($get_cats as $one_cat ){
                    $_SESSION['set_last_cat_id'] = $one_cat->term_id;
            }
        }
    }  
    if( is_cart()){
        //var_dump( $_SESSION['set_last_cat_id'] );
    }
}
add_filter( 'woocommerce_continue_shopping_redirect', 'woo_add_continue_shop_btn_to_cart', 20 );
function woo_add_continue_shop_btn_to_cart($return_to){
    if( isset( $_SESSION['set_last_cat_id'] ) ){
        $redirect_page_url = get_term_link( $_SESSION['set_last_cat_id'], 'product_cat' );
    }else{
        $redirect_page_url = get_permalink( wc_get_page_id( 'shop' ) );
    }
    return $redirect_page_url;
}

-------------------------------------------------
Let me know your thoughts and questions in the comments.
Email: vyasankit2008@gmail.com

Tuesday, 17 July 2018

Add Custom Fields To Woocommerce Products Single Page, Cart Page, Checkout Page, Order Detaile And Save To Database

<?php
//add custom fields to WooCommerce products Single Page, Cart Page, Checkout Page, Order Detaile And Save To Database Table Name "wp_woocommerce_order_itemmeta"

add_action( 'woocommerce_before_add_to_cart_button', 'add_fields_before_add_to_cart' );
function add_fields_before_add_to_cart(){ ?>
    <table>
        <tr>
            <td>
                <?php _e( "Name:", "aoim"); ?>
            </td>
            <td>
                <input type = "text" name = "customer_name" id = "customer_name" placeholder = "Name on Gift Card">
            </td>
        </tr>
        <tr>
            <td>
                <?php _e( "Gift Price:", "aoim"); ?>
            </td>
            <td>
                <input type = "text" name = "custom_price" id = "custom_price" placeholder = "Your GiftPrice on Gift Card">
            </td>
        </tr>
    </table>
    <?php
}

/**
 * Add data to cart item
 */
add_filter( 'woocommerce_add_cart_item_data', 'add_cart_item_data', 25, 2 );
function add_cart_item_data( $cart_item_meta, $product_id ) {
    if ( isset( $_POST ['customer_name'] ) && isset( $_POST ['custom_price'] ) ) {
      
        $product = wc_get_product($product_id);      
        $price = $product->get_regular_price();
      
        $custom_data  = array() ;
        $custom_data [ 'customer_name' ]    = isset( $_POST ['customer_name'] ) ?  sanitize_text_field ( $_POST ['customer_name'] ) : "" ;
        $custom_data [ 'custom_price' ] = isset( $_POST ['custom_price'] ) ? sanitize_text_field ( $_POST ['custom_price'] ): "" ;
      
        $custom_data['totalPrice'] = $price + $custom_data [ 'custom_price' ];              
      
        $cart_item_meta ['custom_data']     = $custom_data ;
    }  
    return $cart_item_meta;
}

/**
 * Display custom data on cart and checkout page.
 */
add_filter( 'woocommerce_get_item_data', 'get_item_data' , 25, 2 );
function get_item_data ( $other_data, $cart_item ) {
    if ( isset( $cart_item [ 'custom_data' ] ) ) {
        $custom_data  = $cart_item [ 'custom_data' ];          
        $other_data[] = array( 'name' => 'Name', 'display'  => $custom_data['customer_name'] );
        $other_data[] = array( 'name' => 'GiftPrice', 'display'  => $custom_data['custom_price'] );
    }
    return $other_data;
}

// Set the new calculated price replacing cart item price
add_action( 'woocommerce_before_calculate_totals', 'set_cart_item_calculated_price', 20, 1 );
function set_cart_item_calculated_price( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
  
    // Loop through cart items
    foreach ( $cart->get_cart() as $key=>$value ){                      
        if( ! isset( $value['custom_data']['totalPrice'] ) ){ continue; }
      
        if( $value['custom_data']['totalPrice'] > 0 ){
            $value['data']->set_price((float)$value['custom_data']['totalPrice']);
        }
    }
}

/**
 * Add order item meta.
*/
add_action( 'woocommerce_add_order_item_meta', 'add_order_item_meta' , 10, 2);
function add_order_item_meta ( $item_id, $values ) {
    if ( isset( $values [ 'custom_data' ] ) ) {
        $custom_data  = $values [ 'custom_data' ];
        wc_add_order_item_meta( $item_id, 'Name', $custom_data['customer_name'] );
        wc_add_order_item_meta( $item_id, 'GiftPrice', $custom_data['custom_price'] );
    }
}