【问题标题】:Restrict a custom plugin functionality to a specific Woocommerce product or category将自定义插件功能限制为特定的 Woocommerce 产品或类别
【发布时间】:2018-01-10 19:00:02
【问题描述】:

首先,我知道在 stackoverflow 上有类似的主题,但是我尝试过的所有方法都对我有用。

我创建了一个插件来获取用户在所有产品页面上的文本输入。我需要这个额外的功能才能仅在某些产品或某个类别中的某些产品上运行。

这里是功能。

function kia_custom_option(){
$value = isset( $_POST['_custom_option'] ) ? sanitize_text_field( $_POST['_custom_option'] ) : '';
printf( '<label>%s</label><input name="_custom_option" value="%s" />', __( 'Custom Text: ', 'kia-plugin-textdomain' ), esc_attr( $value ) );
}
add_action( 'woocommerce_before_add_to_cart_button', 'kia_custom_option', 9 );

function kia_add_to_cart_validation($passed, $product_id, $qty){

if( isset( $_POST['_custom_option'] ) && sanitize_text_field( $_POST['_custom_option'] ) == '' ){
    $product = wc_get_product( $product_id );
    wc_add_notice( sprintf( __( '%s cannot be added to the cart until you enter your custom text.', 'kia-plugin-textdomain' ), $product->get_title() ), 'error' );
    return false;
}

return $passed;

}
add_filter( 'woocommerce_add_to_cart_validation', 'kia_add_to_cart_validation', 10, 3 );

function kia_add_cart_item_data( $cart_item, $product_id ){

if( isset( $_POST['_custom_option'] ) ) {
    $cart_item['custom_option'] = sanitize_text_field( $_POST['_custom_option'] );
}

return $cart_item;

}
add_filter( 'woocommerce_add_cart_item_data', 'kia_add_cart_item_data', 10, 2 );

function kia_get_cart_item_from_session( $cart_item, $values ) {

if ( isset( $values['custom_option'] ) ){
    $cart_item['custom_option'] = $values['custom_option'];
}

return $cart_item;

}
add_filter( 'woocommerce_get_cart_item_from_session', 'kia_get_cart_item_from_session', 20, 2 );

function kia_add_order_item_meta( $item_id, $values ) {

if ( ! empty( $values['custom_option'] ) ) {
    woocommerce_add_order_item_meta( $item_id, 'custom_option', $values['custom_option'] );           
}
}
add_action( 'woocommerce_add_order_item_meta', 'kia_add_order_item_meta', 10, 2 );

function kia_get_item_data( $other_data, $cart_item ) {

if ( isset( $cart_item['custom_option'] ) ){

    $other_data[] = array(
        'name' => __( 'Origin Email', 'kia-plugin-textdomain' ),
        'value' => sanitize_text_field( $cart_item['custom_option'] )
    );

}

return $other_data;

}
add_filter( 'woocommerce_get_item_data', 'kia_get_item_data', 10, 2 );

我试图仅在某些产品或类别上显示文本框。我有一个 ID 为 680 的产品。

我在插件顶部尝试了这个。

if (is_single( '680' )) {
//All the above code here.
}

但这没有用。我也按类别尝试过,但也没有运气。有人可以向我解释我做错了什么或如何将这些功能仅应用于某些产品吗?

编辑---我找到了解决方案:

function kia_custom_option(){
global $product;

$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;

if($product_id == 680 || $product_id == 687){
    $value = isset( $_POST['_custom_option'] ) ? sanitize_text_field( $_POST['_custom_option'] ) : '';
    printf( '<label>%s</label><input name="_custom_option" value="%s" />', __( 'Custom Text: ', 'kia-plugin-textdomain' ), esc_attr( $value ) );
}
}
add_action( 'woocommerce_before_add_to_cart_button', 'kia_custom_option', 9 );

【问题讨论】:

    标签: php wordpress woocommerce custom-fields custom-taxonomy


    【解决方案1】:

    更新 (看来您使用的是 WooCommerce 3 之前的版本)

    对于产品类别,您可以使用 has_term() Wordpress 条件函数和 WooCommerce 使用的相应 'product_cat' 自定义分类法,就在您的第一个挂钩函数中,您将通过以下方式定义允许的产品类别(或产品类别):

    function kia_custom_option(){
        global $product;
    
        // HERE set your allowed product categories (can be IDs, slugs or names)
        $product_categories = array( 't-shirts', 'socks' );
    
        // Added WooCommerce retro compatibility
        $product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
    
        if( has_term( $product_categories, 'product_cat', $product_id ) ){
    
            $value = isset( $_POST['_custom_option'] ) ? sanitize_text_field( $_POST['_custom_option'] ) : '';
            printf( '<label>%s</label><input name="_custom_option" value="%s" />', __( 'Custom Text: ', 'kia-plugin-textdomain' ), esc_attr( $value ) );
        }
    }
    add_action( 'woocommerce_before_add_to_cart_button', 'kia_custom_option', 9 );
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。

    经过测试和工作(适用于自 2.4 以来的所有 WC 版本)。不需要其他任何东西。


    对于已定义的产品 ID,代码将为:

    function kia_custom_option(){
        global $product;
    
        // HERE set your allowed product IDs in the array
        $product_ids = array( 680, 687 );
    
        // Added WooCommerce retro compatibility
        $product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
    
        if( in_array( $product_id, $product_ids ) ){
    
            $value = isset( $_POST['_custom_option'] ) ? sanitize_text_field( $_POST['_custom_option'] ) : '';
            printf( '<label>%s</label><input name="_custom_option" value="%s" />', __( 'Custom Text: ', 'kia-plugin-textdomain' ), esc_attr( $value ) );
        }
    }
    add_action( 'woocommerce_before_add_to_cart_button', 'kia_custom_option', 9 );
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。

    经过测试和工作(适用于自 2.4 以来的所有 WC 版本)

    【讨论】:

    • 感谢您的回复。虽然我确信这将在我的 functions.php 文件中工作,但我怎样才能使此代码能够从我的插件中运行?把它放在我的插件文件的顶部,会导致页面不显示的错误。
    • 不幸的是,我无法在我的插件中使用它。我设法让它使用与您的非常相似的替代解决方案。我将使用解决方案更新原始帖子。感谢您的建议。
    • @SamuelClayton 更新:我认为问题是由于您使用的是旧版本的 woocommerce(版本 3 之前)......所以我添加了兼容性。我的代码现在适用于自 2.4 版以来的所有 woocommerce 版本……所以请,因为它正在工作并回答您的问题,如果您愿意/喜欢,您应该接受这个答案和/或投票~它。谢谢
    猜你喜欢
    • 2016-08-06
    • 1970-01-01
    • 1970-01-01
    • 2018-08-19
    • 2018-01-25
    • 2023-04-08
    • 2017-11-13
    相关资源
    最近更新 更多