0

I got a problem and I am realy dying on this right now. I did the WooCommerce Update up to the major 3 version and straight to 3.0.2.

After completet, updated the database. So far so good.

My problem now is, that a function I wrote seems to work no longer. Its like this:

    add_action( 'woocommerce_before_calculate_totals','calc_ferienspiele_discount' );
function calc_ferienspiele_discount( $cart_object ) {
global $woocommerce;
$items = $woocommerce->cart->get_cart();
$x=0;
$custom_price = 140; // This will be your custome price
foreach ( $cart_object->cart_contents as $key => $value ) {
    $target_product_id=$value['data']->get_sku();
    for ($_i = 1; $_i <= $value['quantity']; $_i++) {
        $x++;
        if ( $value['data']->get_sku() == $target_product_id ) {
            if ($x>1) {
                $value['data']->set_price = $custom_price;
                echo $value['data']->get_sku();
                echo 'This works and get displayed';
            }
        }
    }
}
}

I now its maybe a little sloppy but what it did is, it checked, if there are several items with the same SKU. If so, it counted up. When there is more than 1 item of that specific SKU, the following cost less. So if the item is 3 times in the cart the first would be 100$, and the other two like 80$.

I can jump into the last if where the new price is set (tested that with an echo), but the new price wont be set. I assume something changed from major version 2 to 3 and I cant figure it out whats wrong here. spent hours on finding it...

U guys have any ideas what could be wrong?

Thanks!

0

Finally did it. With the update of WooCommerce, which I assumed, the syntax of the set price did change. Function now looks like this:

add_action( 'woocommerce_before_calculate_totals', 'calc_ferienspiele_discount' );
function calc_ferienspiele_discount( $cart_object ) {
global $woocommerce;
$items = $woocommerce->cart->get_cart();
$x=0;
$custom_price = 140; // This will be your custome price
foreach ( $cart_object->cart_contents as $key => $value ) {
    $target_product_id=$value['data']->get_sku();
    for ($_i = 1; $_i <= $value['quantity']; $_i++) {
        $x++;
        if ( $target_product_id=='01010101' ) {
            if ($x>1) {
                $value['data']->set_price($custom_price);
                echo 'This works and get displayed';
            }
        }
    }
}
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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