php 购物车按钮的woocommerce代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22679674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:10:53  来源:igfitidea点击:

woocommerce code for cart button

phpwordpresswoocommercecart

提问by helpanoobout

I need to add the woocommerce cart button to one of my pages and was wondering if someone could help with the code required to call the cart button. Here is the current code:

我需要将 woocommerce 购物车按钮添加到我的一个页面中,并且想知道是否有人可以帮助编写调用购物车按钮所需的代码。这是当前的代码:

<?php woocommerce_product_loop_start(); ?>

<?php woocommerce_product_subcategories(); ?>

<?php while ( have_posts() ) : the_post(); ?>
<div id="product-image1">
<a href="<?php echo esc_url( get_permalink( $product->id ) ); ?>" title="<?php echo esc_attr( $product->get_title() ); ?>">
  <?php echo $product->get_image(); ?>
</a>
</div>
<div id="product-description-container">
  <ul>
  <a href="<?php echo esc_url( get_permalink( $product->id ) ); ?>" title="<?php echo esc_attr( $product->get_title() ); ?>">
    <li><h4><?php echo $product->get_title(); ?></h4></li></a>
    <li><?php echo apply_filters( 'woocommerce_short_description', $post->post_excerpt )?></li>
    <li><h6><?php echo $product->get_price_html(); ?>  **MISSING CODE TO ADD TO CART BUTTON HERE**</h6></li>
 </ul>
</div>
<?php endwhile; // end of the loop. ?>

回答by Aksh?y Paghdar

I think may be you need following code.

我认为您可能需要以下代码。

Add this code to your place = (**MISSING CODE TO ADD TO CART BUTTON HERE**).

将此代码添加到您的位置 = ( **MISSING CODE TO ADD TO CART BUTTON HERE**)。

global $product;

echo apply_filters( 'woocommerce_loop_add_to_cart_link',
    sprintf( '<a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" class="button %s product_type_%s">%s</a>',
        esc_url( $product->add_to_cart_url() ),
        esc_attr( $product->get_id() ),
        esc_attr( $product->get_sku() ),
        $product->is_purchasable() ? 'add_to_cart_button' : '',
        esc_attr( $product->get_type() ),
        esc_html( $product->add_to_cart_text() )
    ),
$product );

Hope this will help.

希望这会有所帮助。