【问题标题】:WooCommerce: Move price and subtotal under product name in cartWooCommerce:在购物车中的产品名称下移动价格和小计
【发布时间】:2020-06-09 10:56:19
【问题描述】:

我想在产品名称的表格单元格中显示购物车项目的价格和小计。原因是,我需要对手机有另一种看法。对于桌面视口,价格和小计的表格单元格应保留。所以我想添加一个额外的移动版本并将其隐藏在桌面上。

我找到了钩子woocommerce_after_cart_item_name,我知道如何在产品名称下方添加内容。

这是我尝试过的代码:(目前不成功)

    add_filter('woocommerce_after_cart_item_name','copy_cart_product_price',10,3);
function copy_cart_product_price($cart_item, $cart_item_key){

    $cart_price_subtotal = sprintf(
        '<p class="cart-price">6,90&euro;</p>',
        '<p class="cart-subtotal">13,80&euro;</p>'
    );

    $cart_item .= $cart_price_subtotal;
    return $cart_item;

}

我的问题是,我不知道如何获得价格。

我想我必须使用这两个过滤器:

价格:

echo apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $_product ), $cart_item, $cart_item_key ); // PHPCS: XSS ok.

小计:

echo apply_filters( 'woocommerce_cart_item_subtotal', WC()->cart->get_product_subtotal( $_product, $cart_item['quantity'] ), $cart_item, $cart_item_key ); // PHPCS: XSS ok.

但是如何在我的 sn-p 中显示/使用这些过滤器?

如果购物车中的一件商品有 2 件或更多件,有没有办法只显示小计?

【问题讨论】:

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


    【解决方案1】:

    我相信你是这个意思?代码中添加注释的解释

    function action_woocommerce_after_cart_item_name( $cart_item, $cart_item_key ) {
        // Price
        $price = $cart_item['data']->get_price();
    
        // Line subtotal
        $subtotal = $cart_item['line_subtotal'];
    
        // Quantity
        $quantity = $cart_item['quantity'];
    
        echo '<p class="cart-price">' . wc_price( $price )  . '</p>';
    
        if ( $quantity >= 2 ) {
            echo '<p class="cart-subtotal">' . wc_price( $subtotal ) . '</p>';
        }
    }
    add_action( 'woocommerce_after_cart_item_name', 'action_woocommerce_after_cart_item_name', 10, 2 );
    

    【讨论】:

      猜你喜欢
      • 2021-11-08
      • 2017-04-06
      • 1970-01-01
      • 2017-08-07
      • 1970-01-01
      • 1970-01-01
      • 2018-06-03
      • 2018-02-17
      • 2017-04-28
      相关资源
      最近更新 更多