Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 自定义添加到购物车按钮,如果客户以前购买过该产品_Php_Wordpress_Woocommerce_Product_Orders - Fatal编程技术网

Php 自定义添加到购物车按钮,如果客户以前购买过该产品

Php 自定义添加到购物车按钮,如果客户以前购买过该产品,php,wordpress,woocommerce,product,orders,Php,Wordpress,Woocommerce,Product,Orders,在WooCommerce中,如果客户以前购买过产品,我需要知道是否有任何选项可以更改“添加到购物车”按钮 我们通过WooCommerce&Membership销售在线课程,因此我们的想法是,如果客户没有购买课程,他必须看到“立即购买”按钮。但如果他已经购买了课程,他应该会看到一个“立即查看”按钮,每个产品(课程)都有一个自定义链接 我怎样才能做到这一点 谢谢。下面的解决方案将其添加到functions.php中,应该可以正常工作。当订单完成时,按钮将被更改 add_filter('woocomm

在WooCommerce中,如果客户以前购买过产品,我需要知道是否有任何选项可以更改“添加到购物车”按钮

我们通过WooCommerce&Membership销售在线课程,因此我们的想法是,如果客户没有购买课程,他必须看到“立即购买”按钮。但如果他已经购买了课程,他应该会看到一个“立即查看”按钮,每个产品(课程)都有一个自定义链接

我怎样才能做到这一点


谢谢。

下面的解决方案将其添加到functions.php中,应该可以正常工作。当订单完成时,按钮将被更改

add_filter('woocommerce_loop_add_to_cart_link','add_to_cart_link_customer_has_bought');

    function add_to_cart_link_customer_has_bought() {

        global $product;

        if( empty( $product->id ) ){

            $wc_pf = new WC_Product_Factory();
            $product = $wc_pf->get_product( $id );

        }

        $current_user = wp_get_current_user();

        if( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $product->id ) ){

            $product_url = get_permalink();
            $button_label =  "View Product";  

        } else {

            $product_url =  $product->add_to_cart_url();  
            $button_label = $product->add_to_cart_text();

        };

        echo sprintf( '<a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" data-quantity="%s" class="button %s product_type_%s">%s</a>',
            esc_url( $product_url ),
            esc_attr( $product->id ),
            esc_attr( $product->get_sku() ),
            esc_attr( isset( $quantity ) ? $quantity : 1 ),
            $product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
            esc_attr( $product->product_type ),
            esc_html( $button_label )
        );

    }
add_filter('woocommerce_loop_add_to_cart_link','add_to_cart_link_customer_has_buy');
功能添加到购物车链接客户已购买(){
全球$产品;
if(空($product->id)){
$wc_pf=新wc_产品_工厂();
$product=$wc\U pf->get\U product($id);
}
$current_user=wp_get_current_user();
如果(wc_客户_购买了_产品($current_user->user_email,$current_user->ID,$product->ID)){
$product\u url=get\u permalink();
$button\u label=“查看产品”;
}否则{
$product_url=$product->将_添加到_购物车_url();
$button_label=$product->add_to_cart_text();
};
echo sprintf(“”,
esc_url($product_url),
esc_attr($product->id),
esc_attr($product->get_sku()),
esc_属性(isset($数量)?$数量:1),
$product->is_purchable()&&&$product->is_in_stock()?“添加到购物车按钮”:“,
esc_attr($product->product_type),
esc_html($button_标签)
);
}

这是一个功能齐全且经过测试的自定义功能,将显示在商店页面和商业页面上,这是一个定制的购物车附加功能(文本+链接),适用于已购买产品的登录客户:

add_filter( 'woocommerce_loop_add_to_cart_link', 'customizing_add_to_cart_button', 10, 2 );
function customizing_add_to_cart_button( $link, $product ){

    $bought = false;

    if( is_user_logged_in() ){

        $customer_orders = get_posts( array(
            'numberposts' => -1,
            'meta_key'    => '_customer_user',
            'meta_value'  => get_current_user_id(),
            'post_type'   => 'shop_order', // WC orders post type
            'post_status' => 'wc-completed' // Only orders with status "completed"
        ) );

        // Going through each current customer orders
        foreach ( $customer_orders as $customer_order ) {
            $order = wc_get_order( $customer_order->ID );
            // Going through each current customer order items
            foreach($order->get_items() as $item_id => $item_values){
                if($item_values['product_id'] == $product->id){
                    $bought = true;
                    break;
                }
            }
        }
    }

    if($bought){

        // ==> SET HERE YOUR
        // CUSTOM ADD TO CART text and link
        $add_to_cart_url = site_url('/custom_link/');
        $button_text =  __('View now', 'woocommerce');

    } else {

        $add_to_cart_url = $product->add_to_cart_url();
        $button_text =  $product->add_to_cart_text();

    }

    $link = sprintf( '<a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" data-quantity="%s" class="button product_type_%s">%s</a>',
        esc_url( $add_to_cart_url ),
        esc_attr( $product->id ),
        esc_attr( $product->get_sku() ),
        esc_attr( isset( $quantity ) ? $quantity : 1 ),
        esc_attr( $product->product_type ),
        esc_html( $button_text )
    );

    return $link;
}

哇,谢谢你!!还有一个问题,一旦使用了这个代码,我在哪里可以更改应该重定向按钮的url?是否可以为每种产品设置不同的链接,我可以如何设置?什么链接?它应该去哪里?您可以在此处放置链接
esc\u url($product\u url)
,但我们必须知道它是什么链接?
    if($bought){

        // for the product ID 45 (for example)
        if( $product->id == 45 ){
            $add_to_cart_url = site_url('/custom-link/product-45/');
            $button_text =  __('View now product 45', 'woocommerce');
        }

        // for the product ID 64 (for example)
        if( $product->id == 64){
            $add_to_cart_url = site_url('/custom-link/product-64/');
            $button_text =  __('View now product 64', 'woocommerce');
        }

        // for an array of product IDs (for example)
        $product_ids = array(89, 92, 124);
        if( in_array( $product->id, $product_ids ) ){
            $add_to_cart_url = site_url('/custom-link/some-products/');
            $button_text =  __('View now some products', 'woocommerce');
        }
        // for a product category
        // set here your product category ID, slug or name (or an array of values)
        $category = 'My category'; // Here a category name for example
        if( has_term( $category, 'product_cat', $product->id ) ){
            $add_to_cart_url = site_url('/custom-link/my-category/');
            $button_text =  __('View now from my category', 'woocommerce');
        }
    } else {

        $add_to_cart_url = $product->add_to_cart_url();
        $button_text =  $product->add_to_cart_text();

    }