【问题标题】:How to align 'price' and 'add to cart' button in the same row at Shop Page of Woocommerce?如何在 Woocommerce 商店页面的同一行中对齐“价格”和“添加到购物车”按钮?
【发布时间】:2019-04-15 21:24:18
【问题描述】:

我想知道如何在 woocommerce 的 SHOP PAGE 的同一行中对齐“价格”和“添加到购物车”按钮。

【问题讨论】:

  • 转到父元素并使其显示:flex;然后转到两个孩子的并将两个元素的宽度设置为 50%。
  • 有没有办法使用我可以插入到主题定制器的附加 CSS 部分的确切 css 代码来制作它?因为我不知道如何去父母和孩子。
  • 我目前不在我的电脑上。您可以使用开发人员工具检查 DOM 以查看布局。也许你有一个链接到你的网站给我?所以我直接在你的页面上验证它
  • 我在当地环境工作 Jo. (主题:Woocommerce 的 Storefront 主题)而且我没有编码知识,我只是尝试找到该代码并插入主题的 Additional CSS 部分。
  • 乔先生;我刚刚在下面看到你的帖子。我现在正在寻找。请给我时间来处理。

标签: css wordpress woocommerce


【解决方案1】:

您可以在附加的 css 中添加以下 css

.products span.price{
    float:left;
    margin: 10px 0px;
}
.products a.button.add_to_cart_button{
    float: right;
}

我已经检查过了,它正在工作。您可以查看此屏幕截图

【讨论】:

  • 在响应式布局中使用浮动不是很好,但使用 CSS 是最好的解决方案。如果要使用flex,则需要更改模板。
  • 在店面商店页面;这个 CSS 代码运行良好。但是,如果您在产品上有“销售”徽章,则“销售”徽章会将“添加到购物车”按钮向下推,看起来很奇怪。 link。我希望你能在链接中看到图片。
  • 乔先生;我将主题从 Storefront 更改为 Astra,但是这个 CSS 代码根本不起作用。梅尔文先生,然后我再次尝试使用 Strorefront,但这次使用的是平板电脑视图/布局;而且看起来也不太好。仅在没有销售徽章的桌面视图 + 电话视图中看起来不错。
  • 我可以删除或更改“销售”徽章的位置,但是如何让这个 CSS 代码在平板电脑视图中看起来不错? (在桌面和手机视图中;代码运行良好)
  • 我赞成梅尔文先生。但仍显示 1 个数字。但我赞成先生。
【解决方案2】:

我们开始吧。我假设您在安装中创建了一个woocommerce 文件夹。现在转到plugins > woocommerce > templates 并将文件content-product.php 直接复制到您的子主题的woocommerce 文件夹中。经过上述步骤后,结构需要如下所示:

- themes
  - Storefront
  - YourChildTheme
    - functions.php
    - woocommerce <- you need create this folder
        - content-product.php

完成后,您需要打开复制的文件。用这个内容替换文件的内容并保存文件:

<?php
/**
 * The template for displaying product content within loops
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/content-product.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 3.4.0
 */

defined( 'ABSPATH' ) || exit;

global $product;

// Ensure visibility.
if ( empty( $product ) || ! $product->is_visible() ) {
    return;
}
?>
<li <?php wc_product_class(); ?>>
    <?php
    /**
     * Hook: woocommerce_before_shop_loop_item.
     *
     * @hooked woocommerce_template_loop_product_link_open - 10
     */
    do_action( 'woocommerce_before_shop_loop_item' );

    /**
     * Hook: woocommerce_before_shop_loop_item_title.
     *
     * @hooked woocommerce_show_product_loop_sale_flash - 10
     * @hooked woocommerce_template_loop_product_thumbnail - 10
     */
    do_action( 'woocommerce_before_shop_loop_item_title' );

    /**
     * Hook: woocommerce_shop_loop_item_title.
     *
     * @hooked woocommerce_template_loop_product_title - 10
     */
    do_action( 'woocommerce_shop_loop_item_title' );

    /**
     * Hook: woocommerce_after_shop_loop_item_title.
     *
     * @hooked woocommerce_template_loop_rating - 5
     * @hooked woocommerce_template_loop_price - 10
     */
    ?>
    <div class="shop-loop-item-details-wrapper">
        <?php 
        do_action( 'woocommerce_after_shop_loop_item_title' );
        do_action( 'woocommerce_after_shop_loop_item' );
        ?>
    </div>
    <?php

    /**
     * Hook: woocommerce_after_shop_loop_item.
     *
     * @hooked woocommerce_template_loop_product_link_close - 5
     * @hooked woocommerce_template_loop_add_to_cart - 10
     */
    //do_action( 'woocommerce_after_shop_loop_item' );
    ?>
