【问题标题】:How to change "add to cart" in WooCommerce?如何在 WooCommerce 中更改“添加到购物车”?
【发布时间】:2020-04-23 00:36:17
【问题描述】:

我想使用<ul> <li> 而不是<select> <option>
我在 function.php 中输入此代码但不起作用:

function wc_dropdown_variation_attribute_options( $args = array() ) {
.
.
.
    $html  = '<div class="tt-wrapper"><div class="tt-title-options">اندازه:</div><ul id="' . esc_attr( $id ) . '" class="tt-options-swatch options-large ' . esc_attr( $class ) . '" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '" data-show_option_none="' . ( $show_option_none ? 'yes' : 'no' ) . '">';

    if ( ! empty( $options ) ) {
        if ( $product && taxonomy_exists( $attribute ) ) {
            // Get terms if this is a taxonomy - ordered. We need the names too.
            $terms = wc_get_product_terms(
                $product->get_id(),
                $attribute,
                array(
                    'fields' => 'all',
                )
            );

            foreach ( $terms as $term ) {
                if ( in_array( $term->slug, $options, true ) ) {
                    $html .= '<li><a href="#" value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $args['selected'] ), $term->slug, false ) . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name, $term, $attribute, $product ) ) . '</a></li>';
                }
            }
        } else {
            foreach ( $options as $option ) {
                // This handles < 2.4.0 bw compatibility where text attributes were not sanitized.
                $selected = sanitize_title( $args['selected'] ) === $args['selected'] ? selected( $args['selected'], sanitize_title( $option ), false ) : selected( $args['selected'], $option, false );
                $html    .= '<li><a href="#" value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option, null, $attribute, $product ) ) . '</a></li>';
            }
        }
    }

    $html .= '</ul></div></div>';

    echo apply_filters( 'woocommerce_dropdown_variation_attribute_options_html', $html, $args ); // WPCS: XSS ok.
}

如何将 woocommerce HTML 代码更改为我自己的 HTML 代码?
这是发布此问题的更多详细信息

【问题讨论】:

    标签: php wordpress woocommerce wordpress-theming woocommerce-theming


    【解决方案1】:

    要覆盖 WooCommerce 的默认主题,您最好在主题文件夹中添加一个“woocommerce”子目录。 例如: /wp-内容/主题/mytheme/woocommerce/ 您可以在此处准确指定要更改的主题。 了解更多信息here

    此外,您还可以挂钩到操作和过滤器。在您发送的示例中,您可以连接到此过滤器:woocommerce_dropdown_variation_attribute_options_html 以更改其最终输出。 了解更多信息herehere

    【讨论】:

      猜你喜欢
      • 2016-09-15
      • 1970-01-01
      • 2014-09-04
      • 1970-01-01
      • 2018-10-10
      • 2021-04-21
      • 2018-08-29
      • 2015-08-18
      • 2014-02-25
      相关资源
      最近更新 更多