【问题标题】:how to show add to cart button for specific products in woocommerce?如何在woocommerce中显示特定产品的添加到购物车按钮?
【发布时间】:2021-07-13 07:49:33
【问题描述】:

我正在使用一个名为“一键聊天订购”的插件通过 whatsapp 订购产品,并且我在所有产品的单个产品页面上隐藏了添加到购物车按钮。只有当产品重量小于 50 公斤时,我才会显示添加到购物车按钮。

sample output

需要在此处的报价按钮后显示添加到购物车按钮。 我已经使用这个 sn-p 根据我的情况隐藏添加到购物车按钮。但它也隐藏了whatsapp报价按钮。不知道为什么?

add_action( 'woocommerce_single_product_summary', 'action_single_product_summary_callback', 1 );
function action_single_product_summary_callback() {
global $product;

$weight = $product->get_weight();
preg_replace('/\D/', '', $weight);

if ( $weight > 50 ) {

    // For variable product types
    if( $product->is_type( 'variable' ) ) {
        remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
        
    }
    // For all other product types
    else {
        remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
        
    }
}

我是 woocommerce 新手。请帮助解决这个问题。

【问题讨论】:

  • 你的产品类型是variable吗?
  • @Bhautik 是的,它的变量。
  • 在下面查看我的答案。

标签: wordpress woocommerce


【解决方案1】:

您正在删除woocommerce_single_product_summary。以及您使用的插件one click chat to order 使用woocommerce_after_add_to_cart_buttonwoocommerce_after_add_to_cart_form 来显示whatsapp quote button

这些是他们用来显示whatsapp quote button的条件

if ( get_option('wa_order_single_product_button_position') == 'after_atc') {
    add_action( 'woocommerce_after_add_to_cart_button', 'wa_order_add_button_plugin', 5 );
} elseif ( get_option('wa_order_single_product_button_position') == 'under_atc') {
    add_action( 'woocommerce_after_add_to_cart_form', 'wa_order_add_button_plugin', 5 );
} elseif (get_option('wa_order_single_product_button_position') == 'after_shortdesc') {
    add_action( 'woocommerce_before_add_to_cart_form', 'wa_order_add_button_plugin', 5 );
} else {
    add_action( 'woocommerce_after_add_to_cart_button', 'wa_order_add_button_plugin', 5 );
}

您可以通过他们的设置显示whatsapp quote button 按钮。有三种显示方式。默认是 After Add to Cart Button (Default).,您已被 woocommerce_single_product_summary 删除,因此您可以使用 Under Add to Cart Button. 选项。

After Add to Cart Button (Default).
Under Add to Cart Button.
After Short Description.

或者你可以通过 CSS 隐藏。

add_action( 'woocommerce_single_product_summary', 'action_single_product_summary_callback', 1 );
function action_single_product_summary_callback() {
    global $product;

    $weight = $product->get_weight();
    preg_replace('/\D/', '', $weight);

    if ( $weight > 50 ) {
        ?>
        <style type="text/css">
            .single_add_to_cart_button{display: none;}
        </style>
        <?php
    }
}

经过测试且有效

【讨论】:

  • 谢谢您,如果产品重量小于 50 则工作正常。如果重量大于此值,则隐藏两个按钮。如您所说,我已在“添加到购物车”选项下进行了选择,我也尝试在简短描述后进行选择。没有任何作用。 :(
  • 欢迎...很高兴为您提供帮助。如果这个答案对你有帮助,那么你可以accept 答案,如果你喜欢/想要你也可以upvote 答案,谢谢。
猜你喜欢
  • 2018-08-11
  • 2021-12-29
  • 2020-09-01
  • 1970-01-01
  • 2023-04-10
  • 2017-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多