当前位置: 首页 > 知识库问答 >
问题:

WooCommerce从购物车获取产品自定义元

周宏伯
2023-03-14
function add_names_on_tshirt_field() {   
    global $post;
    $name_one_on_tshirt = "";
    $name_two_on_tshirt = "";
    $name_three_on_tshirt = "";

    foreach ( WC()->cart->cart_contents as $key => $value ) {
        if( $value["product_id"] == $post->ID && isset( $value["name_one_on_tshirt"] ) ) {
            $name_one_on_tshirt = $value["name_one_on_tshirt"];
        }
        if( $value["product_id"] == $post->ID && isset( $value["name_two_on_tshirt"] ) ) {
            $name_two_on_tshirt = $value["name_two_on_tshirt"];
        }
        if( $value["product_id"] == $post->ID && isset( $value["name_three_on_tshirt"] ) ) {
            $name_three_on_tshirt = $value["name_three_on_tshirt"];
        }
    }

    echo '<table class="variations" cellspacing="0">
      <tbody>
          <tr>
            <td class="label"><label for="color">Name 1 on T-Shirt</label></td>
            <td class="value">
                <input type="text" name="name-one-on-tshirt" value="'. esc_attr( $name_one_on_tshirt ) .'" />
            </td>
        </tr>
        <tr>
            <td class="label"><label for="color">Name 2 on T-Shirt</label></td>
            <td class="value">
                <input type="text" name="name-two-on-tshirt" value="'. esc_attr( $name_two_on_tshirt ) .'" />
            </td>
        </tr>
        <tr>
            <td class="label"><label for="color">Name 3 on T-Shirt</label></td>
            <td class="value">
                <input type="text" name="name-three-on-tshirt" value="'. esc_attr( $name_three_on_tshirt ) .'" />
            </td>
        </tr>
      </tbody>
    </table>';
}
add_action( 'woocommerce_before_add_to_cart_button', 'add_names_on_tshirt_field' );

问题在于当我的客户尝试从购物车中编辑一个产品时。因为所有产品都有相同的ID(我只有一个产品),所以添加到购物车的最后一个产品的元加载到文本字段中。我当前使用产品id检索foreach中的元,但该id与购物车中的所有其他产品相同。有没有其他方法可以在不使用PRODUCT_ID/$post->ID(如cart_item_key或使用会话)的情况下检索元?

共有1个答案

傅元龙
2023-03-14

下面是你如何实现这一点的想法

您必须编辑your-theme/woocommerce/cart.php(特别是product-thumbnail TD和product-name TD)

<?php

$query_str = "";

if( isset( $cart_item["name_one_on_tshirt"] ) ) {
    $query_str .= "?name_one_on_tshirt=" . $cart_item["name_one_on_tshirt"];
}

if( isset( $cart_item["name_two_on_tshirt"] ) ) {
    if( $query_str == "" ) {
        $query_str .= "?name_two_on_tshirt=" . $cart_item["name_two_on_tshirt"];
    } else {
        $query_str .= "&name_two_on_tshirt=" . $cart_item["name_two_on_tshirt"];
    }
}

if( isset( $cart_item["name_three_on_tshirt"] ) ) {
    if( $query_str == "" ) {
        $query_str .= "?name_three_on_tshirt=" . $cart_item["name_three_on_tshirt"];
    } else {
        $query_str .= "&name_three_on_tshirt=" . $cart_item["name_three_on_tshirt"];
    }
}
?>

<td class="product-thumbnail">
<?php
$thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $_product->get_image(), $cart_item, $cart_item_key );

if ( ! $_product->is_visible() ) {
    echo $thumbnail;
} else {    
    printf( '<a href="%s">%s</a>', esc_url( $_product->get_permalink( $cart_item ) . $query_str ), $thumbnail );    
}
?>
</td>

