如何为Woocommerce自定义购物车和结帐页面?

时间:2020-06-30 23:11:34

标签: wordpress woocommerce

我注意到,当我添加到具有变化的购物车产品时,Woocommerce在购物车或结帐页面中使用2个模板。 如果该产品只有一个或两个版本,则使用此模板作为我的产品的示例:

<tr class="cart_item">
                    <td class="product-name">
                        3-in-1 Rechargeable Trimmer Grinder - CHINA, 3 IN 1 Grooming N5&nbsp;                        
                        <strong class="product-quantity">×&nbsp;2</strong>                                          
                    </td>

</tr>

如果产品具有两个以上的版本,则使用以下内容:

<tr class="cart_item">
    <td class="product-name">
          Mini Shaver Hair Trimmer Rechargeable&nbsp;                        
          <strong class="product-quantity">×&nbsp;1</strong>                        
        <dl class="variation">
            <dt class="variation-Size">Size:</dt>
                <dd class="variation-Size"><p>Standard</p> </dd>
            <dt class="variation-ShipsFrom">Ships From:</dt>
                <dd class="variation-ShipsFrom"><p>CHINA</p> </dd>
            <dt class="variation-Color">Color:</dt>
                <dd class="variation-Color"><p>White</p> </dd>
        </dl>
    </td> 
</tr>

如您所见,当有两个以上时,它将使用类变体。 我的问题是如何修改Woocommerce以使其仅使用第二个模板(两个以上的变体),以便在我要自定义CSS或javascript时简化代码。

更多详细信息: review-order.php:

<tbody>
    <?php
    do_action( 'woocommerce_review_order_before_cart_contents' );

    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        $_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );

        if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_checkout_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
            ?>
            <tr class="<?php echo esc_attr( apply_filters( 'woocommerce_cart_item_class', 'cart_item', $cart_item, $cart_item_key ) ); ?>">
                <td class="product-name">
                    <?php echo apply_filters( 'woocommerce_cart_item_name', $_product->get_name(), $cart_item, $cart_item_key ) . '&nbsp;'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
                    <?php echo apply_filters( 'woocommerce_checkout_cart_item_quantity', ' <strong class="product-quantity">' . sprintf( '&times;&nbsp;%s', $cart_item['quantity'] ) . '</strong>', $cart_item, $cart_item_key ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
                    <?php echo wc_get_formatted_cart_item_data( $cart_item ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
                </td>
                <td class="product-total">
                    <?php echo apply_filters( 'woocommerce_cart_item_subtotal', WC()->cart->get_product_subtotal( $_product, $cart_item['quantity'] ), $cart_item, $cart_item_key ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
                </td>
            </tr>
            <?php
        }
    }

    do_action( 'woocommerce_review_order_after_cart_contents' );
    ?>
</tbody>

cart-item-data.php:

    <?php
/**
 * Cart item data (when outputting non-flat)
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/cart/cart-item-data.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see         https://docs.woocommerce.com/document/template-structure/
 * @package     WooCommerce/Templates
 * @version     2.4.0
 */
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}
?>
<dl class="variation">
        <?php foreach ( $item_data as $data ) : ?>
            <dt class="<?php echo sanitize_html_class( 'variation-' . $data['key'] ); ?>"><?php echo wp_kses_post( $data['key'] ); ?>:</dt>
            <dd class="<?php echo sanitize_html_class( 'variation-' . $data['key'] ); ?>"><?php echo wp_kses_post( wpautop( $data['display'] ) ); ?></dd>
        <?php endforeach; ?>
    </dl>

cart.php:

