私はwebshopのテストケースに取り組んでいます。私は、自動的に商品/商品を訪問時にカートに追加することをお勧めします。だから私はどこでも同じコードを何度も見つけたときにそのようなものを探しました。複数の商品をカートに自動的に追加訪問するWooCommerce
/*
* goes in theme functions.php or a custom plugin
**/
// add item to cart on visit
add_action('template_redirect', 'add_product_to_cart');
function add_product_to_cart() {
if (! is_admin()) {
$product_id = 64;
$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);
}
}
}
から:
https://docs.woocommerce.com/document/automatically-add-product-to-cart-on-visit/ https://gist.github.com/kloon/2376300
ですから、1つの製品だけを持っている場合、これは正常に動作しますが、私はちょうど1製品以上のものを追加したいと思います。私を助けることができるいくつかのPHPの知識(といくつかのワードプレス)を持っている人はいますか?前もって感謝します!