H
H
hellion352021-09-02 15:06:38
WordPress
hellion35, 2021-09-02 15:06:38

Add 1 item to cart as Woocommerce different items?

Hello everyone, I have implemented an additional input for entering the length in the product card. The price of the goods is indicated per meter, a person enters, enters the length of the pole, the number of pieces, and the total number of meters and the amount are calculated for him. When I click on the add to cart button, I change the price of the product as follows:

add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );
function add_custom_price( $cart_object ) {
    $length = (int)$_POST['length'];
    $price = (int)$_POST['price'];
    $itog = ($length / 1000)*$price;
    $target_product_id = 15713;
    foreach ( $cart_object->cart_contents as $key => $value ) {
        if ( $value['product_id'] == $target_product_id ) {
            $value['data']->set_price((int)$itog);
        }
    }
}

Thus, the desired price of the product is added, but if the user wants to add the same columns of a different size, the goods in the basket are added and the price is overwritten already from the new length of the column. The question is, how can I add 1 and the same product to the cart in different positions? Plugins are a huge request not to offer, I know that there is something similar but it does not suit me.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question