| 1 |
<?php |
| 2 |
/** |
| 3 |
* Dynamic Flow shortcodes |
| 4 |
* |
| 5 |
* @package CartFlows |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Initialization |
| 10 |
* |
| 11 |
* @since 1.0.0 |
| 12 |
*/ |
| 13 |
class Cartflows_Wd_Flow_Shortcodes { |
| 14 |
|
| 15 |
|
| 16 |
/** |
| 17 |
* Member Variable |
| 18 |
* |
| 19 |
* @var instance |
| 20 |
*/ |
| 21 |
private static $instance; |
| 22 |
|
| 23 |
/** |
| 24 |
* Member Variable |
| 25 |
* |
| 26 |
* @var object _product |
| 27 |
*/ |
| 28 |
private static $main_product = array(); |
| 29 |
|
| 30 |
/** |
| 31 |
* Member Variable |
| 32 |
* |
| 33 |
* @var string Add to cart text. |
| 34 |
*/ |
| 35 |
private static $add_to_cart_text = ''; |
| 36 |
|
| 37 |
/** |
| 38 |
* Initiator |
| 39 |
*/ |
| 40 |
public static function get_instance() { |
| 41 |
if ( ! isset( self::$instance ) ) { |
| 42 |
self::$instance = new self(); |
| 43 |
} |
| 44 |
return self::$instance; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Constructor |
| 49 |
*/ |
| 50 |
public function __construct() { |
| 51 |
|
| 52 |
add_shortcode( 'cartflows_product_title', array( $this, 'product_title' ) ); |
| 53 |
add_shortcode( 'cartflows_product_add_to_cart', array( $this, 'add_to_cart' ) ); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Get all selected products |
| 58 |
* |
| 59 |
* @param int $id product id. |
| 60 |
*/ |
| 61 |
public function get_product_obj( $id = false ) { |
| 62 |
|
| 63 |
if ( $id ) { |
| 64 |
|
| 65 |
if ( empty( self::$main_product[ $id ] ) ) { |
| 66 |
|
| 67 |
self::$main_product[ $id ] = wc_get_product( $id ); |
| 68 |
} |
| 69 |
|
| 70 |
return self::$main_product[ $id ]; |
| 71 |
} |
| 72 |
|
| 73 |
return null; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Product title. |
| 78 |
* |
| 79 |
* @param array $atts attributes. |
| 80 |
*/ |
| 81 |
public function product_title( $atts ) { |
| 82 |
|
| 83 |
if ( empty( $atts['id'] ) ) { |
| 84 |
return ''; |
| 85 |
} |
| 86 |
|
| 87 |
$product_id = intval( $atts['id'] ); |
| 88 |
$product = $this->get_product_obj( $product_id ); |
| 89 |
|
| 90 |
if ( ! $product ) { |
| 91 |
return ''; |
| 92 |
} |
| 93 |
|
| 94 |
if ( 'publish' !== $product->get_status() && ! current_user_can( 'read_private_products' ) ) { |
| 95 |
return ''; |
| 96 |
} |
| 97 |
|
| 98 |
return $product->get_title(); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Show a single product add to card. |
| 103 |
* |
| 104 |
* @param array $atts Attributes. |
| 105 |
*/ |
| 106 |
public function add_to_cart( $atts ) { |
| 107 |
|
| 108 |
if ( empty( $atts['id'] ) ) { |
| 109 |
return ''; |
| 110 |
} |
| 111 |
|
| 112 |
$args = array( |
| 113 |
'posts_per_page' => 1, |
| 114 |
'post_type' => 'product', |
| 115 |
'post_status' => 'publish', |
| 116 |
'ignore_sticky_posts' => 1, |
| 117 |
'no_found_rows' => 1, |
| 118 |
); |
| 119 |
|
| 120 |
if ( isset( $atts['id'] ) ) { |
| 121 |
$args['p'] = absint( $atts['id'] ); |
| 122 |
} |
| 123 |
|
| 124 |
// Update the add to cart button text. |
| 125 |
if ( isset( $atts['text'] ) && ! empty( $atts['text'] ) ) { |
| 126 |
|
| 127 |
self::$add_to_cart_text = sanitize_text_field( $atts['text'] ); |
| 128 |
|
| 129 |
add_filter( 'woocommerce_product_single_add_to_cart_text', array( $this, 'replace_add_to_cart_text' ), 40, 2 ); |
| 130 |
} |
| 131 |
|
| 132 |
// Don't render titles if desired. |
| 133 |
if ( isset( $atts['show_title'] ) && ! $atts['show_title'] ) { |
| 134 |
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 ); |
| 135 |
} |
| 136 |
|
| 137 |
// Change form action to avoid redirect. |
| 138 |
add_filter( 'woocommerce_add_to_cart_form_action', '__return_empty_string' ); |
| 139 |
|
| 140 |
$single_product = new WP_Query( $args ); |
| 141 |
|
| 142 |
// For "is_single" to always make load comments_template() for reviews. |
| 143 |
$single_product->is_single = true; |
| 144 |
|
| 145 |
ob_start(); |
| 146 |
|
| 147 |
global $product; |
| 148 |
global $wp_query; |
| 149 |
|
| 150 |
// Backup query object so following loops think this is a product page. |
| 151 |
$previous_wp_query = $wp_query; |
| 152 |
$wp_query = $single_product; //phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
| 153 |
|
| 154 |
wp_enqueue_script( 'wc-single-product' ); |
| 155 |
|
| 156 |
while ( $single_product->have_posts() ) { |
| 157 |
|
| 158 |
$single_product->the_post(); |
| 159 |
|
| 160 |
woocommerce_template_single_add_to_cart(); |
| 161 |
|
| 162 |
/* |
| 163 |
?> |
| 164 |
<div class="single-product" data-product-page-preselected-id="<?php echo esc_attr( $preselected_id ); ?>"> |
| 165 |
<?php wc_get_template_part( 'content', 'single-product' ); ?> |
| 166 |
</div> |
| 167 |
<?php |
| 168 |
*/ |
| 169 |
} |
| 170 |
|
| 171 |
// Restore $previous_wp_query and reset post data. |
| 172 |
$wp_query = $previous_wp_query; //phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
| 173 |
|
| 174 |
wp_reset_postdata(); |
| 175 |
|
| 176 |
// Remove text change action. |
| 177 |
if ( isset( $atts['text'] ) && ! empty( $atts['text'] ) ) { |
| 178 |
|
| 179 |
self::$add_to_cart_text = ''; |
| 180 |
|
| 181 |
remove_filter( 'woocommerce_product_single_add_to_cart_text', array( $this, 'replace_add_to_cart_text' ), 40, 2 ); |
| 182 |
} |
| 183 |
|
| 184 |
// Re-enable titles if they were removed. |
| 185 |
if ( isset( $atts['show_title'] ) && ! $atts['show_title'] ) { |
| 186 |
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 ); |
| 187 |
} |
| 188 |
|
| 189 |
remove_filter( 'woocommerce_add_to_cart_form_action', '__return_empty_string' ); |
| 190 |
|
| 191 |
return '<div class="woocommerce wcf-product-atc">' . ob_get_clean() . '</div>'; |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Show a single product add to card. |
| 196 |
* |
| 197 |
* @param string $add_to_cart_text Button text. |
| 198 |
* @param obj $product Product object. |
| 199 |
* |
| 200 |
* @return string Add to cart button text. |
| 201 |
*/ |
| 202 |
public function replace_add_to_cart_text( $add_to_cart_text, $product ) { |
| 203 |
|
| 204 |
if ( ! empty( self::$add_to_cart_text ) ) { |
| 205 |
|
| 206 |
$add_to_cart_text = self::$add_to_cart_text; |
| 207 |
} |
| 208 |
|
| 209 |
return $add_to_cart_text; |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Kicking this off by calling 'get_instance()' method |
| 215 |
*/ |
| 216 |
Cartflows_Wd_Flow_Shortcodes::get_instance(); |
| 217 |
|