【问题标题】:Hide, remove or disable the Add to Cart button in WooCommerce, if the product price is zero or 100% Discount如果产品价格为零或 100% 折扣,则隐藏、删除或禁用 WooCommerce 中的“添加到购物车”按钮
【发布时间】:2021-10-10 08:33:31
【问题描述】:

当产品价格为零或 100% 折扣时,我需要一个解决方案来隐藏产品页面中的“添加到购物车”按钮

我认为以下代码可能用于添加到购物车部分 此代码位于以下路径中 mytheme / woocommerce / 单一产品 / 添加到购物车 / simple.php

<?php

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

global $product;

if ( ! $product->is_purchasable() ) {
    return;
}

echo wc_get_stock_html( $product );

$prefix = '_studiare_';

$woo_studiare_btn_link = get_post_meta(get_the_id(), $prefix . 'woo_course_url', true);
$woo_studiare_btn_label = get_post_meta(get_the_id(), $prefix . 'woo_course_label', true);?>

<?php if ( ( empty( $woo_studiare_btn_label ) ) && ( empty( $woo_studiare_btn_link ) ) ) :

    if ( $product->is_in_stock() ) : ?>

        <?php do_action( 'woocommerce_before_add_to_cart_form' ); ?>

        <form class="cart" method="post" enctype='multipart/form-data'>
            <?php
            /**
             * @since 2.1.0.
             */
            do_action( 'woocommerce_before_add_to_cart_button' );

            /**
             * @since 3.0.0.
             */
            do_action( 'woocommerce_before_add_to_cart_quantity' );

            woocommerce_quantity_input( array(
                'min_value'   => apply_filters( 'woocommerce_quantity_input_min', 1, $product ),
                'max_value'   => apply_filters( 'woocommerce_quantity_input_max', $product->get_max_purchase_quantity(), $product ),
                'input_value' => isset( $_POST['quantity'] ) ? wc_stock_amount( $_POST['quantity'] ) : $product->get_min_purchase_quantity(),
            ) );

            /**
             * @since 3.0.0.
             */
            do_action( 'woocommerce_after_add_to_cart_quantity' );
            ?>

            <button type="submit" name="add-to-cart" value="<?php echo esc_attr( $product->get_id() ); ?>" class="single_add_to_cart_button button alt"><?php echo esc_html( $product->single_add_to_cart_text() ); ?></button>

            <?php
            /**
             * @since 2.1.0.
             */
            do_action( 'woocommerce_after_add_to_cart_button' );
            ?>
        </form>

        <?php do_action( 'woocommerce_after_add_to_cart_form' ); ?>

    <?php endif; ?>

<?php else: ?>
    <a href="<?php echo esc_url($woo_studiare_btn_link) ?>" class="single_add_to_cart_button single_add_to_cart_button_link button alt"><?php echo esc_attr($woo_studiare_btn_label); ?></a>
<?php endif; ?>

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    更改此代码:

    if ( $product->is_in_stock() ) : ?>
    

    到:

    if ( $product->is_in_stock() && $product->get_price() > 0 ) : ?>
    

    或在您的活动主题functions.php中添加以下代码并检查它。

    function wpcustom_is_purchasable( $purchasable, $product ){
        if( $product->get_price() == 0 )
            $purchasable = false;
        return $purchasable;
    }
    add_filter( 'woocommerce_is_purchasable', 'wpcustom_is_purchasable', 10, 2 );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-08
      • 2018-09-30
      • 2021-01-31
      • 1970-01-01
      • 2019-05-02
      • 1970-01-01
      • 2023-04-07
      • 2016-04-05
      相关资源
      最近更新 更多