Skip to content

Instantly share code, notes, and snippets.

@anibalardid
Created March 6, 2019 20:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anibalardid/79d57c2b5735f91e8b5aa1e2f9fcce0a to your computer and use it in GitHub Desktop.
Save anibalardid/79d57c2b5735f91e8b5aa1e2f9fcce0a to your computer and use it in GitHub Desktop.
integración de woocommerce con paid membership pro #wordpress #woocommerce
add_action('template_redirect','pmpro_woocommerce_redirect_checkout');
function pmpro_woocommerce_redirect_checkout(){
$id = get_option('pmpro_checkout_page_id');
global $post;
if($post->ID == $id && !empty($_REQUEST['level'])){
global $wpdb;
$product_id = $wpdb->get_var($wpdb->prepare("SELECT pm.post_id FROM {$wpdb->postmeta} as pm LEFT JOIN {$wpdb->posts} as p ON p.ID=pm.post_id WHERE pm.meta_key = %s AND pm.meta_value = %d AND p.post_status='publish'",'_membership_product_level',$_REQUEST['level']));
if(!empty($product_id)){
$checkout_url = WC()->cart->get_checkout_url();
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->id == $product_id )
$found = true;
}
// if product not found, add it
if ( ! $found )
WC()->cart->add_to_cart( $product_id );
wp_redirect( $checkout_url);
}else{
// if no products in cart, add it
WC()->cart->add_to_cart( $product_id );
wp_redirect( $checkout_url);
}
exit();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment