简体   繁体   English

WooCommerce 缺货时的“联系”按钮

[英]WooCommerce “CONTACT” button when out of stock

Trying to achieve something that should be simple, but I've tried 3 approaches with multiple code variations and I just can't make it work.试图实现一些应该很简单的东西,但我已经尝试了 3 种具有多种代码变体的方法,但我无法使其正常工作。 I'm trying to create a button that will appear in place of the "ADD TO CART" button on single product pages when the item is out of stock.我正在尝试创建一个按钮,当商品缺货时,该按钮将代替单个产品页面上的“添加到购物车”按钮。 Clicking the button will fire a popup contact form.单击该按钮将触发一个弹出式联系表单。

Is creating an add action in functions the right way to go, or should I replace the normal button with an if statement?在函数中创建添加操作是正确的方法,还是应该用 if 语句替换普通按钮? I've tried both, so help with coding either would be greatly appreciated.我已经尝试了两者,因此非常感谢您对编码的帮助。

You can either hook into woocommerce_loop_add_to_cart_args using a filter in your functions.php or edit the template file directly by pulling it into your theme.您可以挂接到woocommerce_loop_add_to_cart_args使用过滤器在你functions.php直接或编辑该模板文件由拉到你的主题。 Either way will require a bit of PHP.无论哪种方式都需要一点 PHP。

If doing it in your functions.php , it would look something like this (untested but should send you down the right path):如果在你的functions.php这样做,它看起来像这样(未经测试但应该让你走正确的道路):

<?php
add_filter( 'woocommerce_loop_add_to_cart_link', 'my_out_of_stock_button' );

function my_out_of_stock_button( $args ){
  global $product;
  if( $product && !$product->is_in_stock() ){
    return '<a href="' . home_url( 'contact' ) . '">Contact us</a>';
  }
  return $args;
}

I don't know what your button code should actually look like or what other information you need to capture, but this is how you could override the "Add to Cart" button and replace it if out of stock.我不知道您的按钮代码实际上应该是什么样子,或者您需要捕获哪些其他信息,但这是您可以覆盖“添加到购物车”按钮并在缺货时更换它的方法。

UPDATE更新

LoicTheAztec brought up a great point - the filter provided only affects the button on the archive, category, tag overview pages - not the individual product pages. LoicTheAztec 提出了一个很好的观点——提供的过滤器只影响存档、类别、标签概览页面上的按钮——而不是单个产品页面。 There are no hooks for the individual product page buttons BUT you can copy the templates to your theme and override them .单个产品页面按钮没有挂钩,但您可以将模板复制到您的主题并覆盖它们

You'll want to look at the files in templates/single-product/add-to-cart .您需要查看templates/single-product/add-to-cart Use a similar if statement as above:使用与上面类似的 if 语句:

#simple.php
<?php if ( $product->is_in_stock() ) : ?>
  // Standard WooCommerce code
<?php else: ?>
  // Your button code
<?php endif; ?>

Just add below code in functions.php file of your enabled theme reference只需在启用的主题参考的functions.php文件中添加以下代码

add_action('woocommerce_after_shop_loop_item', 'themelocation_change_outofstock_to_contact_us', 1);
// for shop page 
function themelocation_change_outofstock_to_contact_us() {
  global $product;
  if (!$product->is_in_stock()) {
    remove_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart');
    remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart');
    //change the link to your contact us page
    echo '<a href="/contact"> Contact Us </a>';
  }
}
// for single page
add_filter('woocommerce_get_availability', 'wcs_custom_get_availability', 1, 2);
function wcs_custom_get_availability($availability, $_product) {
  // Change In Stock Text
  if ($_product->is_in_stock()) {
    $availability['availability'] = __('Available!', 'woocommerce');
  }
  // Change Out of Stock Text
  if (!$_product->is_in_stock()) {
    $availability['availability'] = __('<a href="/contact"> Contact Us </a>', 'woocommerce');
  }
  return $availability;
}

I was looking for a way to show a contact button on bespoke products and @ahwychkchih solution works great.我一直在寻找一种在定制产品上显示联系按钮的方法,@ahwychkchih 解决方案效果很好。 One issue I had though is that schema markup will show as out of stock for those products which is not the case for beskpoke products is just they can't be purchased straight away so I've added this to force in_stock markup for my products.但我遇到的一个问题是,架构标记将显示为那些产品缺货,而定制产品并非如此,只是它们无法立即购买,所以我添加了它以强制为我的产品添加 in_stock 标记。 I'm aware that this solution would affect all products so you can always add a product id filter if needed我知道此解决方案会影响所有产品,因此您可以随时根据需要添加产品 ID 过滤器

// Force In Stock schema markup
function fix_my_product_offers_schema ($markup_offer, $product) {
  if (!$product->is_in_stock()) {
        $markup_offer['availability'] = 'https://schema.org/InStock';
  }
  return $markup_offer;
}
add_filter('woocommerce_structured_data_product_offer', 'fix_my_product_offers_schema', 1, 2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 产品缺货时删除按钮 Elementor PRO/Woocommerce - Remove button when product out of stock Elementor PRO/Woocommerce 用 WooCommerce 中的按钮替换缺货文本 - Replace Out of stock text with a button in WooCommerce WooCommerce 产品在实际没有缺货时显示“缺货”消息 - WooCommerce products showing “Out of stock” message when not actually out of stock 如果 WooCommerce 中的所有变体都缺货,则显示已售罄的灰色按钮 - Display a Sold out greyed button if all variations are out of stock in WooCommerce 如果 Woocommerce 商店和档案缺货,请删除产品按钮 - Remove product button if out of stock from Woocommerce shop and archives 如何在woocommerce中为缺货产品启用“添加到购物车”按钮? - How to enable add to cart button for out of stock product in woocommerce? 更改缺货 Woocommerce 产品上的“阅读更多”按钮链接 - Change “Read More” button link on Out of stock Woocommerce products WooCommerce 积木:更改缺货产品上的阅读更多按钮 - WooCommerce Blocks: Change Read More Button on Out of Stock Products 当库存设置为“ 0”或“无库存”时,WooCommerce隐藏价格 - WooCommerce Hide Prices When Inventory is set to '0' or 'out of stock' 在 WooCommerce 中选择的变体缺货时显示一个表格 - Display a form when the selected variation is out of stock in WooCommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM