In WooCommerce I am using variables products which variations are based on date availability.
In backend, I have added successfully 2 custom fields with custom prices (adult/children) on each variation of the variable products settings (the 2 custom fields appear at the bottom):
It is necessary that the 2 prices show on the same variation because they are linked to the same date attribute and quantity.
Problem
I can define the prices in backend, show them with a quantity selector on frontend, but I can't add them to cart at the same "click" on add to cart button. When user clicks add to cart button, the appropriate number of adults is added to cart with the right quantity, but I don't know how to add children as well...
In fact I would like to also add to the cart another item corresponding to the same variation, but with the children prices and quantity, and an attribute like "Price : children".
I am thinking of using the add_to_cart function, like:
add_to_cart( $product_id , $quantity , $variation_id, $variation , array(__('Price:','ah')=>__('Children','ah)) );
With the appropriate $product_id
, $variation_id
and $variation
, with $quantity
= the number of children input, but I don't really know where and when to hook to include that properly.
What I've tried (and does not work) :
- I've tried hooking into add_to_cart itself, but only succeeded to create a recursive call that ends with an error.
- I've also tried to hook into woocommerce_before_calculate_totals, but it seems that it's the right place to alter price, but that it is "too late" to add a new product, and set quantity for it.
Display of adult and children quantity and prices
Product page is modified to show the 2 prices, with each a quantity selector, and also a jQuery script that calculates total dynamically :
View of the front-end, with prices and quantity selector, and automatic calculation of total
Here is my code:
function ah_activity_product_price_display() {
global $product;
$stock = $product->get_total_stock();
$pricepolicy = get_post_meta( $product->id, '_ah_pricing', true );
if( is_activity()) {
$price_list = array();
if ($pricepolicy == 'multiple') {
$variations=$product->get_available_variations();
foreach ($variations as $variation) {
$variation_id = $variation['variation_id'] ;
$start = $variation['attributes']['attribute_date'];
$price_list[$start]['adult_price'] = get_post_meta( $variation_id, '_adult_price', true );
$price_list[$start]['children_price'] = get_post_meta( $variation_id, '_children_price', true );
}
//var_dump($price_list,json_encode($price_list));
?>
<div class="multiple">
<p class="clear">
<div class="quantity">
<input class="minus" type="button" value="-">
<input type="number" step="1" min="1" max="<?php echo esc_attr( 0 < $stock ? $stock : '' ); ?>" name="nb_adulte" value="1" title="<?php echo esc_attr_x( 'Adult number', 'Product quantity input tooltip', 'ah' ); ?>" class="input-text qty text" size="4" pattern="" inputmode="" />
<input class="plus" type="button" value="+">
</div> <?php _e('Adult x ','ah'); ?> <span class="prix-adulte"><?php esc_html($adult_price); ?></span> €
</p>
<p class="clear">
<div class="quantity">
<input class="minus" type="button" value="-">
<input type="number" step="1" min="0" max="<?php echo esc_attr( 0 < $stock ? $stock : '' ); ?>" name="nb_enfant" value="0" title="<?php echo esc_attr_x( 'Children number', 'Product quantity input tooltip', 'ah' ); ?>" class="input-text qty text" size="4" pattern="" inputmode="" />
<input class="plus" type="button" value="+">
</div> <?php _e('Children (under 18) x ','ah'); ?> <span class="prix-enfant"><?php echo esc_html($children_price); ?></span> €
</p>
</div>
<?php
}
echo '<input type="hidden" id="pricelist" name="pricelist" value=\''.json_encode($price_list).'\'>';
} else {
woocommerce_get_price_html();
}
}