如果产品变体包含此词组,请执行此操作

时间:2018-10-10 13:44:03

标签: php woocommerce conditional variations

已编辑:问题:Force Sells插件的数量需要根据添加到购物车中的商品数量而有所不同,或者根据所选的变化量(pa_size)而有所不同。

换句话说,我们正试图强迫出售冰袋。如果客户购买的产品少于4个(数量),我们要强制销售1个冰袋。如果他们购买8个(数量)产品,我们要强制出售2个冰袋。而且,如果他们购买了6包(数量)中的1个(数量),我们想强制出售2个冰包。而且,如果他们购买了12个装(数量)中的1个(数量),我们想强制出售3个冰袋。除此以外,强行出售1个Ice Pack。

我真的很想学习如何实现这一目标。

我需要变量$ quantitybj作为结果,因为它将被更多的代码传递到更远的地方。

我现在得到的输出是$ quantitybj = 3-无论如何。 我期望的输出是$ quantitybj取决于数量和所选产品的变化(即6包饼干,而不是1包饼干)。

我已注释掉以下代码中的错误。

$ product_size 应该是一个字符串,然后我尝试查看该字符串是否包含单词“ 6 pack”或“ 12 pack”。如何获得选定的变体进行比较?

这是woocommerce-force-sells.php中的完整功能。 我添加的代码已用BJ注释掉-以便您知道我添加的内容。

BEGIN
  IF (SELECT count(*) FROM USERS_STATS WHERE LOGIN = OLD.LOGIN) = 0 THEN
    INSERT INTO USERS_STATS (LOGIN) values (OLD.LOGIN);
  END IF;
  UPDATE USERS_STATS SET
    TOTAL_DEPOSIT = TOTAL_DEPOSIT - IF(OLD.CMD = 6 AND OLD.COMMENT NOT LIKE '%internal%' AND OLD.COMMENT NOT LIKE '%Tr %' AND OLD.COMMENT NOT LIKE '%agent%' AND OLD.COMMENT NOT LIKE '%rebate%' AND OLD.COMMENT NOT LIKE '%MAM:CASH%' AND OLD.PROFIT > 0, OLD.PROFIT, 0),
    TOTAL_WITHDRAW = TOTAL_WITHDRAW - IF(OLD.CMD = 6 AND OLD.COMMENT NOT LIKE '%internal%' AND OLD.COMMENT NOT LIKE '%Tr %' AND OLD.COMMENT NOT LIKE '%agent%' AND OLD.COMMENT NOT LIKE '%rebate%' AND OLD.COMMENT NOT LIKE '%MAM:CASH%' AND OLD.PROFIT < 0, OLD.PROFIT, 0),
    TOTAL_VOLUME = TOTAL_VOLUME - IF(OLD.CMD IN(0, 1) AND OLD.CLOSE_TIME > '1970-01-01', OLD.VOLUME, 0),
    TOTAL_REBATE = TOTAL_REBATE - IF((OLD.COMMENT LIKE '%agent%' OR OLD.COMMENT LIKE '%rebate%' OR OLD.COMMENT LIKE '%MAM:CASH%'), ROUND(OLD.PROFIT, 2), 0)
  WHERE LOGIN = OLD.LOGIN;
END

1 个答案:

答案 0 :(得分:2)

基本上,您需要一些条件语句,一个条件语句检查数量是否为1,另一个条件语句检查数量是否为4以上,另一个条件语句检查数量是否为8以上,另外一个条件语句为包。

看看这个例子,我对代码进行了重注释,希望您可以轻松地遵循它:

<?php

// Declare the sizes of the packs, get a random number between 1-8, and then a random pack size
$sizes = array("6 pack", "12 pack", "18 pack", "24 pack", "30 pack");
$quantity = rand(1, 8);
$product_size = $sizes[rand(0, count($sizes) - 1)];

// Print the quantity and pack size
echo $quantity, ' cases of ', $product_size, 's';

// The default number of ice packs is 1
$ice_packs = 1;
if ($quantity === 1) {
    // if the quantity is 1 and it is a 6 pack, then add an aditional ice pack
    // if the quantity is 1 and it is a 12 pack, then add one more addtional ice pack
    if ($product_size === "6 pack") {
        $ice_packs++;
    } elseif ($product_size === "12 pack") {
        $ice_packs += 2;
    }
} else {
    // if the quantity is 4 or more then add an additional ice pack
    // if the quantity is 8 or more then add one more additional ice pack
    if ($quantity > 3) {$ice_packs++;}
    if ($quantity > 7) {$ice_packs++;}
}

echo '<br />Ice Packs: ', $ice_packs;

提琴:Live Demo