在Woocommerce结帐部分,我试图添加一个复选框,以添加其他订阅产品。它工作得很好,我得到了here
还有一个要求是,如果购物车中有其他产品,那么我想根据自定义字段更改每个产品的价格,并且新的价格应该显示在结帐中。提前谢谢。
我使用了此代码,工作正常,但贝宝支付页面中的产品价格没有变化。
// 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\' => \' \' . __(\'Please check here to get VIP Membership\'),
\'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 = 179;
if( WC()->session->get(\'add_a_product\') == \'yes\' && ! WC()->session->get(\'product_added_key\') )
{
foreach ( $cart->get_cart() as $cart_item ) {
$cart_item_key = $cart->add_to_cart( $product_id );
WC()->session->set(\'product_added_key\', $cart_item_key);
// get the product id (or the variation id)
$product_id = $cart_item[\'data\']->get_id();
// GET THE NEW PRICE (code to be replace by yours)
$new_price = get_post_meta( $product_id, \'pro_price_extra_info\', true );
// Updated cart item price
$cart_item[\'data\']->set_price( floatval( $new_price ) );
}
}
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\');
}
}
SO网友:Praveen
我解决了我的问题。此代码将来可能会帮助其他人。
// 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\' => \' \' . __(\'Please check here to get VIP Membership\'),
\'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);
}
});
if(value == \'no\'){
window.location.reload();
}
});
});
</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;
$has_sub = wcs_user_has_subscription( \'\', \'179\', \'active\' );
$product_id = 179;
if( WC()->session->get(\'add_a_product\') == \'yes\' && ! WC()->session->get(\'product_added_key\') ){
// HERE the specific Product ID
foreach ( $cart->get_cart() as $cart_item ) {
// get the product id (or the variation id)
$product_idnew = $cart_item[\'data\']->get_id();
// GET THE NEW PRICE (code to be replace by yours)
$new_price = get_post_meta( $product_idnew, \'pro_price_extra_info\', true );
$old_price = $cart_item[\'data\']->get_price();
if($new_price == NULL){
$cart_item[\'data\']->set_price( floatval( $old_price ) );
} else {
$cart_item[\'data\']->set_price( floatval( $new_price ) );
}
}
$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\');
}elseif ( $has_sub ) {
foreach ( $cart->get_cart() as $cart_item ) {
// get the product id (or the variation id)
$product_idnew = $cart_item[\'data\']->get_id();
// GET THE NEW PRICE (code to be replace by yours)
$new_price = get_post_meta( $product_idnew, \'pro_price_extra_info\', true );
$cart_item[\'data\']->set_price( floatval( $new_price ) );
}
}
}
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;
}
add_action( \'woocommerce_cart_loaded_from_session\', \'adding_vip_product\' );
function adding_vip_product( $cart ) {
$product_id = 179;
if(woo_in_cart($product_id) ) {
foreach ( $cart->get_cart() as $cart_item ) {
// get the product id (or the variation id)
$product_idnew = $cart_item[\'data\']->get_id();
// GET THE NEW PRICE (code to be replace by yours)
$new_price = get_post_meta( $product_idnew, \'pro_price_extra_info\', true );
if($new_price != 0){
$cart_item[\'data\']->set_price( floatval( $new_price ) );
}
}
}
}