【问题标题】:Update a custom field on "Update Cart" click in WooCommerce Cart在 WooCommerce 购物车中单击“更新购物车”更新自定义字段
【发布时间】:2019-08-07 14:08:32
【问题描述】:

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

有没有办法避免这种情况?


我的代码:

functions.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 文件:

<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>

到目前为止我的结论:

我相信是因为 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;
}

还有一点:

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

【问题讨论】:

    标签: php ajax wordpress woocommerce cart


    【解决方案1】:

    我在我的 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();
    

    更新:

    要在购物车中的变化值更新后更改价格,需要在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
        );
    }
    

    并且应该从我在问题中提到的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);
    

    【讨论】:

      猜你喜欢
      • 2017-01-20
      • 1970-01-01
      • 1970-01-01
      • 2018-01-15
      • 2018-07-29
      • 1970-01-01
      • 2018-02-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多