私は、woocommerceスマートクーポンを使用して "招待コード"を作成しようとしています。そのためには、カート/チェックアウト時に特定の商品にクーポンコードが必要になります。私はこれを行う方法を見てきましたが、私が望むものだけでなく、すべての製品に影響します。特定の商品カテゴリを持つ必須のクーポンコードの通知を表示
返された変数を別の関数に渡そうとしていますが、その方法を理解できません。
//Check to see if user has product with a specific product category in cart
function check_product_in_cart() {
global $woocommerce;
//assigns a default negative value
// categories targeted 87, 18, 19
$product_in_cart = false;
// start of the loop that fetches the cart items
foreach ($woocommerce->cart->get_cart() as $cart_item_key => $values) {
$_product = $values['data'];
$terms = get_the_terms($_product->id, 'product_cat');
// second level loop search, in case some items have several categories
foreach ($terms as $term) {
$_categoryid = $term->term_id;
if (($_categoryid === 87) || ($_categoryid === 18) || ($_categoryid === 19)) {
//category is in cart!
$product_in_cart = true;
}
}
}
return $product_in_cart;
}
// Force Coupon codes for Woocommerce
add_action('woocommerce_check_cart_items', 'force_coupon_code');
function force_coupon_code($product_in_cart)
{
global $woocommerce;
if(is_cart() || is_checkout()){
$my_coupon = $woocommerce->cart->applied_coupons;
echo $woocommerce->cart->get_applied_coupons;
if(empty($my_coupon))
{
wc_add_notice('<strong>' . $btn['label'] . '</strong> ' . __('A coupon code is mandatory for this product.', 'woocommerce'), 'error');
}
}
}
最初の部分は、製品の$ product_in_cartと第二部は、クーポンを強制的に返す必要があります。ここでは
は最新バージョンです。ここで私が使用しているコードのソースがあります。 Mandatory Coupon codeおよびChecking products in cart based on category。新しい関数argの中に変数を置くことは、like soを達成するかもしれませんが、それは決して機能しません。
美しい!魅力を発揮します。私はまだ学んでいるので、もっと良いコードを除いて、 'if(空($ my_coupon)&& $ has_cat)'という行はすべて一緒に結びついているものです。理論的には、文にさらに引数を追加し、 'if(empty($ my_coupon)&& $ has_cat)&& $ color_blue && etc ... 'のようにさらに狭くすることはできますか? – jimmyjames