显示WooCommerce“添加到购物车”用短号[add_to_cart]动态的按钮

发布于 2025-01-28 16:24:25 字数 224 浏览 3 评论 0原文

我在WordPress网站上的小部件侧边栏中使用WooCommerce短号[add_to_cart]在产品页面上显示“添加到购物车”按钮(想要将短代码放在自定义字段中)。我了解如何使用产品“ ID”在特定页面上显示按钮(例如:[ADD_TO_CART ID =“ 1874”]),但是当它自动获得当前产品页面的ID时,我想这样做(动态)并显示与每个产品页面特定产品相关的“添加到购物车”按钮。有人可以建议如何做吗?

谢谢

I use WooCommerce short-code [add_to_cart ] inside a widget sidebar On WordPress website to display “Add to cart” button on product pages (wanted to put the short-code to Custom Field). I understand how to display the button on a specific page using a product “Id” (for example: [add_to_cart id="1874" ]), but I wanted to make it that way when it gets Id of a current product page automatically (dynamically) and display “Add to cart” button related to a specific product for each product page. Can someone advise how to do it, please?

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北方的韩爷 2025-02-04 16:24:26

将以下nipet放在您的函数中
首先,我们需要扩展WP_Widget类

// Adds widget: Add to cart button
class AddToCartButton_Widget extends WP_Widget {

    // Register widget with WordPress
    function __construct() {
        parent::__construct(
                    'addtocartbutton_widget',
                    esc_html__( 'Add to cart button', 'text-domain' ),
                    array( 'description' => esc_html__( 'Woocommerce add to cart button widget', 'text-domain' ), ) // Args
                );
    }

    // Frontend display of widget
    public function widget( $args, $instance ) {
        if(!is_product()) return; // Show button only if its product page
        echo $args['before_widget'];
        $post_id = get_queried_object_id(); // Get current product ID
        echo do_shortcode('[add_to_cart id="'.$post_id.'"]'); //Dynamicly generate button with correct ID
        echo $args['after_widget'];
    }
}

,然后我们需要注册我们的小部件

function register_AddToCartButton_widget() {
    register_widget( 'AddToCartButton_Widget' );
}
add_action( 'widgets_init', 'register_AddToCartButton_widget' );

Place the following snipet in your functions.php
First we need to extend WP_Widget class

// Adds widget: Add to cart button
class AddToCartButton_Widget extends WP_Widget {

    // Register widget with WordPress
    function __construct() {
        parent::__construct(
                    'addtocartbutton_widget',
                    esc_html__( 'Add to cart button', 'text-domain' ),
                    array( 'description' => esc_html__( 'Woocommerce add to cart button widget', 'text-domain' ), ) // Args
                );
    }

    // Frontend display of widget
    public function widget( $args, $instance ) {
        if(!is_product()) return; // Show button only if its product page
        echo $args['before_widget'];
        $post_id = get_queried_object_id(); // Get current product ID
        echo do_shortcode('[add_to_cart id="'.$post_id.'"]'); //Dynamicly generate button with correct ID
        echo $args['after_widget'];
    }
}

After that we need to register our widget

function register_AddToCartButton_widget() {
    register_widget( 'AddToCartButton_Widget' );
}
add_action( 'widgets_init', 'register_AddToCartButton_widget' );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文