Created
May 30, 2019 17:00
-
-
Save hslaszlo/376314a33788c1b0cd275fea590f94e0 to your computer and use it in GitHub Desktop.
Programmatically add product to cart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Programmatically add product to cart | |
Plugin URI: https://www.webmenedzser.hu | |
Description: Add a product to the cart programmatically if the user role is subscriber | |
Version: 1.0 | |
Author: Radics Ottó | |
Author URI: https://www.webmenedzser.hu | |
License: GPLv3 | |
*/ | |
function add_product_to_cart_programmatically() { | |
if (current_user_can('subscriber')) { | |
$productID = 39; | |
$productCartID = WC()->cart->generate_cart_id( $productID ); | |
$inCart = WC()->cart->find_product_in_cart( $productCartID ); | |
if ( !$inCart ) { | |
WC()->cart->add_to_cart( $productID ); | |
} | |
} | |
} | |
add_action( 'template_redirect', 'add_product_to_cart_programmatically', 10, 0 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment