简体   繁体   English

在 WooCommerce 购物车中单击“更新购物车”更新自定义字段

[英]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.我向购物车页面中的每个产品添加了一个自定义<select>字段,以便能够更新变体,但是当我更改它的值并单击“更新购物车”时,除非数量字段也已更改,否则不会更新任何内容。

Is there a way to avoid this?有没有办法避免这种情况?


My code:我的代码:

functions.php file:函数.php文件:

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:购物车.php文件:

<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 :我相信这是因为 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:我在我的functions.php文件中扩展了WC_Form_Handler类,复制了一个我需要的方法并对其进行了编辑,并在我的扩展类中赋予它比原始类更高的钩子优先级:

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要在更新购物车中的变化值后更改价格,需要将此功能添加到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:它应该从我在我的问题中提到的functions.php中的循环中调用:

$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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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