<tbody>
    <?php do_action( 'woocommerce_before_cart_contents' ); ?>

    <?php
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        $_product   = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
        $product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );

        if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
            $product_permalink = apply_filters( 'woocommerce_cart_item_permalink', $_product->is_visible() ? $_product->get_permalink( $cart_item ) : '', $cart_item, $cart_item_key );
            ?>
            <tr class="woocommerce-cart-form__cart-item <?php echo esc_attr( apply_filters( 'woocommerce_cart_item_class', 'cart_item', $cart_item, $cart_item_key ) ); ?>">

                <td class="product-remove">
                    <?php
                        echo apply_filters( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                            'woocommerce_cart_item_remove_link',
                            sprintf(
                                '<a href="%s" class="remove" aria-label="%s" data-product_id="%s" data-product_sku="%s">&times;</a>',
                                esc_url( wc_get_cart_remove_url( $cart_item_key ) ),
                                esc_html__( 'Remove this item', 'woocommerce' ),
                                esc_attr( $product_id ),
                                esc_attr( $_product->get_sku() )
                            ),
                            $cart_item_key
                        );
                    ?>
                </td>

                <td class="product-thumbnail">
                <?php
                $thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $_product->get_image(), $cart_item, $cart_item_key );

                if ( ! $product_permalink ) {
                    echo $thumbnail; // PHPCS: XSS ok.
                } else {
                    printf( '<a href="%s">%s</a>', esc_url( $product_permalink ), $thumbnail ); // PHPCS: XSS ok.
                }
                ?>
                </td>

                <td class="product-name" data-title="<?php esc_attr_e( 'Product', 'woocommerce' ); ?>">
                <?php
                if ( ! $product_permalink ) {
                    echo wp_kses_post( apply_filters( 'woocommerce_cart_item_name', $_product->get_name(), $cart_item, $cart_item_key ) . '&nbsp;' );
                } else {
                    echo wp_kses_post( apply_filters( 'woocommerce_cart_item_name', sprintf( '<a href="%s">%s</a>', esc_url( $product_permalink ), $_product->get_name() ), $cart_item, $cart_item_key ) );
                }

                do_action( 'woocommerce_after_cart_item_name', $cart_item, $cart_item_key );

                // Meta data.
                echo wc_get_formatted_cart_item_data( $cart_item ); // PHPCS: XSS ok.

                // Backorder notification.
                if ( $_product->backorders_require_notification() && $_product->is_on_backorder( $cart_item['quantity'] ) ) {
                    echo wp_kses_post( apply_filters( 'woocommerce_cart_item_backorder_notification', '<p class="backorder_notification">' . esc_html__( 'Available on backorder', 'woocommerce' ) . '</p>', $product_id ) );
                }
                ?>
                </td>

                <td class="product-price" data-title="<?php esc_attr_e( 'Price', 'woocommerce' ); ?>">
                    <?php
                        echo apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $_product ), $cart_item, $cart_item_key ); // PHPCS: XSS ok.
                    ?>
                </td>

                <td class="product-quantity" data-title="<?php esc_attr_e( 'Quantity', 'woocommerce' ); ?>">
                <?php
                if ( $_product->is_sold_individually() ) {
                    $product_quantity = sprintf( '1 <input type="hidden" name="cart[%s][qty]" value="1" />', $cart_item_key );
                } else {
                    $product_quantity = woocommerce_quantity_input(
                        array(
                            'input_name'   => "cart[{$cart_item_key}][qty]",
                            'input_value'  => $cart_item['quantity'],
                            'max_value'    => $_product->get_max_purchase_quantity(),
                            'min_value'    => '0',
                            'product_name' => $_product->get_name(),
                        ),
                        $_product,
                        false
                    );
                }

                echo apply_filters( 'woocommerce_cart_item_quantity', $product_quantity, $cart_item_key, $cart_item ); // PHPCS: XSS ok.
                ?>
                </td>

                <td class="product-subtotal" data-title="<?php esc_attr_e( 'Subtotal', 'woocommerce' ); ?>">
                    <?php
                        echo apply_filters( 'woocommerce_cart_item_subtotal', WC()->cart->get_product_subtotal( $_product, $cart_item['quantity'] ), $cart_item, $cart_item_key ); // PHPCS: XSS ok.
                    ?>
                </td>
            </tr>
            <?php
        }
    }
    ?>

    <?php do_action( 'woocommerce_cart_contents' ); ?>

    <tr>
        <td colspan="6" class="actions">

            <?php if ( wc_coupons_enabled() ) { ?>
                <div class="coupon">
                    <label for="coupon_code"><?php esc_html_e( 'Coupon:', 'woocommerce' ); ?></label> <input type="text" name="coupon_code" class="input-text" id="coupon_code" value="" placeholder="<?php esc_attr_e( 'Coupon code', 'woocommerce' ); ?>" /> <button type="submit" class="button" name="apply_coupon" value="<?php esc_attr_e( 'Apply coupon', 'woocommerce' ); ?>"><?php esc_attr_e( 'Apply coupon', 'woocommerce' ); ?></button>
                    <?php do_action( 'woocommerce_cart_coupon' ); ?>
                </div>
            <?php } ?>

            <button type="submit" class="button" name="update_cart" value="<?php esc_attr_e( 'Update cart', 'woocommerce' ); ?>"><?php esc_html_e( 'Update cart', 'woocommerce' ); ?></button>

            <?php do_action( 'woocommerce_cart_actions' ); ?>

            <?php wp_nonce_field( 'woocommerce-cart', 'woocommerce-cart-nonce' ); ?>
        </td>
    </tr>

    <?php do_action( 'woocommerce_after_cart_contents' ); ?>
