我想使用所有用户的代码将产品添加到购物车。 add_to_cart()函数可以完成工作,但仅对我有用。
我怎样才能做到为每个人做到?还有其他什么可以帮助我完成自己想要的事情吗?
$args = array('post_type' => 'product', 'posts_per_page' => -1);
$all_products = get_posts($args);
$woocommerce = WC();
foreach ($buyers as $subscriber) {
$products = array_slice($all_products, rand(0, count($all_products)), $subscriber['count']);
foreach ($products as $product) {
try {
$woocommerce->cart->add_to_cart($product->ID, rand(1, 20));
wp_send_json(true);
} catch (\Exception $e) {
wp_send_json($e);
}
}
}