PHP Tweaks Help

below mentioned is a solution where woocommerce hooks are used to add a price coming from a checkbox ticked status.

// Add selected add-on option as custom cart item data
add_filter( 'woocommerce_add_cart_item_data', 'filter_add_cart_item_data_callback', 10, 3 );
function filter_add_cart_item_data_callback( $cart_item_data, $product_id, $variation_id ) {
    if ( isset( $_POST['option1'] ) && $pricing_custom = get_post_meta( $product_id, '_number_field_1', true ) ) {
        $cart_item_data['pricing_custom'] = $pricing_custom;
        $cart_item_data['unique_key']     = md5( microtime().rand() ); // Make each item unique
    }
    return $cart_item_data;
}

// Change the product price
add_action( 'woocommerce_before_calculate_totals', 'action_before_calculate_totals_callback', 10, 1 );
function action_before_calculate_totals_callback( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Avoiding hook repetition and price calculation problems
    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item ) {
        if ( isset( $cart_item['pricing_custom'] ) ) {
            // Set the calculated price
            $cart_item['data']->set_price( $cart_item['data']->get_price() + $cart_item['pricing_custom'] );
        }
    }
}

I need a tweak in both the prices. what if we have three checkboxes?
option1
option2
option3

I tried a few things but calculations were not working.

Show us what you tried, and explain what was wrong with the way the calculations were working. Someone might be able to see why they were not working.

// Add selected add-on option as custom cart item data
add_filter( 'woocommerce_add_cart_item_data', 'filter_add_cart_item_data_callback', 10, 3 );
function filter_add_cart_item_data_callback( $cart_item_data, $product_id, $variation_id ) {
		if(isset( $_POST['option_1'] ) && $_POST['option_2'] ) && $_POST['option_3'] ) &&  $pricing_custom = get_post_meta( $product_id, '_number_field_1', true ) + get_post_meta( $product_id, '_number_field_2', true ) + get_post_meta( $product_id, '_number_field_3', true )){
			$cart_item_data['pricing_custom'] = $pricing_custom;
			$cart_item_data['unique_key']     = md5( microtime().rand() ); // Make each item unique
		}else if(isset( $_POST['option_1'] ) && $_POST['option_2'] )) &&  $pricing_custom = get_post_meta( $product_id, '_number_field_1', true ) + get_post_meta( $product_id, '_number_field_2', true )){
			$cart_item_data['pricing_custom'] = $pricing_custom;
			$cart_item_data['unique_key']     = md5( microtime().rand() ); // Make each item unique

		}else {
			$cart_item_data['pricing_custom'] = $pricing_custom;
			$cart_item_data['unique_key']     = md5( microtime().rand() ); // Make each item unique
		}
    // if ( isset( $_POST['option_1'] ) &&  $pricing_custom = get_post_meta( $product_id, '_number_field_1', true ) ) {
    //     $cart_item_data['pricing_custom'] = $pricing_custom;
    //     $cart_item_data['unique_key']     = md5( microtime().rand() ); // Make each item unique
    // }
    return $cart_item_data;
}

generated this error:

**Parse error** : syntax error, unexpected '&&' (T_BOOLEAN_AND) in **/home3/s**.....

It’d be a help if you didn’t truncate the error message, and show which line is throwing it.

Count the opening and closing brackets on this line though:

