Woocommerce 添加到购物车 link Ajax 在其他 post 或页面中启用

Woocommerce Add to cart link Ajax enabled in other post or pages

在 Woocommerce 中,我想在 WordPress 网站 (不是产品页面) 的一个简单页面上共同创建添加到购物车 link。

所以我试过这个代码(其中123是产品ID):

<a href="http://example.com/cart/?add-to-cart=123">Buy</a>

我已启用 AJAX 在存档页面 Woocommerce 选项设置中添加到购物车。

但它不起作用,并且 Ajax 功能未在我的自定义添加到购物车上启用 link。

如何在自定义 link (除 woocommerce 页面之外的其他页面)上启用 ajax 添加到购物车?

我建议您使用 [add_to_cart] 简码。

使用默认参数的最简单用法:

[add_to_cart id="123" /]

禁用默认样式并隐藏价格:

[add_to_cart id="123" style="" show_price="false" /]

查看一些示例输出

要在自定义添加到购物车按钮中启用 ajax,至少有 3 种方法:

  1. 一个简单的 HTML Ajax 添加到购物车 link:

    <a rel="nofollow" href="/?add-to-cart=37" data-quantity="1" data-product_id="123" data-product_sku="" class="add_to_cart_button ajax_add_to_cart">Add to cart</a>
    
  2. 使用 Woocommerce 现有的 [add_to_cart id='123'] 短代码 (与上述用法相同)

  3. 最可定制:不包含价格的自定义短代码 (可定制的按钮文本,额外的类,数量和许多其他可能性)

    if( ! function_exists('custom_ajax_add_to_cart_button') && class_exists('WooCommerce') ) {
        function custom_ajax_add_to_cart_button( $atts ) {
            // Shortcode attributes
            $atts = shortcode_atts( array(
                'id' => '0', // Product ID
                'qty' => '1', // Product quantity
                'text' => '', // Text of the button
                'class' => '', // Additional classes
            ), $atts, 'ajax_add_to_cart' );
    
            if( esc_attr( $atts['id'] ) == 0 ) return; // Exit when no Product ID
    
            if( get_post_type( esc_attr( $atts['id'] ) ) != 'product' ) return; // Exit if not a Product
    
            $product = wc_get_product( esc_attr( $atts['id'] ) );
    
            if ( ! $product ) return; // Exit when if not a valid Product
    
            $classes = implode( ' ', array_filter( array(
                'button',
                'product_type_' . $product->get_type(),
                $product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
                $product->supports( 'ajax_add_to_cart' ) ? 'ajax_add_to_cart' : '',
            ) ) ).' '.$atts['class'];
    
            $add_to_cart_button = sprintf( '<a rel="nofollow" href="%s" %s %s %s class="%s">%s</a>',
                esc_url( $product->add_to_cart_url() ),
                'data-quantity="' . esc_attr( $atts['qty'] ) .'"',
                'data-product_id="' . esc_attr( $atts['id'] ) .'"',
                'data-product_sku="' . esc_attr( $product->get_sku() ) .'"',
                esc_attr( isset( $classes ) ? $classes : 'button' ),
                esc_html( empty( esc_attr( $atts['text'] ) ) ? $product->add_to_cart_text() : esc_attr( $atts['text'] ) )
            );
    
            return $add_to_cart_button;
        }
        add_shortcode('ajax_add_to_cart', 'custom_ajax_add_to_cart_button');
    }
    

    此代码在您的活动子主题(或主题)的 function.php 文件中。 已测试并有效。

    用法:

    在帖子和页面文本编辑器中:

    [ajax_add_to_cart id='123' text='Buy']
    

    在 PHP 个文件或模板中:

    echo do_shortcode( "[ajax_add_to_cart id='123' text='Buy']" );
    

    在 php 页面上的 HTML 代码中插入:

    <?php echo do_shortcode( "[ajax_add_to_cart id='123' text='Buy']" ); ?>
    

    Hide Ajax "view cart"

    For custom shortcode, to hide the Ajax "view cart" behavior, you can add in the code, before return $add_to_cart_button; the following:

    $style = '<style>a.added_to_cart.wc-forward { display:none !important; }</style>';
    
    $add_to_cart_button = $style . $add_to_cart_button ;
    

我将它用于我的插件并且工作正常:

function wsb_add_to_cart_button( ) {
    global $product;

    $classes = implode( ' ',  array(
        'button',
        'product_type_' . $product->get_type(),
        $product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
        $product->supports( 'ajax_add_to_cart' ) ? 'ajax_add_to_cart' : '',
    )  );

    return apply_filters( 'woocommerce_loop_add_to_cart_link',
        sprintf( '<a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" data-quantity="%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() ),
            esc_attr( isset( $quantity ) ? $quantity : 1 ),
            esc_attr( isset( $classes ) ? $classes : 'button' ),
            esc_attr( $product->get_type() ),
            esc_html( $product->add_to_cart_text() )
        ),
    $product );
} 

用于显示按钮:

<?php echo wsb_add_to_cart_button(); ?>