1
votes

i was wondering if someone had some experience with the following scenario.

I'm using WooCommerce to have my products listed, ready for sale. Some products (about 20) have a massive amount of variations, so i decided to have the product data in separated tables, and via SKU (simple query to get the data) to load them into woocommerce. So far, everything works great. These products are working fine. Their variations are showing up, and the user can select them, to add them to cart.

Thing is, i have setup these 20 products as single products, and gave them a price of $1, just to show the add to cart button (.single_add_to_cart_button), but when im selecting my variations (from the outside table/tables) the product gets added fine into the cart, but it shows for every product in that case, a price of $1, with all the custom fields included (which is nice). As far as i know, these custom fields are from the sessions). Below is all my code that im using in my functions.php (not mine, found on the net, fixed it to work with my case)

// Step 1: Add Data in a Custom Session, on ‘Add to Cart’ Button Click

add_action('wp_ajax_wdm_add_user_custom_data_options', 'wdm_add_user_custom_data_options_callback');
add_action('wp_ajax_nopriv_wdm_add_user_custom_data_options', 'wdm_add_user_custom_data_options_callback');

function wdm_add_user_custom_data_options_callback()
{

    $product_id = $_POST['id'];
    $product_category = $_POST['product_category'];

    $custom_data_1 = $_POST['custom_data_1'];
    $custom_data_2 = $_POST['custom_data_2'];
    $custom_data_3 = $_POST['custom_data_3'];
    $custom_data_4 = $_POST['custom_data_4'];
    $custom_data_5 = $_POST['custom_data_5'];
    session_start();

    $_SESSION['custom_data_1'] = $custom_data_1;
    $_SESSION['custom_data_2'] = $custom_data_2;
    $_SESSION['custom_data_3'] = $custom_data_3;
    $_SESSION['custom_data_4'] = $custom_data_4;
    $_SESSION['custom_data_5'] = $custom_data_5;

    die();
}

// Step 2: Add Custom Data in WooCommerce Session

add_filter('woocommerce_add_cart_item_data','wdm_add_item_data',1,2);

if(!function_exists('wdm_add_item_data'))
{

    function wdm_add_item_data($cart_item_data,$product_id)

    {
        global $woocommerce;
        session_start();
        $new_value = array();
        if (isset($_SESSION['custom_data_1'])) {
            $option1 = $_SESSION['custom_data_1'];
            $new_value['custom_data_1'] =  $option1;
        }
        if (isset($_SESSION['custom_data_2'])) {
            $option2 = $_SESSION['custom_data_2'];
            $new_value['custom_data_2'] =  $option2;
        }
        if (isset($_SESSION['custom_data_3'])) {
            $option3 = $_SESSION['custom_data_3'];
            $new_value['custom_data_3'] =  $option3;
        }
        if (isset($_SESSION['custom_data_4'])) {
            $option4 = $_SESSION['custom_data_4'];
            $new_value['custom_data_4'] =  $option4;
        }
        if (isset($_SESSION['custom_data_5'])) {
            $option5 = $_SESSION['custom_data_5'];
            $new_value['custom_data_5'] =  $option5;
        }

        if( empty($option1) && empty($option2) && empty($option3) && empty($option4) && empty($option5)  )
            return $cart_item_data;
        else
        {
            if(empty($cart_item_data))
                return $new_value;
            else
                return array_merge($cart_item_data,$new_value);
        }

        unset($_SESSION['custom_data_1']);
        unset($_SESSION['custom_data_2']);
        unset($_SESSION['custom_data_3']);
        unset($_SESSION['custom_data_4']);
        unset($_SESSION['custom_data_5']);

    }
}

// Step 3: Extract Custom Data from WooCommerce Session and Insert it into Cart Object

add_filter('woocommerce_get_cart_item_from_session', 'wdm_get_cart_items_from_session', 1, 3 );
if(!function_exists('wdm_get_cart_items_from_session'))
{
    function wdm_get_cart_items_from_session($item,$values,$key)
    {
        if (array_key_exists( 'custom_data_1', $values ) )
        {
            $item['custom_data_1'] = $values['custom_data_1'];
        }
        if (array_key_exists( 'custom_data_2', $values ) )
        {
            $item['custom_data_2'] = $values['custom_data_2'];
        }
        if (array_key_exists( 'custom_data_3', $values ) )
        {
            $item['custom_data_3'] = $values['custom_data_3'];
        }
        if (array_key_exists( 'custom_data_4', $values ) )
        {
            $item['custom_data_4'] = $values['custom_data_4'];
        }
        if (array_key_exists( 'custom_data_5', $values ) )
        {
            $item['custom_data_5'] = $values['custom_data_5'];
        }

        return $item;
    }
}

// Step 4: Display User Custom Data on Cart and Checkout page

add_filter('woocommerce_checkout_cart_item_quantity','wdm_add_user_custom_option_from_session_into_cart',1,3);
add_filter('woocommerce_cart_item_price','wdm_add_user_custom_option_from_session_into_cart',1,3);