<td class="product-name" data-title="<?php _e( 'Product', 'woocommerce' ); ?>">
    <?php
        if ( ! $_product->is_visible() ) {
            echo apply_filters( 'woocommerce_cart_item_name', $_product->get_title(), $cart_item, $cart_item_key ) . ' ';
        } else {         
            echo apply_filters( 'woocommerce_cart_item_name', sprintf( '<a href="%s">%s</a>', esc_url( $_product->get_permalink( $cart_item ) . $query_str ), $_product->get_title() ), $cart_item, $cart_item_key );                                        
        } 
        // Meta data
        echo WC()->cart->get_item_data( $cart_item );

            // Backorder notification
        if ( $_product->backorders_require_notification() && $_product->is_on_backorder( $cart_item['quantity'] ) ) {
            echo '<p class="backorder_notification">' . esc_html__( 'Available on backorder', 'woocommerce' ) . '</p>';
        }
    ?>
</td>

然后更新add_name_on_tshirt_field函数,如下所示

function add_names_on_tshirt_field() {
    global $post;
    $name_one_on_tshirt = "";
    $name_two_on_tshirt = "";
    $name_three_on_tshirt = "";

    if( isset( $_GET["name_one_on_tshirt"] ) ) {
        $name_one_on_tshirt = $_GET["name_one_on_tshirt"];
    }

    if( isset( $_GET["name_two_on_tshirt"] ) ) {
        $name_two_on_tshirt = $_GET["name_two_on_tshirt"];
    }

    if( isset( $_GET["name_three_on_tshirt"] ) ) {
        $name_three_on_tshirt = $_GET["name_three_on_tshirt"];
    }

    echo '<table class="variations" cellspacing="0">
      <tbody>
          <tr>
            <td class="label"><label for="color">Name 1 on T-Shirt</label></td>
            <td class="value">
                <input type="text" name="name-one-on-tshirt" value="'. esc_attr( $name_one_on_tshirt ) .'" />
            </td>
        </tr>
        <tr>
            <td class="label"><label for="color">Name 2 on T-Shirt</label></td>
            <td class="value">
                <input type="text" name="name-two-on-tshirt" value="'. esc_attr( $name_two_on_tshirt ) .'" />
            </td>
        </tr>
        <tr>
            <td class="label"><label for="color">Name 3 on T-Shirt</label></td>
            <td class="value">
                <input type="text" name="name-three-on-tshirt" value="'. esc_attr( $name_three_on_tshirt ) .'" />
            </td>
        </tr>
      </tbody>
    </table>';
}
add_action( 'woocommerce_before_add_to_cart_button', 'add_names_on_tshirt_field' );
 类似资料:
  • 问题内容: 我有上面的代码。 如果我在cart_item上运行print_r,我将得到一个多维数组: 我如何只获得product_id? 我试过$ test = 没用 问题答案: 要获取 foreach循环中的每个购物车商品(对于简单产品): 如果是可变产品,请获取 : 或在两种情况下 ( Woocommerce 3+中 的 Object在哪里) : 更新: 循环外使用产品ID 1)打破循环 (仅

  • 我正在尝试为variations父产品创建一个单独的add to cart按钮。我正在使用以下方法访问产品数据: 谢谢你。

  • 当我进入产品编辑页面时,有一个选项卡“属性”。在那里我可以设置属性名及其值。 我假设这就是在Woocommerce上为产品添加自定义属性的方式。 但是如何在循环中得到这个值呢? 我看到人们使用,但它希望我传递分类法和另一个参数数组。分类学是什么?我没有手动添加。论点是什么?

  • 你在为它做什么?我在文件末尾(wp-content/plugins/woocommerce/woocommerce.php)写了这个字符串: 但当我从购物车中删除项目时,它就不起作用了! 但也不起作用

  • 我正在尝试将我的WooCommerce档案页面上特定产品类别的add to cart按钮更改为“Read More”按钮,该按钮将链接到产品页面(但不链接到单个产品页面本身)。 使用“将添加到购物车按钮替换为WooCommerce 3中的shop pages中的read more链接到product页面”的答案代码,如何将其限制为仅有一个产品类别(本例中术语名称为“Classs”)? 感谢任何帮助