如何正确初始化Woocommerce购物车 [英] How to properly initialize Woocommerce Cart

查看:89
本文介绍了如何正确初始化Woocommerce购物车的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义模板文件,呈现了一些产品及其尝试添加到添加到购物车中的按钮,这些都是我试图手动实现的。按下添加到购物车后,页面将重新加载,并且包含某些数据的$ _POST变量将添加新产品。 cart_contents_count也反映了添加的项目。但是,当我进入购物车页面时,它是空的。请参阅以下代码。

I have a custom template file, rendering some products and their Add To Cart buttons, that I am trying to implement manually. When Add to Cart is pressed, the page is reloaded and a $_POST variable containing some data adds the new products. 'cart_contents_count' also reflects the added item. However, when I go to the cart page, it's empty. Please see the following code.

global $woocommerce;
if ( isset( $_POST['AddedToCart'] ) ) {
$woocommerce->cart->add_to_cart($_POST['event'], $_POST['qty']);
}
$cart_total = $woocommerce->cart->cart_contents_count; 

当我转到正常的默认商店页面(/ shop /)并从那里添加产品时,我的购物车页面指示此产品已添加。现在,当我进入自定义页面并通过添加到购物车按钮添加产品​​时,它可以正常工作。

When I however go to the normal default shop page ( /shop/ ) and add a product from there, my cart page indicates that this product has been added. When I NOW go to my custom page and add products from that Add To Cart button, it works perfectly.

在我看来,在运行上述功能之前代码,我必须检查购物车会话是否已初始化,如果没有初始化,则将其初始化。有人可以帮我确认一下我理解正确并告诉我如何初始化购物车吗?

It seems to me that, before I run the above-mentioned code, I must check if a Cart Session has been initialized, and if not, initialize it. Could someone please confirm for me that I understand it right and show me how to initialize the cart?

推荐答案

如果您的自定义表单位于页面模板上。此代码位于您的functions.php文件中。确保将您的主题模板更改为更独特的内容。另外,将 $ session_templates 数组中的项目更改为要使用此过滤器的模板文件名。它使用 template_include 过滤器,这不是一个易于跟踪的过滤器,更不用说 $ woocommerce-> session-> set_customer_session_cookie(true )-特别感谢@vrazer(在Twitter上提供了帮助)。

Here is a solution if your custom form is on a page template. This code goes in your functions.php file. Be sure to change yourtheme_template to something more unique. Also, change the items in the $session_templates array to the template filenames where you want this filter to be used. It uses the template_include filter, which isn't an easy filter to track down, let alone $woocommerce->session->set_customer_session_cookie(true) - Special thanks to @vrazer (on twitter) for the help.

function yourtheme_template($template) {

//List of template file names that require WooCommerce session creation
$session_templates = array(
    'page-template-file-name.php', 'another-page-template-filename.php'
);

//Split up the template path into parts so the template file name can be retrieved
$parts = explode('/', $template);

//Check the template file name against the session_templates list and instantiate the session if the
//template is in the list and the user is not already logged in.  If the session already exists from
//having created a cart, WooCommerce will not destroy the active session
 if (in_array($parts[count($parts) - 1], $session_templates)  && !is_user_logged_in()) {
 global $woocommerce;

  $woocommerce->session->set_customer_session_cookie(true);
 }

 return $template;
}

//Filter to run the WooCommerce conditional session instantiation code
add_filter('template_include', 'yourtheme_template');

这篇关于如何正确初始化Woocommerce购物车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