如何获取和修改购物车中第二件商品的价格?
我想对第二个产品进行-3%的折扣(购物车中的商品已按价格排序,最高的顶部)。
我认为它必须计算在woocommerce_before_calculate_totals或喜欢折扣woocommerce_cart_calculate_fees?
谢谢
我正在尝试将复选框添加到 WooCommerce(在管理面板中)的设置选项卡并使用以下代码:
add_action( 'woocommerce_product_options_general_product_data', 'wc_custom_add_custom_fields' );
function wc_custom_add_custom_fields() {
global $post;
woocommerce_wp_checkbox(array(
'id' => 'is_gift',
'label' => __('Gift', 'woocommerce' ),
'description' => __( 'Add gift label', 'woocommerce' ),
'value' => get_post_meta($post->ID, 'is_gift', true)
));
}
add_action( 'woocommerce_process_product_meta', 'wc_custom_save_custom_fields' );
function wc_custom_save_custom_fields() {
global $post;
if (!empty($_POST['is_gift'])) {
update_post_meta( $post->ID, 'is_gift', esc_attr( $_POST['is_gift'] ) );
}
}
Run Code Online (Sandbox Code Playgroud)
此代码显示复选框,但不保存更改。它仅适用于一种产品。我猜有什么问题$post->ID吗?