简体   繁体   English

Woocommerce:访问页面时自动添加到购物车(可变产品ID)

[英]Woocommerce: add to cart automatically when visiting a page (variable product IDs)

I am aware that there's already an existing function where you can add a certain product on cart upon visiting the website. 我知道,已经有一个现有功能,您可以在访问网站时在购物车中添加某些产品。 The problem is you can only do it with 1 product and I want it variable depending on the page you are visiting. 问题是您只能用1种产品来做,我希望它随您访问的页面而变化。

here's the existing code (added on functions.php file) 这是现有代码(添加到functions.php文件中)

add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
    if ( ! is_admin() ) {
        $product_id = 275;
        $found = false;
        //check if product already in cart
        if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
            foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
                $_product = $values['data'];
                if ( $_product->id == $product_id )
                    $found = true;
            }
            // if product not found, add it
            if ( ! $found )
                WC()->cart->add_to_cart( $product_id );
        } else {
            // if no products in cart, add it
            WC()->cart->add_to_cart( $product_id );
        }
    }
}

Inside my landing page called page-template.php, there's a custom field that echos the ID of a certain product selected. 在我的名为page-template.php的着陆页中,有一个自定义字段,该字段会回显所选特定产品的ID。 So when my user visits this page, I want this ID to replace the $product_id on the functions.php file. 因此,当我的用户访问此页面时,我希望该ID替换functions.php文件中的$ product_id。 How do I achieve this? 我该如何实现?

Many thanks in advance for the help. 预先非常感谢您的帮助。

maybe the global variable is loaded after the function. 也许全局变量是在函数之后加载的。 instead of just setting the variable initiate the function passing the variable and avoid the hook: 不仅仅是设置变量,而是通过传递变量并避免钩子来启动函数:

in functions.php: 在functions.php中:

function add_product_to_cart($product_id) {
    if ( ! is_admin() ) {
        $found = false;
        //check if product already in cart
        if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
            foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
                $_product = $values['data'];
                if ( $_product->id == $product_id )
                    $found = true;
            }
            // if product not found, add it
            if ( ! $found )
                WC()->cart->add_to_cart( $product_id );
        } else {
            // if no products in cart, add it
            WC()->cart->add_to_cart( $product_id );
        }
    }
}

in page-template.php: 在page-template.php中:

add_product_to_cart(275);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WooCommerce单品页面自动加入购物车 - Automatically add product to cart when visiting single product page in WooCommerce Woocommerce-在页面加载时,将产品添加到购物车并自动进行结帐 - Woocommerce - On page load, add product to cart and proceed to Checkout automatically 在 WooCommerce 中将产品添加到购物车时自动添加包装 - Automatically add packaging when adding product to cart in WooCommerce WooCommerce AJAX放入购物车以获取可变产品,仍重定向到产品页面 - WooCommerce AJAX Add to Cart for Variable products, still redirecting to product page 自动添加或删除 Woocommerce 购物车中的免费产品,但购物车上没有订阅产品 - Add or remove automatically a free product in Woocommerce cart but not with subscription product on the cart 在 Woocommerce 购物车中自动添加或删除免费产品 - Add or remove automatically a free product in Woocommerce cart 隐藏在Woocommerce中变量产品缺货时添加到购物车块 - Hide Add to cart block when a variable product is out of stock in Woocommerce 如果产品在购物车中,则更改“添加到购物车”按钮文本,并在 WooCommerce 中满足条件时重定向到购物车页面 - Change "add to cart" button text if product is in cart and redirect to cart page when condition is met in WooCommerce Woocommerce - 移动添加到购物车按钮变量产品 - Woocommerce - Move add to cart button variable product Woocommerce将购物车风格添加为产品页面 - Woocommerce Add to cart style as product page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM