Woocommerce checkout:将产品添加到购物车问题的复选框



基于Woocommerce结账页面答案代码中的复选框字段添加到购物车中,我的以下代码尝试工作完美:它允许客户通过单击复选框将特定产品添加到购物车中,然后通过AJAX更新以添加或删除基于它是否被单击的产品。

我的问题是有一个漏洞。如果客户刷新结帐页面,则复选框将恢复为未选中状态,并允许客户添加第二个或更多产品(如果他们保持刷新)。

我还有一个检查购物车中是否有特定产品的函数,它工作得很好。我做不到的是……

如果在checkout加载时该特定产品在购物车中,则应该选中自定义复选框的默认状态。

以下复选框代码:

// -----------------------------------------
// ADD A $5 product TO THE CHECKOUT PAGE
$surprise_product_activated = get_field( 'surprise_product_activated', 'options' );
$surprise_product = get_field( 'surprise_product', 'options' );
if ( $surprise_product_activated == true ) {
// Display a custom checkout field
add_action( 'woocommerce_before_order_notes', 'add_a_product' , 10, 2 );
}
function add_a_product() {
$value = WC()->session->get('add_a_product');
$surprise_product = get_field( 'surprise_product', 'options' );
$product = wc_get_product( $surprise_product );
$price = $product->get_price();

woocommerce_form_field( 'cb_add_product', array(
'type'          => 'checkbox',
'label'         => '<div class="specialty-container f-grey"><div class="custom-checkbox float-right">
<input type="checkbox" id="animated-checkbox" />
<svg viewBox="0 0 35.6 35.6">
<circle class="background" cx="17.8" cy="17.8" r="17.8"></circle>
<circle class="stroke" cx="17.8" cy="17.8" r="14.37"></circle>
<polyline class="check" points="11.78 18.12 15.55 22.23 25.17 12.87"></polyline>
</svg></div>&nbsp;&nbsp;<span class="specialty-product clearfix"><span class="special-offer">Special Offer</span><br>' . __('Add a <strong>Surprise product</strong> for just ') . '<strong class="f-purple">$' . $price . '?</strong></span></div>',
'class'         => array('form-row-wide five-dollar-product'),
), $value == 'yes' ? true : false );
}

// The jQuery Ajax request
add_action( 'wp_footer', 'checkout_custom_jquery_script' );
function checkout_custom_jquery_script() {
// Only checkout page
if( is_checkout() && ! is_wc_endpoint_url() ):
// Remove "ship_different" custom WC session on load
if( WC()->session->get('add_a_product') ){
WC()->session->__unset('add_a_product');
}
if( WC()->session->get('product_added_key') ){
WC()->session->__unset('product_added_key');
}
// jQuery Ajax code
?>
<script type="text/javascript">
jQuery( function($){
if (typeof wc_checkout_params === 'undefined')
return false;
$('form.checkout').on( 'change', '#animated-checkbox', function(){
var value = $(this).prop('checked') === true ? 'yes' : 'no';
$.ajax({
type: 'POST',
url: wc_checkout_params.ajax_url,
data: {
'action': 'add_a_product',
'add_a_product': value,
},
success: function (result) {
$('body').trigger('update_checkout');
console.log(result);
}
});
});
});
</script>
<?php
endif;
}
// The Wordpress Ajax PHP receiver
add_action( 'wp_ajax_add_a_product', 'checkout_ajax_add_a_product' );
add_action( 'wp_ajax_nopriv_add_a_product', 'checkout_ajax_add_a_product' );
function checkout_ajax_add_a_product() {
if ( isset($_POST['add_a_product']) ){
WC()->session->set('add_a_product', esc_attr($_POST['add_a_product']));
echo $_POST['add_a_product'];
}
die();
}
// Add remove free product
add_action( 'woocommerce_before_calculate_totals', 'adding_removing_specific_product' );
function adding_removing_specific_product( $cart ) {
if (is_admin() && !defined('DOING_AJAX'))
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
$surprise_product = get_field( 'surprise_product', 'options' );
// HERE the specific Product ID
$product_id = $surprise_product;
if( WC()->session->get('add_a_product') == 'yes' && ! WC()->session->get('product_added_key') )
{
$cart_item_key = $cart->add_to_cart( $product_id );
WC()->session->set('product_added_key', $cart_item_key);
}
elseif( WC()->session->get('add_a_product') == 'no' && WC()->session->get('product_added_key') )
{
$cart_item_key = WC()->session->get('product_added_key');
$cart->remove_cart_item( $cart_item_key );
WC()->session->__unset('product_added_key');
}
}

并检查产品是否已经在购物车中:

// -----------------------------------------
// CHECK IF PRODUCT ALREADY IN CART
// -----------------------------------------
function woo_in_cart($product_id) {
global $woocommerce;         
foreach($woocommerce->cart->get_cart() as $key => $val ) {
$_product = $val['data'];
if($product_id == $_product->id ) {
return true;
}
}         
return false;
}

由于您使用的是ACF而不是自定义代码,因此您的代码并不能真正进行全局测试。

要避免结帐重新加载问题,请尝试替换checkout_custom_jquery_script()函数:

if( WC()->session->get('product_added_key') ){
WC()->session->__unset('product_added_key');
}

由:

if( $cart_item_key = WC()->session->get('product_added_key') ){
WC()->cart->remove_cart_item( $cart_item_key );
WC()->session->__unset('product_added_key');
}

应避免客户能够多次添加"惊奇产品";如果它重新加载结账页面…

最新更新