代码之家  ›  专栏  ›  技术社区  ›  Rose Thorn

在Woocommerce Force销售旁添加价格

  •  3
  • Rose Thorn  · 技术社区  · 7 年前

    Woocommerce Force销售默认仅列出产品标题。我需要他们在每个标题旁边的括号中显示产品价格。你可以看到 here 如以下截图所示:

    enter image description here

    但它应该是:

    • 出生池租金债券(50.00美元)
    • 出生池衬垫(33.00美元)

    我是否可以使用过滤器,以便强制销售将价格放在商品旁边?除此之外,我如何修改Force-Sells代码,以便它也输出价格?

    这是Force Sales代码的一部分,用于在单个产品页面上输出“添加到购物车”框下的项目:

        /**
         * Displays information of what linked products that will get added when current
         * product is added to cart.
         */
        public function show_force_sell_products() {
            global $post;
    
            $product_ids = $this->get_force_sell_ids( $post->ID, array( 'normal', 'synced' ) );
            $titles      = array();
    
            // Make sure the products still exist and don't display duplicates.
            foreach( array_values( array_unique( $product_ids ) ) as $key => $product_id ) {
                $product = wc_get_product( $product_id );
    
                if ( $product && $product->exists() && 'trash' !== $product->get_status() ) {
                    $titles[] = version_compare( WC_VERSION, '3.0', '>=' ) ? $product->get_title() : get_the_title( $product_id );
                }
            }
    
            if ( ! empty( $titles ) ) {
                echo '<div class="clear"></div>';
                echo '<div class="wc-force-sells">';
                echo '<p>' . __( 'This will also add the following products to your cart:', 'woocommerce-force-sells' ) . '</p>';
                echo '<ul>';
    
                foreach ( $titles as $title ) {
                    echo '<li>' . $title . '</li>';
                }
    
                echo '</ul></div>';
            }
        }
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   LoicTheAztec    7 年前

    您可以尝试以下操作:

    add_filter( 'woocommerce_product_title', 'custom_product_title', 20, 2 );
    function custom_product_title( $name, $product ) {
        // Only in single product pages
        if( ! is_product() ) return $name;
    
        // The product name + the product formatted price
        return $name . ' (' . wc_price( wc_get_price_to_display( $product ) ) . ')';
    }
    

    代码进入功能。活动子主题(或活动主题)的php文件。它应该会起作用。