</li>

之后,打开 Customizer 并转到 Additional CSS 部分。将这里粘贴到字段中并发布页面:

.shop-loop-item-details-wrapper {
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-orient: horizontal;
    -webkit-box-direction: normal;
        -ms-flex-direction: row;
            flex-direction: row;
    -ms-flex-pack: distribute;
        justify-content: space-around;
    -webkit-box-align: center;
        -ms-flex-align: center;
            align-items: center;
    padding-top: 8px;
    -ms-flex-wrap: wrap;
        flex-wrap: wrap;
}

.shop-loop-item-details-wrapper .woocommerce-loop-product__link {
    margin-right: 8px;
    -webkit-box-flex: 1;
        -ms-flex: 1;
            flex: 1;
}

.shop-loop-item-details-wrapper .woocommerce-loop-product__link .price {
    font-size: 22px;
    margin-bottom: 0 !important;
    text-align: left;
}

.shop-loop-item-details-wrapper .add_to_cart_button {
    margin-bottom: 0;
}

关键是,这并没有针对手机进行优化,因为我会在那里使用列布局,并且不会在价格下方显示 添加到购物车 按钮。

当您将浏览器调整为较小的分辨率时,您会看到价格在按钮上方的某个位置,这看起来不太好。

这里的问题是,正如我上面已经说过的,价格是一个动态值,具有动态长度。因此,您应该考虑将部分代码包装到@media 标记中,以便在分辨率为 x 的 2-col 布局中使用。

在这种情况下,您需要检查何时需要使用它。不知道你们店里有什么价格。因此,继续输入您拥有的最大价格并调整浏览器的大小。当项目越过按钮时,您就知道需要在哪里使用 2-col 布局。因此,例如当浏览器宽度为980px 时,如果宽度大于980px,则需要以这种方式修改附加CSS 以仅附加1-col 布局:

@media (min-width: 981px) {
    .shop-loop-item-details-wrapper {
        -webkit-box-orient: horizontal;
        -webkit-box-direction: normal;
        -ms-flex-direction: row;
        flex-direction: row;
    }

    .shop-loop-item-details-wrapper .woocommerce-loop-product__link {
        margin-right: 8px;
    }
}

@media (max-width: 980px) {
    .shop-loop-item-details-wrapper {
        -webkit-box-orient: vertical;
        -webkit-box-direction: normal;
            -ms-flex-direction: column;
                flex-direction: column;
    }

    .shop-loop-item-details-wrapper .woocommerce-loop-product__link .price {
        margin-bottom: 8px !important;
    }
}

.shop-loop-item-details-wrapper {
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -ms-flex-pack: distribute;
    justify-content: space-around;
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;
    padding-top: 8px;
    -ms-flex-wrap: wrap;
    flex-wrap: wrap;
}

.shop-loop-item-details-wrapper .woocommerce-loop-product__link {
    -webkit-box-flex: 1;
    -ms-flex: 1;
    flex: 1;
}

.shop-loop-item-details-wrapper .woocommerce-loop-product__link .price {
    font-size: 22px;
    margin-bottom: 0 !important;
    text-align: left;
}

.shop-loop-item-details-wrapper .add_to_cart_button {
    margin-bottom: 0;
}

希望它适合你。我已经对其进行了测试,并且可以确认它可以正常工作:

如果可行,那就太好了,如果您将此答案标记为解决方案并打勾。

【讨论】:

  • 乔先生。我将第一个代码添加到子主题的“functions.php”文件中(没有发生任何事情),然后我将第二个 css 代码添加到子主题的“style.css”文件中(没有发生任何事情)。 "" -ms-flex-pack:分发; “”这个代码是给的!标记。我哪里做错了???
  • 你读过我的答案了吗(将文件 content-product.php 直接复制到你的子主题的 woocommerce 文件夹中。)? -ms-flex-pack 适用于新版本不支持的旧 IE 浏览器,这就是它显示标记的原因。你可以忽略这个标记。
  • 阅读本文以了解我们的工作(我不知道我应该向您解释清楚多少):zemez.io/wordpress/support/how-to/…(取 content-product.php 而不是在教程)
  • 乔先生;我跟着你的步骤。但它没有用。这是图片链接。 link
  • 如果您按照我的步骤进行操作,它应该看起来像我的照片。您是否从第一个答案中删除了代码?
猜你喜欢
  • 2021-12-29
  • 2018-01-28
  • 2023-02-23
  • 1970-01-01
  • 2018-05-12
  • 2018-09-05
  • 2017-04-20
  • 2016-10-24
  • 1970-01-01
相关资源
最近更新 更多