【问题标题】:Woocommerce custom add to cart loopWoocommerce 自定义添加到购物车循环
【发布时间】:2017-10-14 03:22:31
【问题描述】:

我正在使用 Woocommerce 产品插件插件来允许访问者在简单产品上选择多个选项。由于产品很简单,Woocommerce 在产品页面视图上显示一个添加到购物车按钮,而不是在产品详细信息页面上的选择选项按钮链接,访问者可以在其中选择选项。

我希望创建一个功能,该功能将按 ID 在产品上显示选择选项按钮。

这是我的起始代码。

add_filter( 'woocommerce_loop_add_to_cart_link', 'change_product_button' );
    function change_product_button() {
    global $product;
    $id = $product->id;
    if ($id == 91){
        echo '<a href="'.$product->get_permalink( $product->id ).'" class="button">' . __('View Product', 'woocommerce') . '</a>';
    } else {
      // display usual add to cart button
    }
}

或者也许我们可以通过 id 强制产品为不可购买来模拟可变产品。

【问题讨论】:

  • 只是为了确认:您想在列出产品的页面上显示“查看产品”或“选择选项”而不是“添加到购物车”对吗?喜欢在类别页面或商店页面上吗?
  • 这绝对是我要找的

标签: wordpress woocommerce


【解决方案1】:

这是一个可行的解决方案

add_filter( 'woocommerce_loop_add_to_cart_link', 'change_product_button', 10, 2 );
function change_product_button($html, $product) {
    $values = array(190, 91);
    $id = $product->id;
    if(in_array($id, $values)){
        $html= '<a href="'.$product->get_permalink( $product->id ).'" class="button">' . __('Choose an option', 'woocommerce') . '</a>';
    } else {
        $html = '<form action="' . esc_url( $product->add_to_cart_url() ) . '" class="cart" method="post" enctype="multipart/form-data">';
        $html .= woocommerce_quantity_input( array(), $product, false );
        $html .= '<button type="submit" class="button alt">' . esc_html( $product->add_to_cart_text() ) . '</button>';
        $html .= '</form>';
    }
    return $html;
}

【讨论】:

  • 恭喜您回答了您自己的问题并找到了一个好的过滤器来实现您想要的 +1
  • 感谢@jantea-alexandru,但我确信使用 is_purchasable 有更好的解决方案,即说数组中的所有 ID 都不是 is_purchasable 并且 woocommerce 会影响自己的好按钮
猜你喜欢
  • 2018-05-18
  • 2015-11-28
  • 1970-01-01
  • 1970-01-01
  • 2016-10-21
  • 2015-12-10
  • 2015-12-04
  • 2021-04-21
  • 2021-01-21
相关资源
最近更新 更多