1

I am currently setting up a woocommerce shop. Basically this is what I am trying to achieve.

"this is a fruit shop for whom they want fruits in basket( package), the customer select the fruits he wants (for example 3 items), then he/she select the basket with the desired quantity (1 among 3 different baskets in sites).

the total cost must be calculated like this: {sum(fruit a+fruit b+fruit c+basket price)* (the quantity of selected basket).

for example: apple price 3$, banana price 3$, selected basket price 1$, quantity of basket is: 200. total cost=(3+3+1)*200.

so this is just an example as I would like to setup pricing.

any idea how can that be achieved, I tried multiple plugins recommended by woocommerce but non of them do what I am looking for.

your help is greatly appreciated...

i write the following code for (in this code basket product id is 24). But the problem is when I set for ex: 10 for basket. the total cost at the end of basket line in cart page exactly at the end of basket row will be like this: basket quantity* basket cost of 1$ so this is here 10 $.

And the total price is (3$ for bababa+3$ for apple+10 $ of basket )* QUANTITY of basket.

The right calculation is (3+3+1)*10.

add_action( 'woocommerce_calculate_totals', 'action_cart_calculate_totals', 10, 1 );

function action_cart_calculate_totals( $cart_object ) {

$targeted_id = 24;

foreach ( WC()->cart->get_cart() as $cart_item ) { 
    if($cart_item['product_id'] == $targeted_id ){
        $qty =  $cart_item['quantity'];
        break; // stop the loop if product is found
    }
}       

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( !WC()->cart->is_empty() ):

        $cart_object->subtotal *= $qty;

        $cart_object->total *= $qty;


        $cart_object->cart_contents_total *= $qty;

    endif;
}

1 Answers1

0

It seems your problem falls under Custom price calculation category.

There are some wordpress plugins you may get the job done by them like WooPrice Calculator and Booster for WooCommerce.

Xaqron
  • 29,931
  • 42
  • 140
  • 205
  • @AmirAsgari: What about distributing multiply factor into the sum? `(3+3+1)*10 = 0.3+0.3+0.1`. Simply adding `basket price*k` as a new item and modifying existing items to `x*k`. – Xaqron Aug 11 '17 at 22:45
  • 1
    i didn't get this would you please elaborate on more please? @Xaqron – Amir Asgari Aug 12 '17 at 09:08