if(isset( $_POST['option_1'] ) && $_POST['option_2'] ) && $_POST['option_3'] ) &&  $pricing_custom = get_post_meta( $product_id, '_number_field_1', true ) + get_post_meta( $product_id, '_number_field_2', true ) + get_post_meta( $product_id, '_number_field_3', true )){

You open 5 brackets, and you close 7 - no wonder it has trouble parsing the line. Same here:

}else if(isset( $_POST['option_1'] ) && $_POST['option_2'] )) &&  $pricing_custom = get_post_meta( $product_id, '_number_field_1', true ) + get_post_meta( $product_id, '_number_field_2', true )){

4 vs 6 on here.

Hi there,

sometimes error messages are misleading:

that line deosnt seems to have any error:

functions.php is calling woofunctions.php

376 line is this →

return $cart_item_data;

I think I fixed it →

// Add selected add-on option as custom cart item data
add_filter( 'woocommerce_add_cart_item_data', 'filter_add_cart_item_data_callback', 10, 3 );
function filter_add_cart_item_data_callback( $cart_item_data, $product_id, $variation_id ) {
		if(isset( $_POST['option_1'])  && isset($_POST['option_2'])  && isset($_POST['option_3'])  &&  $pricing_custom = get_post_meta( $product_id, '_number_field_1', true ) + get_post_meta( $product_id, '_number_field_2', true ) + get_post_meta( $product_id, '_number_field_3', true )){
			$cart_item_data['pricing_custom'] = $pricing_custom;
			$cart_item_data['unique_key']     = md5( microtime().rand() ); // Make each item unique
		}else if(isset( $_POST['option_1'] ) && isset($_POST['option_2'])  &&  $pricing_custom = get_post_meta( $product_id, '_number_field_1', true ) + get_post_meta( $product_id, '_number_field_2', true )){
			$cart_item_data['pricing_custom'] = $pricing_custom;
			$cart_item_data['unique_key']     = md5( microtime().rand() ); // Make each item unique

		}else {
			$pricing_custom = get_post_meta( $product_id, '_number_field_1', true );
			$cart_item_data['pricing_custom'] = $pricing_custom;
			$cart_item_data['unique_key']     = md5( microtime().rand() ); // Make each item unique
		}
    return $cart_item_data;
}

Sepearate isset was needed at every checkbox.

There is a logical flaw in it. Last else is not checking if the checkbox option_1 is ticked or not, but assuming it is ticked. How can we fix this? Can we introduce if within else?

Most editors have a highlight brace matching facility.

Try selecting the first brace and see if there is an underlined closing brace.

Also try temporarily removing script blocks /* … */ until the error is removed then gradually introduce script until the error occurs again. This is far quicker than posting and waiting for a possible reply.

Previous issue is fixed.

This is an issue now.

I would just test for one if condition then close the if statement because if the first fails why bother testing for other conditions, put them in a separate if statement.

Its not like that sir.

3 chechboxes can be ticked and hence price
2 checkboxes can be ticked hence price
1 checkbox can be ticked hence price
0 box can be ticked and no price addition.

Based on the checkboxes prices are to be added to base price.

image

This is finally working →

// Add selected add-on option as custom cart item data
add_filter( 'woocommerce_add_cart_item_data', 'filter_add_cart_item_data_callback', 10, 3 );
function filter_add_cart_item_data_callback( $cart_item_data, $product_id, $variation_id ) {
		if(isset( $_POST['option_1'])  && isset($_POST['option_2'])  && isset($_POST['option_3'])  &&  $pricing_custom = get_post_meta( $product_id, '_number_field_1', true ) + get_post_meta( $product_id, '_number_field_2', true ) + get_post_meta( $product_id, '_number_field_3', true )){
			$cart_item_data['pricing_custom'] = $pricing_custom;
			$cart_item_data['unique_key']     = md5( microtime().rand() ); // Make each item unique
		}else if(isset( $_POST['option_1'] ) && isset($_POST['option_2'])  &&  $pricing_custom = get_post_meta( $product_id, '_number_field_1', true ) + get_post_meta( $product_id, '_number_field_2', true )){
			$cart_item_data['pricing_custom'] = $pricing_custom;
			$cart_item_data['unique_key']     = md5( microtime().rand() ); // Make each item unique

		}elseif(isset( $_POST['option_1']) && $pricing_custom = get_post_meta( $product_id, '_number_field_1', true )) {			
			$cart_item_data['pricing_custom'] = $pricing_custom;
			$cart_item_data['unique_key']     = md5( microtime().rand() ); // Make each item unique
		}
    return $cart_item_data;
}

SYNTAX:

if (condition) {
    code to be executed if this condition is true;
} elseif (condition) {
    code to be executed if first condition is false and this condition is true;
} elseif(condition) {
    code to be executed if first and second condition is false and this condition is true;
}

As does the one cip is using - you can see it matching curly-braces in the screen-shot.

Parse errors are always difficult to figure out - sometimes it just gives the last line of the file as the line with the error.

To make things like look slightly cleaner, keep in mind you can do this:

if(isset( $_POST['option_1'], $_POST['option_2'], $_POST['option_3'])) ...
  • that is, put multiple variables into a single isset() call, and it only returns true if all three are set.
1 Like

Thanks that looks more polished.

[quote=“codeispoetry, post:12, topic:342664”]
if(isset( $_POST[‘option_1’]) && isset($_POST[‘option_2’]) && [/quote] …

Try this as a standalone script and rem out the three $_POST options in order to test all eight possible options:

<?php 
declare(strict_types=1);
error_reporting(-1);
ini_set('display_errors', '1');

 $_POST['option_1'] = 'chkd';
 $_POST['option_2'] = 'chkd';
 $_POST['option_3'] = 'chkd';

$a1 = $_POST['option_1'] ?? FALSE;
$a2 = $_POST['option_2'] ?? FALSE;
$a3 = $_POST['option_3'] ?? FALSE;

echo '<br>';
  echo '<br>$a1 ==> ' .$a1;
  echo '<br>$a2 ==> ' .$a2;
  echo '<br>$a3 ==> ' .$a3;
echo '<br>';

if( $a1 && $a2 && $a3 ):
  echo '<br>$a1 && $a2 && $a3';

elseif($a1 && $a2):    
  echo '<br>Both $a1 && $a2';

elseif($a1 && $a3):    
  echo '<br>Both $a1 && $a3';

elseif($a2 && $a3):    
  echo '<br>Both $a2 && $a3';

elseif($a1):    
  echo '<br>Only $a1';

elseif($a2):    
  echo '<br>Only $a2';

elseif($a3):    
  echo '<br>Only $a3';

else:
  echo '<br>NONE chkd';

endif;

1 Like

You are right I missed many permutations/combinations

3C3 + 3C2 + 3C1 =5

I took only 3 in consideration.

1 Like

Can u explain this?

https://dzone.com/articles/whats-new-in-php-7-and-php-7-new-features

Null Coalescing Operator

Q: What is a Null coalescing operator and what are the uses of it?

A: The null coalescing operator is represent edlike this ?? .
It’s used to check if the value is set or null, or in other words,
if the value is exists and not null,
then it returns the first operand,
otherwise it returns the second operand.

Example:

1 Like

So by so doing ISSET is not required any more?

This is flawed. It always give them the checked status whether they are checked or not.