如何使用WooCommerce将添加到购物车按钮添加到自定义HREF链接中


how to add add-to-cart button into custom href link using woocommerce

我正在使用Woo-Commerce产品页面的自定义模板,在这里我显示所有产品名称和产品奖品。 现在我想添加带有产品奖品链接的WooCommerce添加到购物车选项。有没有办法做到这一点?感谢您的帮助。

<?php
    $args = array( 'post_type' => 'product', 
                    'posts_per_page' => 5, 
                    'post_status' =>'any',
                    'order' => 'ASC'
                ); 
                 $allProduct = get_posts( $args );
                 if ( $allProduct ) {
                 foreach ( $allProduct as $products ) { ?>
<ul>
    <li class="product_name"><a href=""><?php  echo apply_filters( 'the_title' , $products->post_title ); ?></li>
    <li class="product_prize"><a href="ADD-TO-CART-URL"><span class="pdfIconSmall">&nbsp;</span></i>Purchase PDF - 
     <?php $price = get_post_meta( $products->ID, '_regular_price', true);
        echo "$ ",$price;
         ?></a>
    </li>
</ul>
<?php 
  }
   } ?>

对于动态添加到购物车,您需要 AJAX,但如果您只想显示购物车(包含小部件),您可以添加以下内容:

<?php if( in_array('woocommerce/woocommerce.php', get_option('active_plugins')) ):?>
    <div id="shop_links" class="cart_right"><?php _e('Cart:','your_theme_slug') ?>
        <a class="link_cart" href="<?php echo esc_url($woocommerce->cart->get_cart_url()); ?>">
            <span>
            <?php
                echo '<span class="items_count">' . $woocommerce->cart->cart_contents_count . '</span> ' ._n('item', 'items',  $woocommerce->cart->cart_contents_count ,'your_theme_slug') . ' ' . '&mdash; ' . $woocommerce->cart->get_cart_total() ;
            ?>
            </span>
            <i class="icon-shopping-cart"></i>
        </a>
        <div class="cart_dropdown_widget">
            <?php the_widget('WC_Widget_Cart'); ?>
        </div>
    </div>
<?php endif; ?>

使用一些用于转换的 js:

var menu_cart = $('#shop_links'), subelement = menu_cart.find('.cart_dropdown_widget').css({display:'none', opacity: 0});
menu_cart.hover(
    function(){
        subelement.css({display:'block'}).stop().animate({opacity:1});
    },
    function(){
        subelement.stop().animate({opacity:0}, function(){
            subelement.css({display:'none'});
        });
    }
);

造型由您决定。

希望这有帮助

<?php echo get_site_url()."?add-to-cart=".esc_attr($loop->post->ID); ?>