</tbody>

wc-product-functions.php:

/**
 * Variation Formatting.
 *
 * Gets a formatted version of variation data or item meta.
 *
 * @param array|WC_Product_Variation $variation Variation object.
 * @param bool                       $flat Should this be a flat list or HTML list? (default: false).
 * @param bool                       $include_names include attribute names/labels in the list.
 * @param bool                       $skip_attributes_in_name Do not list attributes already part of the variation name.
 * @return string
 */
function wc_get_formatted_variation( $variation, $flat = false, $include_names = true, $skip_attributes_in_name = false ) {
    $return = '';

    if ( is_a( $variation, 'WC_Product_Variation' ) ) {
        $variation_attributes = $variation->get_attributes();
        $product              = $variation;
        $variation_name       = $variation->get_name();
    } else {
        $product        = false;
        $variation_name = '';
        // Remove attribute_ prefix from names.
        $variation_attributes = array();
        if ( is_array( $variation ) ) {
            foreach ( $variation as $key => $value ) {
                $variation_attributes[ str_replace( 'attribute_', '', $key ) ] = $value;
            }
        }
    }

    $list_type = $include_names ? 'dl' : 'ul';

    if ( is_array( $variation_attributes ) ) {

        if ( ! $flat ) {
            $return = '<' . $list_type . ' class="variation">';
        }

        $variation_list = array();

        foreach ( $variation_attributes as $name => $value ) {
            // If this is a term slug, get the term's nice name.
            if ( taxonomy_exists( $name ) ) {
                $term = get_term_by( 'slug', $value, $name );
                if ( ! is_wp_error( $term ) && ! empty( $term->name ) ) {
                    $value = $term->name;
                }
            }

            // Do not list attributes already part of the variation name.
            if ( '' === $value || ( $skip_attributes_in_name && wc_is_attribute_in_product_name( $value, $variation_name ) ) ) {
                continue;
            }

            if ( $include_names ) {
                if ( $flat ) {
                    $variation_list[] = wc_attribute_label( $name, $product ) . ': ' . rawurldecode( $value );
                } else {
                    $variation_list[] = '<dt>' . wc_attribute_label( $name, $product ) . ':</dt><dd>' . rawurldecode( $value ) . '</dd>';
                }
            } else {
                if ( $flat ) {
                    $variation_list[] = rawurldecode( $value );
                } else {
                    $variation_list[] = '<li>' . rawurldecode( $value ) . '</li>';
                }
            }
        }

        if ( $flat ) {
            $return .= implode( ', ', $variation_list );
        } else {
            $return .= implode( '', $variation_list );
        }

        if ( ! $flat ) {
            $return .= '</' . $list_type . '>';
        }
    }
    return $return;
}

class-wc-order-item-meta.php:

/**
 * Display meta in a formatted list.
 *
 * @param bool   $flat       Flat (default: false).
 * @param bool   $return     Return (default: false).
 * @param string $hideprefix Hide prefix (default: _).
 * @param  string $delimiter Delimiter used to separate items when $flat is true.
 * @return string|void
 */
public function display( $flat = false, $return = false, $hideprefix = '_', $delimiter = ", \n" ) {
    $output         = '';
    $formatted_meta = $this->get_formatted( $hideprefix );

    if ( ! empty( $formatted_meta ) ) {
        $meta_list = array();

        foreach ( $formatted_meta as $meta ) {
            if ( $flat ) {
                $meta_list[] = wp_kses_post( $meta['label'] . ': ' . $meta['value'] );
            } else {
                $meta_list[] = '
                    <dt class="variation-' . sanitize_html_class( sanitize_text_field( $meta['key'] ) ) . '">' . wp_kses_post( $meta['label'] ) . ':</dt>
                    <dd class="variation-' . sanitize_html_class( sanitize_text_field( $meta['key'] ) ) . '">' . wp_kses_post( wpautop( make_clickable( $meta['value'] ) ) ) . '</dd>
                ';
            }
        }

        if ( ! empty( $meta_list ) ) {
            if ( $flat ) {
                $output .= implode( $delimiter, $meta_list );
            } else {
                $output .= '<dl class="variation">' . implode( '', $meta_list ) . '</dl>';
            }
        }
    }

    $output = apply_filters( 'woocommerce_order_items_meta_display', $output, $this, $flat );

    if ( $return ) {
        return $output;
    } else {
        echo $output; // WPCS: XSS ok.
    }
}

0 个答案:

没有答案