【发布时间】:2021-04-09 11:52:21
【问题描述】:
我有可变产品,我添加了这样的自定义字段:
function ab_preorder_variation_fields( $loop, $variation_data, $variation ) {
echo '<div class="options_group form-row form-row-full">';
// Is Preordable
woocommerce_wp_checkbox(
array(
'id' => '_ab_preorder_checkbox[' . $variation->ID . ']',
'wrapper_class' => 'show_if_simple',
'label' => __(' Disponible à la précommande', 'woocommerce' ),
'description' => __( 'Disponible à la précommande', 'woocommerce' ),
'desc_tip' => true,
'value' => get_post_meta( $variation->ID, '_ab_preorder_checkbox', true )
)
);
// Custom Preorder Price
woocommerce_wp_text_input(
array(
'id' => '_ab_preorder_custom_price[' . $variation->ID . ']',
'label' => __( 'Prix à la précommande', 'woocommerce' ),
'placeholder' => '',
'desc_tip' => true,
'description' => __( "Prix à la précommande", 'woocommerce' ),
'type' => 'number',
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
),
'value' => get_post_meta( $variation->ID, '_ab_preorder_custom_price', true )
)
);
// Date de livraison estimée
woocommerce_wp_text_input(
array(
'id' => '_ab_preorder_estimated_date[' . $variation->ID . ']',
'label' => __( 'Date de livraison estimé', 'woocommerce' ),
'placeholder' => '24/09/2021',
'desc_tip' => true,
'description' => __( "Date de livraison estimé", "woocommerce" ),
'value' => get_post_meta( $variation->ID, '_ab_preorder_estimated_date', true )
)
);
echo '</div>';
}
add_action( 'woocommerce_product_after_variable_attributes', 'ab_preorder_variation_fields', 10, 3 ); // After all Variation fields
function ab_preorder_variation_fields_saving( $post_id ){
// Is Preordable
$woocommerce_text_field = $_POST['_ab_preorder_checkbox'][ $post_id ];
update_post_meta( $post_id, '_ab_preorder_checkbox', esc_attr( $woocommerce_text_field ) );
// Custom Preorder Price
$woocommerce_text_field = $_POST['_ab_preorder_custom_price'][ $post_id ];
update_post_meta( $post_id, '_ab_preorder_custom_price', esc_attr( $woocommerce_text_field ) );
// Date de livraison estimée
$woocommerce_text_field = $_POST['_ab_preorder_estimated_date'][ $post_id ];
update_post_meta( $post_id, '_ab_preorder_estimated_date', esc_attr( $woocommerce_text_field ) );
}
这就是我想要做的:
当客户选择变体时,我想:如果变体库存数量为 0 并且选中 _ab_preorder_checkbox 复选框,我想将产品价格设置为 100。
我尝试使用下面的代码,但它不起作用:/
function action_woocommerce_before_calculate_totals( $cart_object) {
$cart_items = $cart_object->cart_contents;
if ( ! empty( $cart_items ) ) {
$price = 100;
foreach ( $cart_items as $key => $value ) {
if($value['data']['_ab_preorder_checkbox']=="yes")
$value['data']->set_price( $price );
}
}
};
add_action( 'woocommerce_before_calculate_totals', 'action_woocommerce_before_calculate_totals', 10, 2 );
谁能帮我解决这个问题?
问候,
【问题讨论】:
标签: php wordpress woocommerce custom-fields price