php - 当购物车值为零时删除 Woocommerce 结帐页面上的 Div

标签 php css wordpress woocommerce

我运营的网站上有很多免费产品,成员(member)加入后可以访问这些产品。他们必须再次通过结帐才能获得它们(它们是必须为每个用户生成的唯一凭证,因此需要再次通过结帐)。

当用户将这些产品之一添加到购物车并去结账时,我想显示一个 super 简单的结账页面。我真的只是想添加一个横幅图片,然后在图片正下方对齐“下订单”黄油中心。

我设法使用以下代码删除了大部分结帐字段:

function sv_free_checkout_fields() {

global $woocommerce ; 

// Bail we're not at checkout, or if we're at checkout but payment is needed
if ( ! is_checkout() || ( is_checkout() && WC()->cart->needs_payment() ) ) {
    return;
}

if ( $woocommerce->cart->total != 0 ) {
    return;
}

if ( WC_Subscriptions_Cart::cart_contains_subscription() ) {
    return;
}

// remove coupon forms since why would you want a coupon for a free cart??
remove_action( 'woocommerce_before_checkout_form', 'woocommerce_checkout_coupon_form', 10 );

// remove order review section
remove_action( 'woocommerce_checkout_order_review', 'woocommerce_order_review', 10 );

// Remove the "Additional Info" order notes
add_filter( 'woocommerce_enable_order_notes_field', '__return_false' );
// Unset the fields we don't want in a free checkout
function unset_unwanted_checkout_fields( $fields ) {

    // add or remove billing fields you do not want
    // list of fields: http://docs.woothemes.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/#section-2
    $billing_keys = array(
      'billing_first_name',
      'billing_last_name',  
      'billing_company',
        'billing_phone',
      'billing_email',
        'billing_address_1',
        'billing_address_2',
        'billing_city',
        'billing_postcode',
        'billing_country',
        'billing_state',
    );
    // unset each of those unwanted fields
    foreach( $billing_keys as $key ) {
        unset( $fields['billing'][$key] );
    }

    return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'unset_unwanted_checkout_fields' );

// A tiny CSS tweak for the account fields; this is optional
function print_custom_css() {
    echo '<style>.create-account { margin-top: 6em; }</style>';
}
add_action( 'wp_head', 'print_custom_css' );
}
add_action( 'wp', 'sv_free_checkout_fields' );

从现在开始,任何时候这些免费产品中的一个被添加到购物车并且用户去结账时,它会删除除“下订单”按钮(我想保留)之外的所有内容,以及该按钮周围的一些样式, 左侧的“帐单详细信息”标题,加上该标题周围的矩形边框,以及其正下方的另一个矩形框。我似乎无法弄清楚如何删除最后几件事。

这是我需要删除的代码,它创建了按钮周围的框:

body.woocommerce-cart .cart-collaterals, form.checkout.woocommerce-checkout 
#order_review {
width: 40%;
display: inline-block;
padding: 20px;
-webkit-border-radius: 2px;
-moz-border-radius: 2px;
border-radius: 2px;
border: 1px solid #f1f1f1;
background-color: #fdfdfd;
}

这是我需要在左侧删除的 div:

<div class="woocommerce-billing-fields">

    <h3>Billing details</h3>



    <div class="woocommerce-billing-fields__field-wrapper">
        </div>

    </div>

不过,我只需要在购物车总数为零时将它们移除。我还想将下订单按钮居中,并在可能的情况下在其上方添加横幅。

最佳答案

如何将上面概述的 div 替换为:

<?php
$cart_total = WC()->cart->get_cart_total();
if ( $cart_total != 0 ) {
?>
<div class="woocommerce-billing-fields">
<h3>Billing details</h3>
<div class="woocommerce-billing-fields__field-wrapper"></div>
</div>
<?php } ?>

这样,如果购物车值等于零,它显示。

我没有测试过,但理论是存在的。您可能需要将零总元素添加到购物车并回显 WC()->cart->get_cart_total(); 以检查输出。它可能会返回 0.00 或 $0.00。在这种情况下,您需要相应地修改代码。

关于php - 当购物车值为零时删除 Woocommerce 结帐页面上的 Div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46157905/

相关文章:

php - WooCommerce 按 id 从循环中删除产品

php - 在 PHP 中为我的类方法提供默认对象

php - 如何连接两个表以查看两个表中是否存在值

html - css 将背景图像垂直填充到 100%

jquery - 如何居中对齐灯箱并隐藏滚动条

jquery - WordPress TinyMCE 未提交内容

php - 如何将 php (5.5 >7.0) 项目转换为 exe 文件?

php - 在 MacOs Mojave 上找不到 XAMPP 的 httpd-xampp.conf 文件

javascript - 谷歌应用程序脚本 : Separating HTML and CSS

Javascript - 在滚动上添加类不起作用 WordPress