在WooCommerce中,我希望有一个复选框,当按下该复选框时,它会将产品添加到我的购物车中,并且该产品的价格为购物车总价的3%。
基于“ Checkbox field that add to cart a product in Woocommerce checkout page” 回答线程并进行了一些修改以满足我的需要,这是我的代码:
// Display a custom checkout field
add_action( 'woocommerce_checkout_before_terms_and_conditions', 'custom_checkbox_checkout_field' );
function custom_checkbox_checkout_field() {
$value = WC()->session->get('add_a_product');
woocommerce_form_field( 'cb_add_product', array(
'type' => 'checkbox',
'label' => ' ' . __('Add Assembly Service (3% extra)'),
'class' => array('form-row-wide'),
), $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', '#cb_add_product', 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;
// HERE the specific Product ID
$product_id = 1514;
$result = WC()->session->get('add_a_product');
if( strpos($result, 'yes') !== false && ! 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( strpos($result, 'no') !== false && 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');
}
}
function woocommerce_custom_price_to_cart_item( $cart ) {
if (is_admin() && !defined('DOING_AJAX'))
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
if( !WC()->session->__isset( "reload_checkout" )) {
global $woocommerce;
$totalPriceBeforeService = $cart->cart_contents_total;
$servicePrice = (float) ceil($totalPriceBeforeService) * 0.03;
foreach ( $cart->cart_contents as $key => $value ) {
$product_idnew = $value['data']->get_id();
if($product_idnew == 1514){
//set new prices
$value['data']->set_price($servicePrice);
$new_price = $value['data']->get_price();
echo" $new_price ";
}
}
}
}
add_action( 'woocommerce_before_calculate_totals', 'woocommerce_custom_price_to_cart_item' );
它很好地增加了产品,一切都很好。但是,当我设置价格时,它没有更新。
好的,这是未选中的复选框。
这是选中后的复选框。如您所见,该项目的值为46.5,但未在购物车中更新。知道如何解决这个问题吗?
答案 0 :(得分:1)
您使用2x git add
,我将其减少为1。
git worktree add
还包括(新添加产品的)原始产品价格,因此将在最终计算之前扣除。
if self.categories.datas.isEmpty {
Text("")
} else {
Text(self.categories.datas[1].name)
}