if(!function_exists('wdm_add_user_custom_option_from_session_into_cart'))
{


    function wdm_add_user_custom_option_from_session_into_cart($product_name, $values, $cart_item_key )
    {

                        if(count($values['custom_data_1']) > 0)
                        {

                            $return_string = $product_name . "";
                            $return_string .= "";
                            $return_string .= "SKU : " . $values['custom_data_1'] . "";
                            //$return_string .= "Code : " . $values['custom_data_2'] . "";
                            $return_string .= "Width : " . $values['custom_data_3'] . " cm";
                            $return_string .= "Height : " . $values['custom_data_4'] . " cm";
                            $return_string .= "Price : € " . $values['custom_data_5'] . "";
                            $return_string .= "";

                            return $return_string;    
                        }
                        else
                        {
                            return $product_name;
                        }
    }

}

// Step 5: Add Custom Data as Metadata to the Order Items

add_action('woocommerce_add_order_item_meta','wdm_add_values_to_order_item_meta',1,2);
if(!function_exists('wdm_add_values_to_order_item_meta'))
{
    function wdm_add_values_to_order_item_meta($item_id, $values)
    {
        global $woocommerce,$wpdb;
        $user_custom_values = $values['wdm_user_custom_data_value'];
        if(!empty($user_custom_values))
        {
            wc_add_order_item_meta($item_id,'wdm_user_custom_data',$user_custom_values);
        }

        $custom_data_1 = $values['custom_data_1'];
        if(!empty($custom_data_1))
        {
            wc_add_order_item_meta($item_id,'custom_data_1',$custom_data_1);
        }
        $custom_data_2 = $values['custom_data_2'];
        if(!empty($custom_data_2))
        {
            wc_add_order_item_meta($item_id,'custom_data_2',$custom_data_2);
        }
        $custom_data_3 = $values['custom_data_3'];
        if(!empty($custom_data_3))
        {
            wc_add_order_item_meta($item_id,'custom_data_3',$custom_data_3);
        }
        $custom_data_4 = $values['custom_data_4'];
        if(!empty($custom_data_4))
        {
            wc_add_order_item_meta($item_id,'custom_data_4',$custom_data_4);
        }
        $custom_data_5 = $values['custom_data_5'];
        if(!empty($custom_data_5))
        {
            wc_add_order_item_meta($item_id,'custom_data_5',$custom_data_5);
        }
    }
}

// Step 6: Remove User Custom Data, if Product is Removed from Cart

add_action('woocommerce_before_cart_item_quantity_zero','wdm_remove_user_custom_data_options_from_cart',1,1);
if(!function_exists('wdm_remove_user_custom_data_options_from_cart'))
{
    function wdm_remove_user_custom_data_options_from_cart($cart_item_key)
    {
        global $woocommerce;
        // Get cart
        $cart = $woocommerce->cart->get_cart();
        // For each item in cart, if item is upsell of deleted product, delete it
        foreach( $cart as $key => $values)
        {
            if ( $values['wdm_user_custom_data_value'] == $cart_item_key )
                unset( $woocommerce->cart->cart_contents[ $key ] );
        }
    }
}

i tried to include the following code

add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );

    function add_custom_price( $cart_object ) {
        $custom_price = 999; // This is the custom price  
        foreach ( $cart_object->cart_contents as $key => $value ) {
            $value['data']->price = $custom_price;
        }
    }

it changes the price to 999, since its fixed, but in my case, i am not able to include the value

$custom_data_5 = $_POST['custom_data_5']; // thats my unit price which needs to replace the $1 value of the simple product value from the product

any help would be appreciated on my side, since I'm running out of options right now, and this solution seems to be to correct one for me.

Jan

1

1 Answers

1
votes

all right, i think i got it done, searching and searching on stackoverflow.com of course ....

this i what i did :

 function ipe_add_to_cart_link() {

        global $product;
        $custom_price = 30;

        echo sprintf( '%s',
                esc_url( $product->add_to_cart_url() ),
                esc_attr( isset( $quantity ) ? $quantity : 1 ),
                $myCustomPrice, // Thats the value im getting from my add_to_cart_button - hidden
                esc_attr( $product->id ),
                esc_attr( $product->get_sku() ),
                esc_attr( isset( $class ) ? $class : 'button' ),
                esc_html( $product->add_to_cart_text() )
        ); 
}

add_filter('woocommerce_loop_add_to_cart_link','ipe_add_to_cart_link');



function ipe_product_custom_price( $cart_item_data, $product_id ) {

     if( isset( $_POST['myCustomPrice'] ) && !empty($_POST['myCustomPrice'])) {     

         $cart_item_data[ "myCustomPrice" ] = $_POST['myCustomPrice'];     
     }
     return $cart_item_data;

 }

 add_filter( 'woocommerce_add_cart_item_data', 'ipe_product_custom_price', 99, 2 );


function ipe_apply_custom_price_to_cart_item( $cart_object ) {  
    if( !WC()->session->__isset( "reload_checkout" )) {

        foreach ( $cart_object->cart_contents as $key => $value ) {
            if( isset( $value["myCustomPrice"] ) ) {
                $value['data']->price = $value["myCustomPrice"];
            }
        }   
    }   
}

add_action( 'woocommerce_before_calculate_totals', 'ipe_apply_custom_price_to_cart_item', 99 );

and placed everything in functions.php - worked like a charm. Hope that this solution is helping someone out there.

Jan