我有一个问题,我自己无法解决。
我使用自定义输入字段供客户输入个人文本。如果我不使用Ajax请求,那么它可以运行,但是不能。
这是“自定义”输入字段代码
/*
* Display input on single product page
* @return html
*/
function olikdesign_Wunschtext(){
global $post;
$terms = wp_get_post_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ) $categories[] = $term->slug;
$woocommerce;
$value = isset( $_POST['_Wunschtext'] ) ? sanitize_text_field( $_POST['_Wunschtext'] ) : '';
// Wunschtext Textfeld anzeigen, sofern der Artikel sich in der Kategorie Custom befindet.
if ( in_array( 'individuell', $categories ) ) {
printf( '<div class="variations wunschtext"><label for="wunschtext">%s</label><input type="text" id="wunschtext" name="wunschtext" class="input-text-custom_text" maxlength="25" data-attribute_name="attribute_wunschtext" value="%s" /><small class="custom_text_description">Schreibe hier deinen Wunschtext hinein — Max. 25 Zeichen.</small>
</div>', __( 'Wunschtext', 'olikdesign-plugin-textdomain' ), esc_attr( $value ) );
} else {
//Nichts anzeigen
}
}
add_action( 'woocommerce_single_variation', 'olikdesign_Wunschtext', 5 );
/*
* Validate when adding to cart
* @param bool $passed
* @param int $product_id
* @param int $quantity
* @return bool
*/
function olikdesign_add_to_cart_validation($passed, $product_id, $qty){
if( isset( $_POST['_Wunschtext'] ) && sanitize_text_field( $_POST['_Wunschtext'] ) == '' ){
$product = wc_get_product( $product_id );
wc_add_notice( sprintf( __( '”%s” konnte nicht hinzugefügt werden. Schreibe einen Wunschtext.', 'olikdesign-plugin-textdomain' ), $product->get_title() ), 'error' );
return false;
}
return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'olikdesign_add_to_cart_validation', 10, 3 );
/*
* Add custom data to the cart item
* @param array $cart_item
* @param int $product_id
* @return array
*/
function olikdesign_add_cart_item_data( $cart_item, $product_id ){
if( isset( $_POST['_Wunschtext'] ) ) {
$cart_item['Wunschtext'] = sanitize_text_field( $_POST['_Wunschtext'] );
}
return $cart_item;
}
add_filter( 'woocommerce_add_cart_item_data', 'olikdesign_add_cart_item_data', 10, 2 );
/*
* Load cart data from session
* @param array $cart_item
* @param array $other_data
* @return array
*/
function olikdesign_get_cart_item_from_session( $cart_item, $values ) {
if ( isset( $values['Wunschtext'] ) ){
$cart_item['Wunschtext'] = $values['Wunschtext'];
}
return $cart_item;
}
add_filter( 'woocommerce_get_cart_item_from_session', 'olikdesign_get_cart_item_from_session', 20, 2 );
/*
* Add meta to order item
* @param int $item_id
* @param array $values
* @return void
*/
function olikdesign_add_order_item_meta( $item_id, $values ) {
if ( ! empty( $values['Wunschtext'] ) ) {
woocommerce_add_order_item_meta( $item_id, 'Wunschtext', $values['Wunschtext'] );
}
}
add_action( 'woocommerce_add_order_item_meta', 'olikdesign_add_order_item_meta', 10, 2 );
/*
* Get item data to display in cart
* @param array $other_data
* @param array $cart_item
* @return array
*/
function olikdesign_get_item_data( $other_data, $cart_item ) {
if ( isset( $cart_item['Wunschtext'] ) ){
$other_data[] = array(
'name' => __( 'Wunschtext', 'olikdesign-plugin-textdomain' ),
'value' => sanitize_text_field( $cart_item['Wunschtext'] )
);
}
return $other_data;
}
add_filter( 'woocommerce_get_item_data', 'olikdesign_get_item_data', 10, 2 );
/*
* Show custom field in order overview
* @param array $cart_item
* @param array $order_item
* @return array
*/
function olikdesign_order_item_product( $cart_item, $order_item ){
if( isset( $order_item['Wunschtext'] ) ){
$cart_item_meta['Wunschtext'] = $order_item['Wunschtext'];
}
return $cart_item;
}
add_filter( 'woocommerce_order_item_product', 'olikdesign_order_item_product', 10, 2 );
/*
* Add the field to order emails
* @param array $keys
* @return array
*/
function olikdesign_email_order_meta_fields( $fields ) {
$fields['custom_field'] = __( 'Wunschtext', 'olikdesign-plugin-textdomain' );
return $fields;
}
add_filter('woocommerce_email_order_meta_fields', 'olikdesign_email_order_meta_fields');
/*
* Order Again
* @param array $cart_item
* @param array $order_item
* @param obj $order
* @return array
*/
function olikdesign_order_again_cart_item_data( $cart_item, $order_item, $order ){
if( isset( $order_item['Wunschtext'] ) ){
$cart_item_meta['Wunschtext'] = $order_item['Wunschtext'];
}
return $cart_item;
}
add_filter( 'woocommerce_order_again_cart_item_data', 'olikdesign_order_again_cart_item_data', 10, 3 );
所以现在我想将此字段添加到我的ajax请求中,但是我无法解决它。
这是我的Ajax代码:
jQuery( function( $ ) {
$(".single_add_to_cart_button").addClass("ajax_add_to_cart");
$( ".post-type-archive-product" ).on( "click", ".quantity input", function() {
return false;
});
$( ".archive" ).on( "change input", ".quantity .qty", function() {
var add_to_cart_button = $( this ).parents( ".product" ).find( ".add_to_cart_button" );
// For AJAX add-to-cart actions
add_to_cart_button.data( "quantity", $( this ).val() );
// For non-AJAX add-to-cart actions
add_to_cart_button.attr( "href", "?add-to-cart=" + add_to_cart_button.attr( "data-product_id" ) + "&quantity=" + $( this ).val() );
$( '#mini-cart, .mini-cart-inner' ).addClass('active').fadeIn(1000);
$("body").addClass("overflow-hidden");
});
$(".input-text.qty.text").bind('keyup mouseup', function () {
var value = $(this).val();
$(".product_quantity").val(value)
});
if ( typeof wc_add_to_cart_params === 'undefined' )
return false;
$( document ).on( 'click', '.ajax_add_to_cart', function(e) {
e.preventDefault();
var $thisbutton = $(this);
var $variation_form = $( this ).closest( '.variations_form' );
var var_id = $variation_form.find( 'input[name=variation_id]' ).val();
$( '.ajaxerrors' ).remove();
var item = {},
check = true;
variations = $variation_form.find( 'select[name^=attribute]' );
if ( !variations.length) {
variations = $variation_form.find( '[name^=attribute]:checked' );
}
if ( !variations.length) {
variations = $variation_form.find( 'input[name^=attribute]' );
}
variations.each( function() {
var $this = $( this ),
attributeName = $this.attr( 'name' ),
attributevalue = $this.val(),
index,
attributeTaxName;
$this.removeClass( 'error' );
if ( attributevalue.length === 0 ) {
index = attributeName.lastIndexOf( '_' );
attributeTaxName = attributeName.substring( index + 1 );
$this
.addClass( 'required error' )
.before( '<div class="ajaxerrors"><p><i class="fas fa-exclamation-triangle"></i> Bitte auswählen:</p></div>' )
check = false;
} else {
item[attributeName] = attributevalue;
}
} );
if ( !check ) {
return false;
}
if ( $thisbutton.is( '.ajax_add_to_cart' ) ) {
$thisbutton.removeClass( 'added' );
$(".cart-contents").removeClass("newadded");
$thisbutton.addClass( 'loading' );
if ($( this ).parents(".variations_form")[0]){
var product_id = $variation_form.find('input[name=product_id]').val();
var quantity = $variation_form.find( 'input[name=quantity]' ).val();
var data = {
action: 'olikdesign_ajax_add_to_cart_woo',
product_id: product_id,
quantity: quantity,
variation_id: var_id,
variation: item
};
} else {
var product_id = $(this).parent().find(".product_id").val();
var quantity = $(this).parent().find(".qty").val();
var data = {
action: 'olikdesign_ajax_add_to_cart_woo_single',
product_id: product_id,
quantity: quantity
};
}
$( 'body' ).trigger( 'adding_to_cart', [ $thisbutton, data ] );
$.post( wc_add_to_cart_params.ajax_url, data, function( response ) {
if ( ! response )
return;
var this_page = window.location.toString();
this_page = this_page.replace( 'add-to-cart', 'added-to-cart' );
if ( response.error && response.product_url ) {
window.location = response.product_url;
return;
}
if ( wc_add_to_cart_params.cart_redirect_after_add === 'yes' ) {
window.location = wc_add_to_cart_params.cart_url;
return;
} else {
$thisbutton.removeClass( 'loading' );
var fragments = response.fragments;
var cart_hash = response.cart_hash;
if ( fragments ) {
$.each( fragments, function( key ) {
$( key ).addClass( 'updating' );
});
}
$( '#mini-cart, .mini-cart-inner' ).addClass('active').fadeIn(1000);
$("body").addClass("overflow-hidden");
$thisbutton.addClass( 'added' );
$(".cart-contents").addClass("newadded");
if ( fragments ) {
$.each( fragments, function( key, value ) {
$( key ).replaceWith( value );
});
}
$( '.widget_shopping_cart, .updating' ).stop( true ).css( 'opacity', '1' ).unblock();
$( '.shop_table.cart' ).load( this_page + ' .shop_table.cart:eq(0) > *', function() {
$( '.shop_table.cart' ).stop( true ).css( 'opacity', '1' ).unblock();
$( document.body ).trigger( 'cart_page_refreshed' );
});
$( '.cart_totals' ).load( this_page + ' .cart_totals:eq(0) > *', function() {
$( '.cart_totals' ).stop( true ).css( 'opacity', '1' ).unblock();
});
}
});
return false;
} else {
return true;
}
});
});
我认为可以很轻松地解决它,但我看不到解决方案。我认为这个“ $ cart_item ['Wunschtext']”必须作为值保存到我的ajax中,但是怎么办?
如果没有输入输入,我也想使错误消息起作用。
我希望有人能帮助我。
在您的时间里,有很多爱<3。
答案 0 :(得分:0)
添加了一些代码,但未保存值:
对我的职能:
function olikdesign_ajax_add_to_cart_woo_callback() {
//在ob_start()之后;
$wunschtext = $_POST['_wunschtext']; // Wunschtext
error_log("Variation Product", 0);
$passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity, $wunschtext);
if ( $passed_validation && WC()->cart->add_to_cart( $product_id, $quantity, $variation_id, $variation, $wunschtext ) ) {
do_action( 'woocommerce_ajax_added_to_cart', $product_id );
if ( get_option( 'woocommerce_cart_redirect_after_add' ) == 'yes' ) {
wc_add_to_cart_message( $product_id );
}
// Return fragments
WC_AJAX::get_refreshed_fragments();
}
添加到我添加的Ajax中
var wunschtext = $variation_form.find( 'input[name=_wunschtext]' ).val(); //Wunschtext
和
var data = {
action: 'olikdesign_ajax_add_to_cart_woo',
product_id: product_id,
quantity: quantity,
variation_id: var_id,
variation: item,
wunschtext: wunschtext // Wunschtext
};
我发现此问题已存在,但无法弄清楚我需要在何处添加下划线(_)。 Same issue here
希望你能理解我。