【问题标题】:Display purchase note and attribute on cart and checkout page in WooCommerce在 WooCommerce 的购物车和结帐页面上显示购买说明和属性
【发布时间】:2021-09-23 13:07:24
【问题描述】:

以下代码有效,并将购买备注添加到购物车中。

add_filter( 'woocommerce_get_item_data', 'wc_test', 10, 2 );  
function wc_test ( $other_data, $cart_item ){
    $post_data = get_post( $cart_item['product_id'] );
    $other_data[] = array( 'name' =>  $post_data->_purchase_note );
    return $other_data;
}

但是,对于没有注释的产品,我总是得到“:”。

我还需要在注释下添加一个特定的属性。

有什么建议吗?

【问题讨论】:

    标签: php wordpress woocommerce cart checkout


    【解决方案1】:
    • 冒号会自动添加到密钥中。因为您只使用名称,所以会在最后添加。
    • 您可以使用get_purchase_note()

    所以你得到:

    // Display on cart & checkout pages
    function filter_woocommerce_get_item_data( $item_data, $cart_item ) {
        // Get an instance of the WC_Product object
        $product = $cart_item['data'];
        
        // Get purchase_note
        $purchase_note = $product->get_purchase_note();
        
        // Get the product attribute value (adjust if desired)
        $attribute = $product->get_attribute( 'pa_color' );
        
        // When empty
        if ( empty ( $attribute ) ) {
            $attribute = '';
        }
        
        // NOT empty
        if ( ! empty( $purchase_note ) ) {
            $item_data[] = array(
                'key'     => __( 'Note', 'woocommerce' ),
                'value'   => $purchase_note . $attribute,
                'display' => $purchase_note . $attribute,
            );
        }
        
        return $item_data;
    }
    add_filter( 'woocommerce_get_item_data', 'filter_woocommerce_get_item_data', 10, 2 );
    

    【讨论】:

    • “注释”部分完美运行!多谢! /products/attributes 我复制了名为“lieferzeit”的“slug”=英文的交货时间)——我想我需要添加“名称”而不是“slug”得到它!
    • 问题:所有内容都只显示在 1 个产品下,而不是全部显示
    • @llndave 这个钩子是对每个购物车项目(产品)单独执行的,所以只要有产品的购买说明,它就会单独显示每个产品的这个说明。另请阅读What should I do when someone answers my question?。确定答案是否有帮助,然后在上面vote 和/或accept 它。
    • 我做了一些改变。看起来不错。你能告诉我如何在邮件中添加相同的内容吗?'// NOT empty if ( ! empty( $purchase_note ) ) { $item_data[] = array( 'key' => __( "Artikelnummer" ), 'value' => $sku_field . $attribute . $purchase_note, 'display' => $sku_field . '<br><br><p style="color: #fad900; font-weight: bold; letter-spacing: 0.5px;">Lieferzeit: </p>' . $attribute . '<br>' . '<p style="color: #fad900; font-weight: bold; letter-spacing: 0.5px;">Anmerkung: </p><br>' . $purchase_note, ); }'-----------------
    猜你喜欢
    • 1970-01-01
    • 2018-02-01
    • 2015-03-17
    • 2013-12-13
    • 1970-01-01
    • 2016-10-30
    • 1970-01-01
    • 1970-01-01
    • 2012-09-29
    相关资源
    最近更新 更多