第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

在 WooCommerce 中選擇產(chǎn)品重量后計(jì)算并顯示新價(jià)格

在 WooCommerce 中選擇產(chǎn)品重量后計(jì)算并顯示新價(jià)格

PHP
MMTTMM 2022-11-12 13:47:59
在 WooCommerce 中,我使用一個(gè)代碼來顯示牛排重量選擇表單,保存選擇數(shù)據(jù)并在購(gòu)物車、結(jié)帳頁(yè)面、編輯訂單時(shí)和電子郵件通知中顯示這些數(shù)據(jù)。// Display Custom Checkbox Fieldadd_action('woocommerce_product_options_general_product_data', 'steak_custom_field_add');function steak_custom_field_add(){    global $post;    // Checkbox    woocommerce_wp_checkbox(        array(            'id' => '_steak_checkbox',            'label' => __('Steak Weight', 'woocommerce' ),            'description' => __( 'If necessary, enable steak weight selection', 'woocommerce' )        )    );}// Save Custom Checkbox Fieldadd_action('woocommerce_process_product_meta', 'steak_custom_field_save');function steak_custom_field_save($post_id){    // Custom Product Checkbox Field    $steak_checkbox = isset( $_POST['_steak_checkbox'] ) ? 'yes' : 'no';    update_post_meta($post_id, '_steak_checkbox', esc_attr( $steak_checkbox ));}// Display Custom Select Boxadd_action( 'woocommerce_before_add_to_cart_button', 'display_steak_custom_field', 0 );function display_steak_custom_field() {    global $product;    // If is single product page and have the "steak_checkbox" enabled we display the field    if ( $product->get_meta( '_steak_checkbox' ) === 'yes' ) {        echo '<div class="steak_select_box">';        $select = woocommerce_form_field( 'steak_custom_options', array(            'type'          => 'select',            'class'         => array('my-steak-select-box form-row-wide'),            'label'         => __('Steak Weight'),            'required'      => false,            'return'       => false,            'options'   => array(            )        ), '' );        echo $select;        echo '</div>';    }}但不幸的是,我不知道如何解決一個(gè)問題......本產(chǎn)品價(jià)格以每100克計(jì)算。最小起訂量為 300 克。而我需要的是,在選擇產(chǎn)品的重量時(shí),應(yīng)該據(jù)此計(jì)算該產(chǎn)品的最終成本。
查看完整描述

1 回答

?
HUX布斯

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超6個(gè)贊

要調(diào)整產(chǎn)品價(jià)格,在購(gòu)物車頁(yè)面等。根據(jù)從下拉列表中選擇的重量,添加以下代碼


function my_before_calculate_totals( $cart ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )

        return;


    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )

        return;


    // Loop through cart items

    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {     

        if( isset( $cart_item['steak_option'] ) ) {


            // Remove the last 2 zeros (100g becomes 1, 300g becomes 3, 1000g becomes 10, etc...)

            // Remove 'g' from grams

            // convert string to integer

            $chosen_weight = (int) str_replace( '00', '', str_replace('g', '', $cart_item['steak_option']) );


            // Get current price

            $current_price = $cart_item['data']->get_price();


            // Set new price, price is already known per 100g

            $cart_item['data']->set_price( $current_price * $chosen_weight );

        }

    }

}

add_action( 'woocommerce_before_calculate_totals', 'my_before_calculate_totals', 10, 1 );

要更改單個(gè)產(chǎn)品頁(yè)面上的價(jià)格,請(qǐng)根據(jù)下拉菜單添加此


function add_footer_steak_script() {

    global $woocommerce, $product;

    ?>

    <script type="text/javascript">

        jQuery(document).ready(function ($) {

            console.log('JS works!');


            var price = <?php echo $product->get_price(); ?>, currency = '<?php echo get_woocommerce_currency_symbol(); ?>';


            $( '[name=steak_custom_options]' ).change(function(){

                if (!(this.value < 1)) {

                    var dropdown_val = this.value;

                    var remove_g = dropdown_val.replace( 'g', '' );

                    var remove_double_zero = remove_g.replace( '00', '' );


                    var product_total = parseFloat( price * remove_double_zero );


                    $( '.woocommerce-Price-amount' ).html( currency + product_total.toFixed(2));


                }

            });

        });

    </script>

    <?php

}

add_action('wp_footer','add_footer_steak_script');


查看完整回答
反對(duì) 回復(fù) 2022-11-12
  • 1 回答
  • 0 關(guān)注
  • 121 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)