当前位置: 首页 > 知识库问答 >
问题:

禁用特定WooCommerce产品的“添加到购物车”按钮

云洋
2023-03-14
add_action( 'woocommerce_product_options_general_product_data', 'custom_general_product_data_custom_fields' );
/**
 * Add `Call to Order` field in the Product data's General tab.
 */
function custom_general_product_data_custom_fields() {
    // Checkbox.
    woocommerce_wp_checkbox(
        array(
            'id'            => '_not_ready_to_sell',
            'wrapper_class' => 'show_if_simple',
            'label'         => __( 'Call to Order', 'woocommerce' ),
            'description'   => __( '', 'woocommerce' )
            )
    );
}

add_action( 'woocommerce_process_product_meta', 'custom_save_general_proddata_custom_fields' );
/**
 * Save the data values from the custom fields.
 * @param  int $post_id ID of the current product.
 */
function custom_save_general_proddata_custom_fields( $post_id ) {
    // Checkbox.
    $woocommerce_checkbox = isset( $_POST['_not_ready_to_sell'] ) ? 'yes' : 'no';
    update_post_meta( $post_id, '_not_ready_to_sell', $woocommerce_checkbox );
}

add_filter( 'woocommerce_is_purchasable', 'custom_woocommerce_set_purchasable', 10, 2);
/**
 * Mark "Not ready to sell" products as not purchasable.
 */
function custom_woocommerce_set_purchasable() {
    $not_ready_to_sell = get_post_meta( get_the_ID(), '_not_ready_to_sell' , true);

    return ( 'yes' == $not_ready_to_sell ? false : true );

}

add_filter( 'woocommerce_product_add_to_cart_text', 'custom_product_add_to_cart_text' );
/**
 * Change "Read More" button text for non-purchasable products.
 */
function custom_product_add_to_cart_text() {
    $not_ready_to_sell = get_post_meta( get_the_ID(), '_not_ready_to_sell', true );

    if ( 'yes' === $not_ready_to_sell ) {
        return __( 'Call to Order', 'woocommerce' );
    } else {
        return __( 'Add to Cart', 'woocommerce' );
    }
}

勾选了复选框的产品实际上是不可购买的,这是期望的结果。

我遇到的问题是,当我在产品目录页面上为可购买的产品(那些没有勾选的)点击“添加到购物车”时,我被重定向到产品页面,并且默认的WooCommerce消息“对不起,这个产品不能被购买。”出现。应该发生的是,当单击“添加到购物车”按钮时,产品会自动添加到购物车中。

同样从单品页面,我可以毫无问题地添加可购推车。

共有1个答案

赫连棋
2023-03-14

我已经测试了你的代码,它没有问题…我没有你描述的有问题的行为…所以是别的东西在制造麻烦:

您首先需要做一个数据库备份…然后您应该尝试:

  1. 检查在其他自定义中是否存在禁用Ajax add to cart并使该消息出现的内容。尝试注释您的其他自定义项,以找出有问题的项。
  2. 尝试禁用所有与Woocommerce相关的第三方插件(Woocommerce除外)。如果问题消失了,请逐一重新启用它们,以找到有罪的人。

问题也可能来自主题。

现在自从Woocommerce 3并引入CRUD对象之后,您的代码就有点过时了。

以下是重新修订和增强的代码版本(适用于Woocommerce 3+):

// Add a custom field in the Product data's General tab (for simple products).
add_action( 'woocommerce_product_options_general_product_data', 'add_general_product_data_custom_field' );
function add_general_product_data_custom_field() {
    woocommerce_wp_checkbox( array( // Checkbox.
        'id'            => '_not_ready_to_sell',
        'label'         => __( 'Call to Order', 'woocommerce' ),
        'wrapper_class' => 'show_if_simple',
    ) );
}

// Save custom field value
add_action( 'woocommerce_admin_process_product_object', 'save_general_product_data_custom_field', 10, 1 );
function save_general_product_data_custom_field( $product ) {
    $product->update_meta_data( '_not_ready_to_sell', isset( $_POST['_not_ready_to_sell'] ) ? 'yes' : 'no' );
}

// Make not purchasable, products with '_not_ready_to_sell' meta data set to "yes" (for simple products)
add_filter( 'woocommerce_is_purchasable', 'filter_woocommerce_set_purchasable', 10, 2);
function filter_woocommerce_set_purchasable( $purchasable, $product ) {
    return 'yes' === $product->get_meta( '_not_ready_to_sell' ) && $product->is_type('simple') ? false : $purchasable;

}

// Change button text to "Call to Order" for simple products not purchasable.
add_filter( 'woocommerce_product_add_to_cart_text', 'filter_product_add_to_cart_text', 10, 2 );
function filter_product_add_to_cart_text( $button_text, $product ) {
    if ( 'yes' === $product->get_meta( '_not_ready_to_sell' ) && $product->is_type('simple') ) {
        $button_text =  __( 'Call to Order', 'woocommerce' );
    }
    return $button_text;
}

代码将在您的活动子主题(或活动主题)的function.php文件上运行。可能有用的。

 类似资料:
  • 我正在尝试将我的WooCommerce档案页面上特定产品类别的add to cart按钮更改为“Read More”按钮,该按钮将链接到产品页面(但不链接到单个产品页面本身)。 使用“将添加到购物车按钮替换为WooCommerce 3中的shop pages中的read more链接到product页面”的答案代码,如何将其限制为仅有一个产品类别(本例中术语名称为“Classs”)? 感谢任何帮助

  • 我经营一家Woocommerce商店,该商店也提供免费产品(_常规价格=0)。客户必须选择数量并将其添加到购物车中,然后下订单才能收到产品。但这并不是Woocommerce的工作原理,它隐藏了所有价格为0的产品的“添加到购物车”链接。并且不会在购物车页面中显示它们。有没有解决这个问题的办法?非常感谢。

  • 我怎样才能使它像预期的那样工作呢? 下面是它在这个链接上的实际工作方式。

  • 我正在使用一个名为“一键聊天订购”的插件通过whatsapp订购产品,并且我在所有产品的单一产品页面上隐藏了“添加到购物车”按钮。只有当产品重量小于50kg时,我才能显示“添加到购物车”按钮。 样本输出 需要在报价按钮后显示添加到购物车按钮。我已经使用这个片段来隐藏基于我的条件的添加到购物车按钮。但它也隐藏了WhatsApp报价按钮。我不知道为什么? 我是WooCommerce的新手。请帮助解决这

  • 我使用“从Woocommerce中的functions.php文件中添加到购物车按钮上显示价格”的答案代码,在简单的产品中添加价格。 但我想改进这个功能,动态显示选择的变异价格内按钮。在变体产品页面上使用此代码,它只显示最低的价格。有什么解决办法吗?