WooCommerce manipulating cart

https://www.sitepoint.com/woocommerce-actions-and-filters-manipulate-cart/

Hi,
you can use the hook woocommerce_add_to_cart. It is fired just after a product has been added to the cart.


Ok, so cart is stored as serialised ‘meta’ data in sessions table

When cart is displayed, you can hook into:

  • add_action( ‘woocommerce_before_calculate_totals’, ‘add_custom_price’ );
  • do_action( ‘woocommerce_calculate_totals’, $this );
  • do_action( ‘woocommerce_after_calculate_totals’, $this );

from https://docs.woocommerce.com/wc-apidocs/source-class-WC_Cart.html#1116-1418

    /** @var array cart_session_data. Array of data the cart calculates and stores in the session with defaults */
    public $cart_session_data = array(
        'cart_contents_total'         => 0,
        'total'                       => 0,
        'subtotal'                    => 0,
        'subtotal_ex_tax'             => 0,
        'tax_total'                   => 0,
        'taxes'                       => array(),
        'shipping_taxes'              => array(),
        'discount_cart'               => 0,
        'discount_cart_tax'           => 0,
        'shipping_total'              => 0,
        'shipping_tax_total'          => 0,
        'coupon_discount_amounts'     => array(),
        'coupon_discount_tax_amounts' => array(),
        'fee_total'                   => 0,
        'fees'                        => array()
    );

woocommerce-measurement-price-calculator.php

* The following cart item data is added for pricing calculator products:
*
* pricing_item_meta_data => Array(
*   _price                   => (float) the total product price,
*   _measurement_needed      => (float) the total measurment needed,
*   _measurement_needed_unit => (string) the total measurement units,
*   _quantity                => (int) the quantity added by the customer,
*   <measurement name>       => (float) measurement amount provided by the customer and depends on the calculator type.  For instance ‘length’ => 2
* )

class-wc-price-calculator-oroduct.php has:

  • public static function calculate_price

Nope here it is?

  • class-wc-price-calculator-cart.php


Snippet (temp) for functions.php

<?php
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );

function add_custom_price( $cart_object ) {

echo 'Custom';

    foreach ( $cart_object->cart_contents as $key => $value ) {    
        $addons = $value['addons'];
        
        $add_to_price = 0;
        foreach($addons as $addon) {
            $add_to_price .= $addon['price'];
   echo $addon['price'].'|';
   echo $add_to_price.'|';
        }
        
        $price = $value['data']->price;
  $quantity = $value['data']->quantity;
  echo $price.'|';
  echo $quantity.'|';
        $price += $add_to_price;
  $price = 0;
  
  $values['quantity'].
                
        $value['data']->price = $price;
    }
}
?>


<?php
?>