【问题标题】:Remove Div on Woocommerce Checkout Page When Cart Value is Zero当购物车价值为零时删除 Woocommerce 结帐页面上的 Div
【发布时间】:2017-09-11 14:18:28
【问题描述】:

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

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

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

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' );

所以到目前为止,只要将这些免费产品之一添加到购物车并且用户去结帐,它就会删除除“下订单”按钮(我想保留)之外的所有内容,以及围绕该按钮的一些样式, 左侧的“Billing Details”标题,以及围绕该标题的矩形边框,以及其正下方的另一个矩形框。我似乎无法弄清楚如何删除最后几件事。

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

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>

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

【问题讨论】:

    标签: php css wordpress woocommerce


    【解决方案1】:

    如何将上面列出的 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()-&gt;cart-&gt;get_cart_total(); 以检查输出。它可能返回 0.00 或 0.00 美元。在这种情况下,您需要相应地修改代码。

    【讨论】:

    • 直接使用更好if ( ! WC()-&gt;cart-&gt;is_empty() ) { /* … */ }
    • 抱歉,我是新手。我会把你概述的那个字符串放在哪里?到目前为止,我使用“代码片段”插件在 sn-p 中上传了我在初始消息中概述的第一部分代码。
    • @LoicTheAztec 唯一的问题是,正如 CCHert 所概述的,购物车中可以有物品,但仍然为零。如果总数为零而不是购物车为空,他想阻止显示。
    • 看起来代码包含在 woocommerce/templates/checkout/ form-b​​illing.php 中。但是,如果您直接对此文件进行更改,则在更新插件时它将被覆盖。所以我想看看如何通过你的主题override the plugin templates
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-24
    • 1970-01-01
    • 2020-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    相关资源
    最近更新 更多