返回woocommerce商品名称从购物车只有当他们是一定的变化


return woocommerce item names from cart only if they are a certain variation

我对php还是个新手。我的目标是从登录用户的购物车中获取商品的名称,并用这些信息动态地填充GravityForms模板。我已经成功地用下面的代码做到了这一点,但是有三件事我没有做到。1:我只想用特定变化的所有项填充表单。2:echo函数将列出所有的项目名称,但不包含在相关字段中,return函数将填充字段,但只包含第一个项目名称。3:我希望在每个项目名称之间使用某种形式的分隔符列出输出项目。以下是目前为止的内容:

<?php  
add_filter( 'gform_field_value_beat_names', 'beat_names_function' );
function beat_names_function( $value ) {
    global $woocommerce;
    $items = $woocommerce->cart->get_cart();
      foreach ($items as $item) {
      $product_variation_id = $item['variation_id'];

        $license_id = new WC_Product($product_variation_id);
        $license_string = $license_id->get_formatted_name();
        $beat_id = $item['data']->post;     
        $beat_name = $beat_id->post_title;

        if (strpos($license_string, 'basic') !== false) {
             echo $beat_name;
        }
     }
}?>

正如您所看到的,我试图使用strpos来隔离我想要瞄准的特定项目变体,使用在变体选项名称中找到的某个单词,即"基本"。我相信有更安全的方法,但目前还有效。问题在于我在条件strpos语句中设置的返回函数,因为它仍然只返回购物车项目的整个列表,而不仅仅是我试图用条件strpos隔离的项目。

最终目标是创建一个使用相关信息动态填充的许可协议,包括被许可的项目的名称。项目变体是我为我的商店中的产品提供的不同许可选项。以上代码的目的是按许可类型过滤购物车商品,以便在结帐时不会在错误的许可协议中列出错误的商品名称。

经过一番修修补补,我终于明白了。我把它一步一步地分解出来,以帮助像我一样无知的人。

add_filter( 'gform_field_value_basic_beat_names', 'basic_beat_names_function' );
function basic_beat_names_function( $value ) {
global $woocommerce;
$items = $woocommerce->cart->get_cart();
// the item names you want will be stored in this array
$license_check = array();
// Loop through cart items
foreach ($items as $item) {
  // get the id number of each product variation in the cart. this won't work for "simple" products
  $product_variation_id = $item['variation_id'];
    // translate that id number into a string containing the name of the product, and the options applied to the product
    $license_id = new WC_Product($product_variation_id);
    $license_string = $license_id->get_formatted_name();
    // check to see if the word "basic" is found anywhere in that string. "basic" is contained in the name of the product option being targeted
    if (strpos($license_string, 'basic') !== false) {
        // if the above condition is met, fetch the name of any products in the cart that have that option applied. The product name will be fetched from the title of the product's page
        $beat_id = $item['data']->post;     
        $beat_name = $beat_id->post_title;
        // store the retrieved product names in the $license_check array
        $license_check [] = $beat_name;
    }
}
    // pull the stored product names from the $license_check array, and format them using implode and concatenation, then return the final result to the form field
    return "'"" . implode("'",&nbsp;'"", $license_check) . "'"";
}
作为警告,strpos方法有点粗糙。只有当你的字符串足够具体,只针对你正在寻找的选项时,它才会正常工作。作为一个例子,下面是输入到strpos函数中的产品变量字符串的格式:
item-name-option-one-name-option-two-name – variation #xxx of item page title
所以,如果你想只通过选项1来过滤条目,最安全的做法是这样写strpos函数:
if (strpos($license_string, 'option-one-name') !== false) {
    //do something
} 

当所有的说和做,最终的结果应该看起来像:"item1", "item2", "item3",等等

然后,要对任何其他选项执行相同的操作,并将结果输出到不同的字段或单独的表单中,我只需复制此代码,并用其他选项中包含的一些不同的唯一字符串替换任何提到的单词"basic"。也不要忘记在必要时配置重力表单字段。