简体   繁体   中英

Update a custom field on "Update Cart" click in WooCommerce Cart

I added a custom <select> field to each product in cart page to be able to update variation, but when I change it's value and click "Update Cart", nothing updates unless quantity field has also been changed.

Is there a way to avoid this?


My code:

functions.php file:

add_filter( 'woocommerce_update_cart_action_cart_updated', 'on_action_cart_updated', 20, 1 );
function on_action_cart_updated( $cart_updated ){

    if ($cart_updated) {
        $cart_content = WC()->cart->get_cart_contents();
        $update_cart = false;
        $cart_totals  = isset( $_POST['cart'] ) ? wp_unslash( $_POST['cart'] ) : '';

        if ( ! empty( $cart_content ) && is_array( $cart_totals ) ) {

            foreach ($cart_content as $key => $item) {

                $lease_period = $cart_totals[$key]['lease'];

                if ( ! empty( $lease_period )) {
                    $cart_content[$key]['variation']['attribute_pa_lease-period'] = $lease_period;
                    $update_cart = true;
                }
            }

            if ($update_cart) {
                WC()->cart->set_cart_contents($cart_content);
            }
        }
    }
}

cart.php file:

<td class="product-lease-period" data-title="<?php esc_attr_e( 'Lease Period', 'woocommerce' ); ?>">
                            <div class="product-lease-period-select">
                                <select name="cart[<?php echo $cart_item_key ?>][lease]">

                                    <?php
                                        $lease_periods = ['6-months'=> '6 Months',
                                                            '12-months' => '12 Months',
                                                            '18-months' => '18 Months',
                                                            '24-months' => '24 Months'];

                                        foreach ($lease_periods as $key => $period) {

                                            $selected = '';

                                            if ($cart_item['variation']['attribute_pa_lease-period'] == $key) {
                                                $selected = 'selected="selected"';
                                            }

                                            echo "<option value=" . $key . " $selected>" . $period . "</option>";
                                        }
                                    ?>
                                </select>
                            </div>
                        </td>

My conclusions so far:

I believe it's because of these pieces of code inside the class-wc-form-handler.php :

// Skip product if no updated quantity was posted.
if ( ! isset( $cart_totals[ $cart_item_key ] ) || ! isset( $cart_totals[ $cart_item_key ]['qty'] ) ) {
    continue;
}

and a bit below:

if ( '' === $quantity || $quantity === $values['quantity'] ) {
    continue;
}

I extended the WC_Form_Handler class in my functions.php file, copied a method I needed and edited it, and gave it higher hook priority in my extended class than it's in the original class:

class WC_Form_Handler_Ext extends WC_Form_Handler {

    /**
     * Hook in method.
     */
    public static function init() {
        add_action( 'wp_loaded', array( __CLASS__, 'update_cart_action' ), 30 );
    }

    /**
     * Remove from cart/update.
     */
    public static function update_cart_action() {
        // method content edited
    }
}

WC_Form_Handler_Ext::init();

UPDATE:

To make price change after variation value in cart is updated, this function needs to be added to the functions.php

function find_matching_product_variation_id($product_id, $attributes)
{
    return (new WC_Product_Data_Store_CPT())->find_matching_product_variation(
        new WC_Product($product_id),
        $attributes
    );
}

And it should be called from the loop in functions.php I mentioned in my question this way:

$attributes = $cart_content[$key]['variation'];
$variation_id = find_matching_product_variation_id($product_id, $attributes);
$price = get_post_meta($variation_id, '_price', true);
$item['data']->set_price($price);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM