AJAX从单个产品页面添加自定义产品订单元数据

时间:2019-07-03 17:28:04

标签: php woocommerce hook-woocommerce

我建立了一个添加到购物车的流程,当将商品添加到购物车时,该流程成功创建了一个“个性化”字段。使用标准的“添加到购物车”按钮,页面将重新加载,一切正常。我想通过AJAX进行篮子更新来改善用户体验,这已经实现并且可以正常工作。

但是,由于未定义请求,这停止了购物车数据元的注册-挂钩不再能够从页面中获取输入值。我用来定义元字段并将其写入其中的钩子是“ woocommerce_add_cart_item_data”

我尝试使用$ _SESSION而不是$ _REQUEST(受此答案启发-> how to pass ajax data to woocommerce filter?)创建自己的单独的AJAX函数来分别定义字段的内容

不幸的是,这似乎在“ woocommerce_add_cart_item_data”运行后触发,因此元数据没有通过(实际上,它最终出现在添加的下一个产品上,因为变量定义得较晚,直到添加第二个产品时才访问)。

我真的很感谢其中的一些帮助:

(1)修改代码,以便“ woocommerce_add_cart_item_data”能够从AJAX中获取数据并添加到我进行的购物车操作中,或者

(2)修改我的自定义AJAX调用,以便可以及时应用会话数据(根据目前为止的情况,不确定是否可以实现此操作)。

// Create AJAX add to cart button
function add_cart_btn($prod) {
            echo apply_filters( 'woocommerce_loop_add_to_cart_link',
                sprintf( '<a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" class="single-product__details-cta button %s product_type_%s ajax_add_to_cart" name="add_to_cart">Add to bag</a>',
                    esc_url( $prod->add_to_cart_url() ),
                    esc_attr( $prod->id ),
                    esc_attr( $prod->get_sku() ),
                    $prod->is_purchasable() ? 'add_to_cart_button' : '',
                    esc_attr( $prod->product_type ),
                    esc_html( $prod->add_to_cart_text() )
                ),
            $prod );
        }
        add_cart_btn($product);



// functions.php

/* Saves field data */
function save_add_custom_info_field( $cart_item_data, $product_id ) {
    if( isset( $_REQUEST['custom_info_message'] ) ) {

        // *** $_REQUEST['custom_info_message'] not defined ***
        $cart_item_data[ 'custom_info_message' ] = $_REQUEST['custom_info_message'];
        $cart_item_data['unique_key'] = md5( microtime().rand() );
    }
    return $cart_item_data;
}
add_action( 'woocommerce_add_cart_item_data', 'save_add_custom_info_field', 10, 2 );

/* Renders field entry on cart and checkout */
function render_mssg_meta_on_cart_and_checkout( $cart_data, $cart_item = null ) {
    $custom_items = array();
    if( !empty( $cart_data ) ) {
        $custom_items = $cart_data;
    }
    if( isset( $cart_item['custom_info_message'] ) ) {
        $custom_items[] = array( "name" => 'Personalisation / Customisation', "value" => $cart_item['custom_info_message'] );
}
    return $custom_items;
}
add_filter( 'woocommerce_get_item_data', 
'render_mssg_meta_on_cart_and_checkout', 10, 2 );


// *** Attempted solution with AJAX - Use $_SESSION['message'] in 'save_add_custom_info_field' function above instead of $_REQUEST... delivered after 'woocommerce_add_cart_item_data' so doesn't work

function set_customised_message() {

    $message = $_POST['message'];

    define( 'DOING_AJAX', true );
    if ( ! defined( 'WP_ADMIN' ) ) {
        define( 'WP_ADMIN', true );
    }

    session_start();
    $_SESSION['message'] = $message;

    echo $message;

    die();

}

我希望自定义meta会显示在购物车/结帐页面上,但这不是由于未定义$ cart_item ['custom_info_message']变量或AJAX调用中的会话变量不可用。

1 个答案:

答案 0 :(得分:0)

我设法通过将产品元数据添加到购物车后(通过AJAX)更新产品元数据来解决此问题-修改了上面的set_customised_message函数。请注意,AJAX调用需要1秒的超时延迟才能正常工作。

如果其他人遇到类似的问题,请留在这里。

function set_customised_message() {
    $cart = WC()->cart->cart_contents;

 foreach( $cart as $cart_item_id=>$cart_item ) {
     echo $cart_item['unique_key'];
 $cart_item['custom_info_message'] = $message;
 echo $cart_item['custom_info_message'];
 WC()->cart->cart_contents[$cart_item_id] = $cart_item;
 }
 WC()->cart->set_session();
}

感谢https://pluginrepublic.com/how-to-update-existing-woocommerce-cart-meta-data/的解决方案

相关问题