0

In the hook woocommerce_checkout_create_order_line_item I want to change the item metas in the checkout page for more than one quantity and now I need the variation price to calculate this.

function wpler_add_quantities_to_order_items($item, $cart_item_key, $values, $order) {
    setlocale(LC_MONETARY, 'de_DE');
    //$product = $values['data']; // Variation Product-Object
    //$product = $item->get_product();
    $product = wc_get_product($item->get_variation_id());
    $price1 = $product->get_rpice(); //$product->get_variation_regular_price('min');
    $price2 = $product->get_sale_price(); //$product->get_variation_sale_price('min');
    $r = $order->get_billing_postcode();

    $rabatt = !empty($r)&&is_numeric($r)&&$r>0 ? doubleval($r) : 0;
    $ep1 = calcPrices($price1, $price2, intval($values['quantity']), $rabatt);
    $ep2 = calcPrices($price1, $price2, intval($values['quantity10']), $rabatt);
    $ep3 = calcPrices($price1, $price2, intval($values['quantity20']), $rabatt);
    $item->add_meta_data("Preis für {$values['quantity']} Stück", money_format('%.2n', $ep1));
    $item->add_meta_data("Preis für {$values['quantity10']} Stück", money_format('%.2n', $ep2));
    $item->add_meta_data("Preis für {$values['quantity20']} Stück", money_format('%.2n', $ep3));
}
add_filter('woocommerce_checkout_create_order_line_item', 'wpler_add_quantities_to_order_items', 10, 4);

On the regular price are the normal price for the first piece. On sale price are the price for the rest of quantity pieces.

Example: variation-article, regular price = 5.00, sale price = 1.00

order are 50 x variation-article

So, I'm calculate ((49 x 1)+5) = 54.00

But also we want show different quantity suggestions with 500 and 5000 quantities, which are should display in the checkout.

My problem on the function above is that i need the regular and sale price of the variation product. How can i get this?

Thanks, Stefan

1 Answer 1

0

Ok, i have found the solution:

In the object $item i found this:

$item->legacy_values['variation_id']

which gives me the ID. Also in $item->legacy_values[] array i have the both prices, which i take now:

function wpler_add_quantities_to_order_items($item, $cart_item_key, $values, $order) {
    setlocale(LC_MONETARY, 'de_DE');
    $price1 = doubleval($item->legacy_values['price1']);
    $price2 = doubleval($item->legacy_values['price2']);
    $r = $order->get_billing_postcode();

    $rabatt = !empty($r)&&is_numeric($r)&&$r>0 ? doubleval($r) : 0;
    $ep1 = calcPrices($price1, $price2, intval($values['quantity']), $rabatt);
    $ep2 = calcPrices($price1, $price2, intval($values['quantity10']), $rabatt);
    $ep3 = calcPrices($price1, $price2, intval($values['quantity20']), $rabatt);
    $item->add_meta_data("Preis für {$values['quantity']} Stück", money_format('%.2n', $ep1));
    $item->add_meta_data("Preis für {$values['quantity10']} Stück", money_format('%.2n', $ep2));
    $item->add_meta_data("Preis für {$values['quantity20']} Stück", money_format('%.2n', $ep3));
}
add_filter('woocommerce_checkout_create_order_line_item', 'wpler_add_quantities_to_order_items', 10, 4);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.