【问题标题】:Update cart-totals after checking a checkbox (woocommerce - wordpress)选中复选框后更新购物车总数(woocommerce - wordpress)
【发布时间】:2016-09-20 14:26:36
【问题描述】:

我对 wordpress 有点陌生,想得到一些建议。我尝试在卡片中添加一个可选字段,用于对卡片总数进行四舍五入。我希望它在不检查时不会四舍五入卡的总数。所以这是我的 current situation 我使用了这个代码:

@functios.php

function woo_add_cart_fee() {

    global $woocommerce;

    $totalprice = floatval( preg_replace( '#[^\d.]#', '', $woocommerce->cart->get_cart_total() ) );
    $rounded = ceil($totalprice);
    $donation = $rounded-$totalprice;
    $woocommerce->cart->add_fee( __('Donatie', 'woocommerce'),$donation);

}
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );

还有这个@cart-totals.php

<?php foreach ( WC()->cart->get_fees() as $fee ) : ?>
    <tr class="fee">
        <th><input type="checkbox"><?php echo esc_html( $fee->name ); ?></th>
        <td data-title="<?php echo esc_html( $fee->name ); ?>"><?php wc_cart_totals_fee_html( $fee ); ?></td>
    </tr>
<?php endforeach; ?>

那么谁能告诉我在哪里可以添加一些仅在选中捐赠选项时将我的购物车总数四舍五入的功能?

【问题讨论】:

标签: php wordpress checkbox woocommerce checked


【解决方案1】:

实际上,我并不完全了解您想要做什么。但是听起来需要在您的选项中包含这句话以检查它是否被选中。

<input type="checkbox" name="your-option-name" value="1" <?php checked(1, get_option('your-option-name'), true); ?> />

你可以找到Function Reference/checked的定义。

之后,添加 if case 用于检查是否被检查。如下所示

// 1 is the value of the checkbox that you defined.
if(get_option("your-option-name") == 1){
    <?php foreach ( WC()->cart->get_fees() as $fee ) : ?>
        <tr class="fee">
            <th><input type="checkbox"><?php echo esc_html( $fee->name ); ?></th>
            <td data-title="<?php echo esc_html( $fee->name ); ?>"><?php wc_cart_totals_fee_html( $fee ); ?></td>
        </tr>
    <?php endforeach; ?>
}

这是我之前建立选项的链接。 Building Your Own Social Sharing Plugin for WordPress

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2016-10-31
    • 1970-01-01
    • 2021-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-08
    • 1970-01-01
    • 2018-07-29
    相关资源
    最近更新 更多