Adding a product to cart with custom info and pric

2020-05-19 00:02发布

I have installed woocommerce to handle product input & checkout process of a wordpress shop.

The shop page is custom built which allows the user to pick a product from a list and customise it which outputs a price in javascript based on information stored in the database.

The products stored in the db are valued at 0.00 because they're different prices depending on the variables chosen.

The output data I'm ready to pass to woocommerce is as follows:

  • WC Product ID (This matches a product in the db)
  • Custom Price
  • Custom Image
  • Custom Description (e.g. 100mm x 100mm)
  • Build Data (to be stored against the item but not seen in checkout)

I'm trying to find a way to add a product to the cart using the product ID (to make it valid) and then overriding the price with the custom price and attaching meta data which most will be seen at checkout and one will be hidden until seen in the wordpress admin.

Adding the product to the cart is achieved by using:

$woocommerce->cart->add_to_cart($_POST['custom_product_id']);

After which point I'm finding it impossible to override the price and add additional information.

2条回答
Melony?
2楼-- · 2020-05-19 00:47

Step 1:- You need to create some custom hidden fields to send custom data that will show on single product page. for example :-

add_action('woocommerce_before_add_to_cart_button', 'custom_data_hidden_fields');
function custom_data_hidden_fields() {
    echo '<div class="imput_fields custom-imput-fields">
        <label class="price_prod">price: <br><input type="text" id="price_prod" name="price_prod" value="" /></label>
        <label class="quantity_prod">quantity: <br>
            <select name="quantity_prod" id="quantity_prod">
                <option value="1" selected="selected">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
                <option value="4">4</option>
                <option value="5">5</option>
            </select>
        </label>
    </div><br>';
}

Step 2:- Now after done that you need to write the main logic for Save all Products custom fields to your cart item data, follow the below codes.

// Logic to Save products custom fields values into the cart item data
add_action( 'woocommerce_add_cart_item_data', 'save_custom_data_hidden_fields', 10, 2 );
function save_custom_data_hidden_fields( $cart_item_data, $product_id ) {

    $data = array();

    if( isset( $_REQUEST['price_prod'] ) ) {
        $cart_item_data['custom_data']['price_pro'] = $_REQUEST['price_prod'];
        $data['price_pro'] = $_REQUEST['price_prod'];
    }

    if( isset( $_REQUEST['quantity_prod'] ) ) {
        $cart_item_data['custom_data']['quantity'] = $_REQUEST['quantity_prod'];
        $data['quantity'] = $_REQUEST['quantity_prod'];
    }

    // below statement make sure every add to cart action as unique line item
    $cart_item_data['custom_data']['unique_key'] = md5( microtime().rand() );
    WC()->session->set( 'price_calculation', $data );

    return $cart_item_data;
}

Step 3: you need to override the item price with your custom calculation. It will work with your every scenario of your single product sessions.

add_action( 'woocommerce_before_calculate_totals', 'add_custom_item_price', 10 );
function add_custom_item_price( $cart_object ) {

    foreach ( $cart_object->get_cart() as $item_values ) {

        ##  Get cart item data
        $item_id = $item_values['data']->id; // Product ID
        $original_price = $item_values['data']->price; // Product original price

        ## Get your custom fields values
        $price1 = $item_values['custom_data']['price1'];
        $quantity = $item_values['custom_data']['quantity'];

        // CALCULATION FOR EACH ITEM:
        ## Make HERE your own calculation 
        $new_price = $price1 ;

        ## Set the new item price in cart
        $item_values['data']->set_price($new_price);
    }
}

Everything will be done inside the functions.php

Reference Site

查看更多
放我归山
3楼-- · 2020-05-19 00:51

All of this code goes into functions.php

  1. This captures additional posted information (all sent in one array)

    add_filter('woocommerce_add_cart_item_data','wdm_add_item_data',1,10);
    function wdm_add_item_data($cart_item_data, $product_id) {
    
        global $woocommerce;
        $new_value = array();
        $new_value['_custom_options'] = $_POST['custom_options'];
    
        if(empty($cart_item_data)) {
            return $new_value;
        } else {
            return array_merge($cart_item_data, $new_value);
        }
    }
    
  2. This captures the information from the previous function and attaches it to the item.

    add_filter('woocommerce_get_cart_item_from_session', 'wdm_get_cart_items_from_session', 1, 3 );
    function wdm_get_cart_items_from_session($item,$values,$key) {
    
        if (array_key_exists( '_custom_options', $values ) ) {
            $item['_custom_options'] = $values['_custom_options'];
        }
    
        return $item;
    }
    
  3. This displays extra information on basket & checkout from within the added info that was attached to the item.

    add_filter('woocommerce_cart_item_name','add_usr_custom_session',1,3);
    function add_usr_custom_session($product_name, $values, $cart_item_key ) {
    
        $return_string = $product_name . "<br />" . $values['_custom_options']['description'];// . "<br />" . print_r($values['_custom_options']);
        return $return_string;
    
    }
    
  4. This adds the information as meta data so that it can be seen as part of the order (to hide any meta data from the customer just start it with an underscore)

    add_action('woocommerce_add_order_item_meta','wdm_add_values_to_order_item_meta',1,2);
    function wdm_add_values_to_order_item_meta($item_id, $values) {
        global $woocommerce,$wpdb;
    
        wc_add_order_item_meta($item_id,'item_details',$values['_custom_options']['description']);
        wc_add_order_item_meta($item_id,'customer_image',$values['_custom_options']['another_example_field']);
        wc_add_order_item_meta($item_id,'_hidden_field',$values['_custom_options']['hidden_info']);
    
    }
    
  5. If you want to override the price you can use information saved against the product to do so

    add_action( 'woocommerce_before_calculate_totals', 'update_custom_price', 1, 1 );
    function update_custom_price( $cart_object ) {
        foreach ( $cart_object->cart_contents as $cart_item_key => $value ) {       
            // Version 2.x
            //$value['data']->price = $value['_custom_options']['custom_price'];
            // Version 3.x / 4.x
            $value['data']->set_price($value['_custom_options']['custom_price']);
        }
    }
    

All your custom information will appear in the customer email and order from within wordpress providing you added it as meta data (4.)

查看更多
登录 后发表回答