第一段代码将确保为每个变体显示一个额外的复选框。
- 其目的是用于仅包含 2 个变体(A 和 B 变体)的可变产品
- 复选框已选中 2 个变体中的 1 个,这将代表“A 变体”
// Add checkbox
function action_woocommerce_variation_options( $loop, $variation_data, $variation ) {
// Checkbox checked (ticked)
$checked = get_post_meta( $variation->ID, '_mycheckbox', true ) == 'yes' ? 'checked' : '';
// Output
?>
<label class="tips" data-tip="<?php esc_attr_e( 'This is my data tip', 'woocommerce' ); ?>">
<?php esc_html_e( 'This my checkbox:', 'woocommerce' ); ?>
<input type="checkbox" class="checkbox variable_checkbox" name="variable_mycheckbox[<?php echo esc_attr( $loop ); ?>]"<?php echo $checked; ?>/>
</label>
<?php
}
add_action( 'woocommerce_variation_options', 'action_woocommerce_variation_options', 10, 3 );
// Save checkbox
function action_woocommerce_admin_process_variation_object( $variation, $i ) {
// Isset, yes or no
$value = isset( $_POST['variable_mycheckbox'][$i] ) ? 'yes' : 'no';
// Update
$variation->update_meta_data( '_mycheckbox', $value );
}
add_action( 'woocommerce_admin_process_variation_object', 'action_woocommerce_admin_process_variation_object', 10, 2 );
第二段代码将检查 2 个变体中的 1 个(A 变体)的复选框是否打开
- 如果“A 变体”是这种情况,并且“B 变体”在购物车中。
然后,“B 变体”将获得 15% 的折扣
// Used to calculate totals
function action_woocommerce_before_calculate_totals( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Discount in percent
$discount = 15;
// Iterating though each cart items
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
// Only for variations
if ( $cart_item['data']->get_type() == 'variation' ) {
// Get the value of the checkbox
$checked = $cart_item['data']->get_meta( '_mycheckbox' );
// Variant is checked
if ( $checked == 'yes' ) {
// Get all variations ID of a variable product
// Main function for returning products
$variation = wc_get_product( $cart_item['product_id'] );
// Get product child ids (Array)
$child_ids = $variation->get_children();
// Normally there are 2 childsIDs, we will remove the current one so that the other variantionID is known
$other_variantion_id = array_diff( $child_ids, array( $cart_item['variation_id'] ) );
// Convert to integer
$other_variantion_id = (int) reset( $other_variantion_id );
// Call function - other product ID in cart? yes, then get that cart item
$other_cart_item = product_id_in_cart_get_cart_item( $other_variantion_id );
// Finds whether a variable is an array
if ( is_array( $other_cart_item ) ) {
// Calculate new price
$new_price = round( $other_cart_item['data']->get_price() * ( ( 100 - $discount ) / 100 ), 2 );
// Set price
$other_cart_item['data']->set_price( $new_price );
}
}
}
}
}
add_action( 'woocommerce_before_calculate_totals', 'action_woocommerce_before_calculate_totals', 10, 1 );
// Product ID in cart? return cart_item
function product_id_in_cart_get_cart_item( $product_id ) {
// Iterating though each cart items
foreach( WC()->cart->get_cart() as $cart_item ) {
// Compare
if ( $product_id == $cart_item['data']->get_id() ) {
// In cart
return $cart_item;
}
}
return false;
}