Woocommerce set_quantity crashes site when adding

2019-08-18 11:16发布

I've set up a custom calculator in WooCommerce as a new plugin.

From this calculator, I am passing my total price and total quantity. The price is calculated by taking the total square inches of the product (it is a piece of cloth) and multiplying that by the total number of pieces ordered.

But we are pricing the shipping based on total pounds, and have set this to be 0.003/sq inch. So, in order to calculate the shipping correctly, I will need to pass this information to the cart. To be clear, I will need to pass the total square inches of the cloth to the cart, where it will then price it based on this.

I've got the quantity and price to add to the cart by using a hidden value field and the following code:

add_action( 'woocommerce_add_cart_item_data', 'save_custom_fields_data_to_cart', 10, 2 );
function save_custom_fields_data_to_cart( $cart_item_data, $product_id ) {

    if( ! empty( $_REQUEST['custom_price'] && $_REQUEST['custom_quantity'] ) ) {
        // Set the custom data in the cart item
        if($_REQUEST['custom_price'] < 25) {
            $cart_item_data['custom_price'] = 25.00;
        } else {
            $cart_item_data['custom_price'] = $_REQUEST['custom_price'];
        }
        // Set the custom data in the cart item
        $cart_item_data['custom_quantity'] = $_REQUEST['custom_quantity'];
        // Make each item as a unique separated cart item
        $cart_item_data['unique_key'] = md5( microtime().rand() );
    }
    return $cart_item_data;
}

But I am running into issues with the following code:

add_action( 'woocommerce_before_calculate_totals', 'change_cart_item_price', 30, 1 );
function change_cart_item_price( $cart ) {
    if ( ( is_admin() && ! defined( 'DOING_AJAX' ) ) )
        return;

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item ) {
        // Set the new price
        if( isset($cart_item['custom_price']) ){
            $cart_item['data']->set_price($cart_item['custom_price']);
        }
        //set the new quantity
        if( isset($cart_item['custom_quantity']) ) {
            $cart_item['data']->set_quantity($cart_item['custom_quantity']);
        }
    }
}

The price passed just fine, however when the code hit's the below part:

if( isset($cart_item['custom_quantity']) ) {
                $cart_item['data']->set_quantity($cart_item['custom_quantity']);
            }

It fails and crashes the site. Specifically the line

$cart_item['data']->set_quantity($cart_item['custom_quantity']);

I know this because I ran a quick echo "good" in it's place and it passed fine.

Am I using set_quantity incorrectly?

0条回答
登录 后发表回答