admin管理员组文章数量:1122832
I want to add Assembly options at the checkout page like this
Anyone knows how to do it without plugin? Thanks
add_action('woocommerce_after_order_notes', 'custom_checkout_fields');
function custom_checkout_fields($checkout) {
echo '<div id="custom_checkout_fields"><h2>' . __('Additional Services') . '</h2>';
woocommerce_form_field('assembly_option', array(
'type' => 'select',
'class' => array('form-row-wide'),
'label' => __('Assembly Service'),
'required' => false,
'options' => array(
'' => __('None'),
'individual_item' => __('Assembly for an Individual Item (+$200)'),
'set' => __('Assembly for a Set (+$600)'),
)
), $checkout->get_value('assembly_option'));
woocommerce_form_field('trash_removal_option', array(
'type' => 'select',
'class' => array('form-row-wide'),
'label' => __('Trash Removal Service'),
'required' => false,
'options' => array(
'' => __('None'),
'individual_item' => __('Trash Removal for an Individual Item (+$50)'),
'set' => __('Trash Removal for a Set (+$150)'),
)
), $checkout->get_value('trash_removal_option'));
echo '</div>';
}
add_action('woocommerce_checkout_create_order', 'custom_checkout_field_update_order_meta');
function custom_checkout_field_update_order_meta($order) {
if (!empty($_POST['assembly_option'])) {
$order->update_meta_data('assembly_option', sanitize_text_field($_POST['assembly_option']));
}
if (!empty($_POST['trash_removal_option'])) {
$order->update_meta_data('trash_removal_option', sanitize_text_field($_POST['trash_removal_option']));
}
}
add_action('woocommerce_cart_calculate_fees', 'add_assembly_trash_removal_cost');
function add_assembly_trash_removal_cost() {
if (is_admin() && !defined('DOING_AJAX')) return;
$assembly_option = WC()->session->get('chosen_assembly_option');
$trash_removal_option = WC()->session->get('chosen_trash_removal_option');
if ($assembly_option) {
switch ($assembly_option) {
case 'individual_item':
$assembly_cost = 200;
break;
case 'set':
$assembly_cost = 600;
break;
default:
$assembly_cost = 0;
}
WC()->cart->add_fee(__('Assembly Service', 'text-domain'), $assembly_cost);
}
if ($trash_removal_option) {
switch ($trash_removal_option) {
case 'individual_item':
$trash_removal_cost = 50;
break;
case 'set':
$trash_removal_cost = 150;
break;
default:
$trash_removal_cost = 0;
}
WC()->cart->add_fee(__('Trash Removal Service', 'text-domain'), $trash_removal_cost);
}
}
add_action('woocommerce_checkout_update_order_review', 'update_session_with_selected_options');
function update_session_with_selected_options($posted_data) {
if (isset($posted_data['assembly_option'])) {
WC()->session->set('chosen_assembly_option', $posted_data['assembly_option']);
}
if (isset($posted_data['trash_removal_option'])) {
WC()->session->set('chosen_trash_removal_option', $posted_data['trash_removal_option']);
}
}
本文标签: phpAssembly Options at checkout
版权声明:本文标题:php - Assembly Options at checkout 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736300132a1930709.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论